简体   繁体   English

在 selenium 中通过 xpath 查找元素

[英]Find element by xpath in selenium

When the test case with below lines is executed, i get below error -当执行带有以下几行的测试用例时,出现以下错误 -

NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//div[@id='sizzle-1442313418270']/div/table/tbody/tr/td[3]"} NoSuchElementException:消息:无法定位元素:{"method":"xpath","selector":"//div[@id='sizzle-1442313418270']/div/table/tbody/tr/td[3]" }

    driver.find_element_by_xpath("//div[@id='sizzle-1442313418270']/div/table/tbody/tr/td[3]").click()
    driver.find_element_by_xpath("//div[@id='sizzle-1442313418270']/div/table/tbody/tr[2]/td[4]").click()

I can't comment so must give this as an answer.我无法发表评论,因此必须将其作为答案。
If you are sure the element exist then before the line of execution put a Thread.sleep (5000) and if that works you will then know selenium is executing before element loads.如果您确定该元素存在,那么在执行行之前放置一个Thread.sleep (5000) ,如果有效,您就会知道 selenium 在元素加载之前正在执行。 You then delete the sleep and wait for something else to appear on the page using explicit wait then execute the rest of the code然后删除睡眠并使用显式等待等待页面上出现其他内容,然后执行其余代码

Try尝试

WebElement element_p = (new WebDriverWait(_driver, 3))
            .until(ExpectedConditions.visibilityOfElementLocated(By
                    .xpath("//div[@id='sizzle-1442313418270']/div/table/tbody/tr/td[3]"))).click();

Another possibility could be that your element is in an iframe?另一种可能性可能是您的元素在 iframe 中?

Use implicit wait statement before your date statement日期语句之前使用隐式等待语句

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

It will be better to execute wait statement after creation of firefox Constructor because before execution of findElement statement ,implicit statement will definitely run创建 firefox 构造函数后执行 wait 语句会更好,因为在执行 findElement 语句之前,隐式语句肯定会运行

if it doesn't work then go for explicit statement如果它不起作用然后去明确声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM