0

没有这样的元素例外,这是它的样子,当我使用Firebug硒的XPath,即使它在firepath

enter image description here

当我尝试了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(); 

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

+0

嗨,你确定你的xpath匹配一个且只有一个元素?硒试图点击第一个元素。如果它没有显示或可点击,这是正常的,你会得到一个异常。 –

+0

1匹配节点,是我在firepath中使用该语法时看到的。谢谢 –

+0

您正在寻找西雅图,但在您使用伦顿的xpath中。修改您的xpath并尝试在整个搜索中重复使用它。 – Grasshopper

回答

3

实际上有3个失误:

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

硒webdriver的脚本只用可见区域合作

enter image description here

解决方案:

添加步骤向下滚动网页

JavascriptExecutor jse = (JavascriptExecutor) driver; 
jse.executeScript("window.scrollBy(0,1000)", ""); 
  1. Xpath不是泛化的。这是为了得到任何URL被放置在2数量页面。所以变化是每下:

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

注:硒3.0+版本

+0

详细的解释,非常感谢。 –

0

尝试读取Chrome浏览器的GeckoDriver Firefox和Chromedriver与以下XPath:

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

备注:2 - 对于2nd page link。根据你想要的页码改变它