Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium-单击元素解决方案_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python Selenium-单击元素解决方案

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

使用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: 
回溯(最近一次呼叫最后一次):
文件“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
    

您的代码尝试使用@id定义xpath,但问题中的元素h4没有任何id,异常显示完全不同的loctor,即文本。您确定添加了正确的代码、元素和locators@PDHide是的,非常确定,请参阅下面的“答案”,了解我遇到的问题的更多细节。这个答案非常有用,但它并没有解决问题。事实上,我在发布之前已经尝试过这些。需要注意的是,这些是角度元素,因此需要时间延迟。我将在帖子中更新我的代码以反映您的输入,但它仍然不起作用。回溯超时,因为它找不到元素。@Mattr42您能交叉检查一下元素是否在快照中显示的
内吗。新的屏幕截图张贴在OP底部。有一个iframe,但元素没有明确包含在其中。底部箭头是元素所在的位置。@Mattr42同意,元素不在
范围内是的,还有其他建议吗?