【问题标题】:Selenium xpath no such element exception even though it works in firepathSelenium xpath 没有这样的元素异常,即使它在 firepath 中工作
【发布时间】:2017-04-26 21:58:25
【问题描述】:

这是我使用 Firebug 检查元素时的样子

当我在 xpath 中尝试相同的语法时,它会选择结果页面 2。我在 selenium IDE 中尝试了相同的方法并单击查找,但在执行代码时它会选择结果页面 2。我得到没有这样的元素例外

Xpath 语法://a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']

public void jobSearch(){
        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.indeed.com");
        driver.findElement(By.id("what")).sendKeys("QA Engineer");
        driver.findElement(By.id("where")).clear();
        driver.findElement(By.id("where")).sendKeys("Seattle,WA");
        driver.findElement(By.id("fj")).click();
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

        driver.findElement(By.xpath("//a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']")).click();

感谢您的宝贵时间和宝贵建议。

【问题讨论】:

  • 嗨,你确定你的 xpath 匹配一个且只有一个元素吗? Selenium 尝试单击第一个元素。如果它没有显示或无法点击,那么你得到一个异常是正常的。
  • 1 个匹配节点,是我在 firepath 中使用该语法时看到的。谢谢
  • 您正在搜索西雅图,但在 xpath 中您使用的是 Renton。修改您的 xpath 并尝试使其在搜索中可重用。

标签: selenium xpath selenium-webdriver selenium-firefoxdriver


【解决方案1】:

其实有3个错误:

  1. 最大的错误,脚本无法找到下一页的可见选项。 在给定的屏幕截图中,运行给定代码时的结果。这就是脚本无法找到元素的原因。

Selenium Webdriver 脚本仅适用于可见区域

解决方案:

添加步骤向下滚动网页

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");
  1. Xpath 不是泛化的。 它用于获取放置在2 num 页面的任何URL。所以改变如下:

    //div[@class='pagination']//span[text()='2']

  2. 有一些中断,如弹出、基于区域的 url。所以为了消除未来的错误,编写代码如下所示:

    public void jobSearch()   {
    
    
        try{
            WebDriver driver= new FirefoxDriver();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("https://www.indeed.com");
            try
            {
                //Here region based URL gets open so remove it if it is directly open www.indeed.com
                driver.findElement(By.linkText("www.indeed.com")).click();
            }
            catch (Exception e)
            {
    
            }
            driver.findElement(By.id("what")).sendKeys("QA Engineer");
            driver.findElement(By.id("where")).clear();
            driver.findElement(By.id("where")).sendKeys("Seattle,WA");
            driver.findElement(By.id("fj")).click();
            try
            {
                // After this one pop up gets open so close it
                driver.findElement(By.xpath("//button[@id='prime-popover-close-button']/span")).click();
            }
            catch (Exception e)
            {
    
            }
            //pageDown(driver.findElement(By.id("searchCount")), 2);
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("window.scrollBy(0,1000)", "");
            //driver.findElement(By.xpath("//a[contains(@href,'/jobs?q=qa+engineer&l=Renton%2C+WA&start=10')]/span[contains(@class,'pn')][text()='2']")).click();
            driver.findElement(By.xpath("//div[@class='pagination']//span[text()='2']")).click();
            // Now continue you code here
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }   }
    

注意:请阅读 GeckoDriver 用于 Firefox 和 Chromedriver 用于 Selenium 3.0+ 版本

的 Chrome 浏览器

【讨论】:

    【解决方案2】:

    尝试使用以下 XPath:

    //div[@class='pagination']/a/span[text()='2'] 
    

    注意: 2 - 代表2nd page link。根据您想要的页码更改它

    【讨论】:

      【解决方案3】:

      因为当时可能尚未在 DOM 中加载该元素,并且您在搜索该元素后正在使用等待语句。但是必须在元素的操作和搜索之前添加等待语句

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-21
        • 2016-10-27
        • 2021-10-21
        • 1970-01-01
        • 2021-01-11
        • 2022-11-14
        相关资源
        最近更新 更多