【问题标题】:Python Selenium - Clicking an element workaroundPython Selenium - 单击元素解决方法
【发布时间】: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


【解决方案1】:

要单击文本为 Matrix 的元素,您可以使用以下任一Locator Strategies

  • 使用css_selector

    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()
    

由于该元素是Angular 元素,理想情况下单击该元素需要为WebDriverWait 诱导element_to_be_clickable(),您可以使用以下Locator Strategies 之一:

  • 使用CSS_SELECTOR

    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
    

【讨论】:

  • 这个答案非常有用,但它并没有解决问题。事实上,我在发布之前已经尝试过这些。重要的是要注意,是的,这些是 Angular 元素,因此需要时间延迟。我将在帖子中更新我的代码以反映您的输入,但它仍然不起作用。 Traceback 超时,因为它找不到元素。
  • 新截图贴在 OP 底部。有一个 iframe,但该元素未明确包含在其中。底部箭头是元素所在的位置。
  • @Mattr42 同意,元素不在&lt;iframe&gt;
  • 是的,还有其他建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 2019-04-13
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多