python selenium无法单击标记

2024-05-29 03:44:22 发布

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

我正在尝试单击下面显示的名为“Segment Metrix 2.0 Consumer Target Profile report”的按钮

enter image description here

我找到了相应的HTML,如下所示。在

enter image description here

我尝试编写如下代码:

elem = driver.find_elements_by_xpath("//*[contains(text(), 'Segment Metrix 2.0 Consumer Target Profile report ')]")
print (elem)

它给了我:

^{pr2}$

但是我不能通过添加elem[0].click()来点击它,它会抛出一个“elementnotvisible”错误。我该怎么办?在


Tags: 代码reporttargetbyconsumerhtmldriversegment
3条回答

问题是元素必须是可见的。这意味着,即使它是用html编写的,也不够,它必须在浏览器中可见。尝试先单击下拉列表以查看其元素,然后再单击其中一个元素。另外,在单击下拉列表之后,不要忘记等待您的元素被显式或隐式地看到。在

你确定要点击它吗?如果它只是一个带有url的对象,请精确指定url并使用driver.get(url)

尝试等待元素变为可见:

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

wait(driver, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, "Segment Metrix 2.0 Consumer Target Profile report"))).click()

您可以等待元素可见

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

elem = driver.find_elements_by_xpath("//*[contains(text(), 'Segment Metrix 2.0 Consumer Target Profile report ')]")
WebDriverWait(driver, 10).until(EC.visibility_of(elem[0]))
elem[0].click()

相关问题 更多 >

    热门问题