无法使用selenium在Python中定位元素
我在使用xPath定位元素时遇到问题无法使用selenium在Python中定位元素,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我在使用xPath定位元素时遇到问题 嘿,我试过使用xPath和其他方法,但找不到该元素。如果您试图单击下拉按钮,请使用下面的xPath //li/a[@href="/drop"]//span[.='Drop'] 包括以下进口: from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriv
嘿,我试过使用xPath和其他方法,但找不到该元素。如果您试图单击下拉按钮,请使用下面的xPath
//li/a[@href="/drop"]//span[.='Drop']
包括以下进口:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
您的代码应该是:
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']//span[.='Drop']"))).click()
要在元素上单击(),您必须引导WebDriverWait使元素成为可单击的()
,并且您可以使用以下任一选项:
- 使用
:CSS\u选择器
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li > a[href=/drop] > button span:nth-of-type(2)")))
- 使用
:XPATH
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/a[@href='/drop']/button//span[text()='Drop']")))
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
raise-TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:message:
使用xPath时仍然出现错误@DebanjanB它没有显示错误,但没有显示错误working@LukasAlišauskas TimeoutException是预期条件失败的结果。通过find\u element\u by.*
与time.sleep()
一起调试代码。如果您能够找到元素,请使用观察结果更新问题。检查HTML并向上遍历,观察元素是否在标记内,然后在单击元素之前必须先切换到所需的
。检查此项尝试了很多东西,但仍然没有写入阅读全文讨论仍然我遇到错误当我试图通过控制台查找元素时没有错误i获取未捕获的语法错误:意外标识符
,但当我使用ChroPath时,它会查找元素和所有内容ok请帮助@supputuri找到方法驱动程序。获取('https://link/drop')
像这样,但有一个按钮,我不能点击它,同样的问题
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC