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
工具书类 您可以在以下内容中找到有关的讨论:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC