Python Selenium-单击元素解决方案
使用Python和Selenium,我尝试了多种变通方法(xpath、css选择器、id等)来捕获这个特定页面上的按钮,然后尝试单击它。似乎没有什么问题,有什么解决办法吗?这似乎与我访问的网站上“链接”本身的书写方式有关。我正在尝试让程序单击矩阵图像下方的“矩阵”链接 我的代码:Python Selenium-单击元素解决方案,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,使用Python和Selenium,我尝试了多种变通方法(xpath、css选择器、id等)来捕获这个特定页面上的按钮,然后尝试单击它。似乎没有什么问题,有什么解决办法吗?这似乎与我访问的网站上“链接”本身的书写方式有关。我正在尝试让程序单击矩阵图像下方的“矩阵”链接 我的代码: from selenium import webdriver from getpass import getpass from selenium.webdriver.common.keys import Keys fr
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:
回溯(最近一次呼叫最后一次):
文件“C:\Users\Matt\Documents\Splitt\ROI Property Finder.py”,第54行,在
WebDriverWait(driver,20)。直到(EC.element可点击((By.XPATH,//div[@id='appDetailMatrix']]//h4[@class='apptitle ng star inserted'和text()='Matrix'])。点击()
文件“C:\Users\Matt\Python3.9\lib\site packages\selenium\webdriver\support\wait.py”,第80行,直到
引发TimeoutException(消息、屏幕、堆栈跟踪)
selenium.common.Exception.TimeoutException:消息:
补充截图:
使用Selenium IDE,它告诉我xpath应该是我当前代码中的xpath,如下所示:
要单击文本为矩阵的元素,可以使用以下任一选项:
- 使用
:css\u选择器
driver.find_element_by_css_selector("div#appDetailMatrix h4.apptitle.ng-star-inserted").click()
- 使用
:xpath
driver.find_element_by_xpath("//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']").click()
元素成为可点击的()
,您可以使用以下任一选项:
- 使用
:CSS\u选择器
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#appDetailMatrix h4.apptitle.ng-star-inserted"))).click()
- 使用
:XPATH
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='appDetailMatrix']//h4[@class='apptitle ng-star-inserted' and text()='Matrix']"))).click()
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
内吗。新的屏幕截图张贴在OP底部。有一个iframe,但元素没有明确包含在其中。底部箭头是元素所在的位置。@Mattr42同意,元素不在
范围内是的,还有其他建议吗?