[英]Why cant i locate this xpath button?
我尝试通过 xpath 和 selenium 单击按钮,但它不起作用,我不知道为什么
它总是说:
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“我在下面写的方式”}
元素显示如下:
<button class="dCJp8 afkep"><span aria-label="load comments" class="glyphsSpriteCircle_add__outline__24__grey_9 u-__7"></span></button>
#i already tried this ways:
browser.find_element_by_xpath("//button[class='dCJp8 afkep']").click()
browser.find_element_by_xpath("//button[text()='load comments'").click()
#and i also tried it like this:
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/li/div/button").click()
browser.find_element_by_xpath("/html/body/div[5]/div[2]/div/article/div[3]/div[1]/ul/li/div/button").click()
#but everything wont work
这是代码,如果你们想自己尝试一下(登录的第 13 行和第 14 行)
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome()
url = "https://www.instagram.com/"
browser.get(url)
sleep(1)
browser.find_element_by_xpath("/html/body/div[2]/div/div/div/div[2]/button[1]").click()
#Login
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input").send_keys("Username")
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input").send_keys("Password")
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").click()
sleep(3)
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button").click()
browser.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]").click()
url = "https://www.instagram.com/p/CLMCNSenf-E/"
browser.get(url)
#try:
# browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/li/div/button").click()
#except:
# browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div[1]/article/div[3]/div[1]/ul/li/div/button").click()
browser.find_element_by_xpath("//button[text()='load comments'").click()
您的xpath
似乎是错误的。 属性名称应以@
开头
试试下面xpath
//button[@class='dCJp8 afkep']
代码:
browser.find_element_by_xpath("//button[@class='dCJp8 afkep']").click()
//button[./span[@aria-label='load comments']]
您也可以将其用作 xpath。
也许在你的方法中尝试一个例外
public static void clickCatchingStaleException(WebElement element) {
try {
element.click();
} catch (Exception e) {
element.click();
}
}
或者可能点击操作
public static void clickWithActions(WebDriver wd, WebElement element) {
Actions actions = new Actions(wd);
actions.moveToElement(element).click().perform();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.