Selenium xpath 没有这样的元素异常,即使它在 firepath 中工作

Selenium xpath no such element exception even though it works in firepath

这是我使用 Firebug

检查元素时的样子

当我在 xpath 中尝试使用相同的语法时,它会选择结果页面 2。我在 selenium IDE 中尝试了相同的方法并单击“查找”时,它会在执行代码时选择结果页面 2。我得到 No Such Element exception

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

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

实际上有3个错误:

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

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

解法:

添加步骤向下滚动 网页

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");
  1. Xpath 不是一概而论的。 它是为了获取 URL 放在 2 num 页的任何内容。所以更改如下:

    //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 for Firefox and Chromedriver for Chrome 浏览器 Selenium 3.0+ version

尝试使用以下 XPath:

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

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

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