【问题标题】:Why cant i locate this xpath button?为什么我找不到这个 xpath 按钮?
【发布时间】:2021-03-30 14:40:18
【问题描述】:

我尝试使用 selenium 通过 xpath 单击按钮,但它不起作用,我不知道为什么

它总是说:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“我在下面写的方式”}

元素显示如下:

<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()

标签: python selenium xpath


【解决方案1】:

您的xpath 似乎有误。属性名应该以@开头

试试下面xpath

//button[@class='dCJp8 afkep']

代码:

browser.find_element_by_xpath("//button[@class='dCJp8 afkep']").click()

【讨论】:

  • 非常感谢,它成功了。你为我节省了很多时间。
【解决方案2】:
//button[./span[@aria-label='load comments']]

您也可以将其用作 xpath。

【讨论】:

    【解决方案3】:

    也许在你的方法中尝试一个异常

    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();
    }
    

    【讨论】:

    • 问题被标记为python。你能把答案改成使用 Python 代码吗?
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2022-01-01
    • 2018-11-06
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多