无法从某些盒状容器中获取标题

2024-04-28 17:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中编写了一个脚本,与selenium关联,用于单击网页中地图上的一些可用点。单击一个点时,会弹出一个包含相关信息的小框。你知道吗

Link to that site

我想解析每个框的标题。当我执行脚本时,它在单击一个点时抛出一个错误。我怎样才能成功地走?你知道吗

以下是脚本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

link = "replace with above link"

driver = webdriver.Chrome()
driver.get(link)
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mapDiv_zoom_slider .esriSimpleSliderIncrementButton"))).click()
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"#NWQMC_VM_directory_June2016_3915_0_layer circle"))):
    item.click()

我出错了:

line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <circle fill="rgb(237, 81, 81)" fill-opacity="1" stroke="rgb(153, 153, 153)" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="4" cx="720" cy="430" r="4" transform="matrix(1.00000000,0.00000000,0.00000000,1.00000000,0.00000000,0.00000000)" fill-rule="evenodd" stroke-dasharray="none" dojoGfxStrokeStyle="solid"></circle> is not clickable at point (720, 430). Other element would receive the click: <circle fill="rgb(20, 158, 206)" fill-opacity="1" stroke="rgb(153, 153, 153)" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="4" cx="720" cy="430" r="4" transform="matrix(1.00000000,0.00000000,0.00000000,1.00000000,0.00000000,0.00000000)" fill-rule="evenodd" stroke-dasharray="none" dojoGfxStrokeStyle="solid"></circle>
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86)

盒子就是这样弹出的:

enter image description here


Tags: fromimport脚本bystrokedriverseleniumlink
2条回答

要回答您的特定问题,您不能像往常一样与svg元素交互。为此,您必须使用xPath,就像我在示例中提供的那样。您也不能像往常一样单击这些元素,但可以使用ActionChains。你知道吗

wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mapDiv_zoom_slider .esriSimpleSliderIncrementButton"))).click()
time.sleep(3)
items = driver.find_elements_by_xpath("//*[name()='svg']//*[name()='circle']")
i = 0
for item in items:
    try:
        time.sleep(1)
        ActionChains(driver).move_to_element(item).click(item).perform()
    except Exception:
        print("Can't click")

这段代码工作,每个元素将被单击,直到地图将被放大。在其中一个元素的地图放大,它不工作之后。为什么?我还没发现,但你可以自己找,或者问其他问题,我们会尽力帮助你。你知道吗

注意:您必须添加一些导入:

from selenium.webdriver.common.action_chains import ActionChains
import time

编辑:我发现了问题,您必须在单击后关闭每个弹出窗口,然后它才能工作。工作代码如下:

wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mapDiv_zoom_slider .esriSimpleSliderIncrementButton"))).click()
time.sleep(3) # wait until all elements will be ready
items = driver.find_elements_by_xpath("//*[name()='svg']//*[name()='circle']")
for item in items:
    time.sleep(0.5) # small pause before each iteration
    ActionChains(driver).move_to_element(item).click(item).perform()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@title = 'Close']"))).click()

我没有找到避免time.sleep()的方法,可能在这种特殊情况下是不可能的。你知道吗

@Andrei Suvorkov非常接近(+1)

尝试以下代码以获得所需的输出:

from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get(link)
wait = WebDriverWait(driver, 5)
driver.maximize_window()

items = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//*[name()='svg']//*[name()='circle']")))
for item in items:
    ActionChains(driver).move_to_element(item).click(item).perform()
    popup = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "contentPane")))
    print(popup.text)
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'close')]"))).click()

相关问题 更多 >