Python 硒没有接触元素异常,但元素存在

Python 硒没有接触元素异常,但元素存在,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我有这个密码 driver.implicitly_wait(300) #I have tried different timers driver.find_element_by_xpath("(//button[@aria-pressed='false'])[1]").click() 但是我得到了这个错误 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable t

我有这个密码

driver.implicitly_wait(300) #I have tried different timers
driver.find_element_by_xpath("(//button[@aria-pressed='false'])[1]").click()
但是我得到了这个错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//button[@aria-pressed='false'])[1]"}
然而,这存在于我的HTML中

<div class="webix_el_box" style="width:90px; height:26px">
<button aria-pressed="false" type="button" class="webix_img_btn" style="line-height:22px;">
<span class="webix_icon_btn fa-lock red" style="max-width:22px;">
</span> Read Only
</button>
</div>
这两种xpath在Google Chrome中都运行良好 在网站中,我可以使用Ctrl+F和“只读”找到按钮,我可以在Selenium中使用它吗

编辑:它不起作用,因为

请尝试仅执行此操作

driver.implicitly_wait(300)
driver.find_element_by_xpath("//button[@aria-pressed='false']").click()


这将单击满足要求的第一个元素。

要单击文本为只读的元素,必须使
元素可单击()
,并且可以使用以下任一选项:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.webix_el_box > button.webix_img_btn"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='webix_el_box']/button[@class='webix_img_btn' and contains(., 'Read Only')]"))).click()
    
  • 注意:您必须添加以下导入:

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

工具书类 您可以在以下内容中找到有关的讨论:


我的HTML中有3个XPATH,我需要单击我尝试过的第一个。selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{“方法”:“XPATH”,“选择器”:“//button[@aria pressed='false']”编辑:这些按钮中有3个内容几乎相同,需要根据需要选择三个,这就是为什么[1]在我的XPATHHi DebanjanB中,非常感谢您的回复。即使在那段时间,我也会超时。例如,当我尝试使用XPath时。回溯(最后一次调用):WebDriverWait(driver,20)。直到(EC.element_to_be_可点击((By.XPATH,//div[@class='webix_el_box']]/按钮[@class='webix_img_btn'并包含(,'Read Only'))))。点击()文件“C:\Users\jrex\PycharmProjects\Dragon\venv\lib\site packages\selenium\webdriver\support\wait.py”,第80行,在until raise TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:message:Hi@DebanjanB中,我发现了问题所在。我在点击另一页的一个按钮后来到这个页面。URL并没有改变,但我打印了(driver.page_source),它仍然在打印上一页的数据。不确定如何加载此页面。如果你有任何想法,请帮忙。嗨,我认为它不起作用都是因为这个原因:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC