【发布时间】:2021-01-07 16:30:16
【问题描述】:
使用 Python 和 Selenium,我尝试了多种解决方法(xpath、css 选择器、id 等)来捕获此特定页面上的按钮,然后尝试单击它。似乎没有任何问题,有什么解决办法吗?似乎它与“链接”本身在我正在访问的网站上的书写方式有关。我正在尝试让程序单击矩阵图像下方的“矩阵”链接。
我的代码:
from selenium import webdriver
from getpass import getpass
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("C:\\Users\\Matt\\Documents\\WebDriver\\chromedriver_win32\\chromedriver.exe")
#This site requires log in entry so you wont be able to access it yourself.
driver.get("https://www.stellarmls.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).click()
print(driver.page_source)
“矩阵”链接的网站源代码:
<h4 _ngcontent-c113="" class="apptitle ng-star-inserted" style="">Matrix</h4>
提供的屏幕截图可让您更好地了解正在发生的事情。任何建议都将不胜感激,我已经梳理了 stackoverflow 并发现了类似的问题,但是通过非常简单的更改解决了他们的问题,我尝试自己无济于事。
回溯错误:
Traceback (most recent call last):
File "C:\Users\Matt\Documents\Splitt\ROI Property Finder.py", line 54, in <module>
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).click()
File "C:\Users\Matt\Python3.9\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
使用 Selenium IDE 它告诉我 xpath 应该是我当前代码中的内容,如下所示:
【问题讨论】:
-
您的代码尝试使用 @id 查找 xpath,但您问题中的元素 h4 没有任何 id ,异常显示完全不同的定位符,即 text 。你确定你添加了正确的代码、元素和定位器
-
@PDHide 是的,非常确定,有关我遇到的问题的更多详细信息,请参见下面的“答案”。
标签: python selenium xpath css-selectors webdriverwait