Selenium xpath no element element exception even if it works in firepath

advertisements

This is how it looks like when I inspect element using Firebug

When I try this same syntax in xpath it selects result page 2. I tried same approach in selenium IDE and clicked on find, it selects result page 2 however while executing the code. I am gettting No Such Element exception

Xpath syntax: //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();

Thank You for your time and valuable suggestion.


There are actually 3 mistakes :

  1. The big mistake, Script is not able to find visible option of next page. In given screenshot the result while run given code. This is why script is unable to find element.

Selenium Webdriver script is only working with Visible Area

Solution :

Add steps to scroll down web page

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,1000)", "");

  1. Xpath is not generalize. It is for get whatever the URL is placed at 2 num page. So change is as per under :

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

  2. There are some interruption like pop up , region based url. So write code is like as under given for eliminating the future error:

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

Note : Please read GeckoDriver for Firefox and Chromedriver for Chrome browsers with Selenium 3.0+ version