1

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

Enter image description here

When I try this same syntax with an XPath expression, it selects result page 2. I tried the same approach in the Selenium IDE and clicked on find. It selects result page 2. However, while executing the code, I am getting 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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sheikh Rahman
  • 895
  • 3
  • 14
  • 29
  • Hi, are you sure your xpath matches one and only one element? Selenium tries to click on the very first element. If it's not displayed or clickable, it's normal you get an exception. – Zoette Dec 12 '16 at 04:33
  • 1 matching node, is what I see when I use that syntax in firepath. Thanks – Sheikh Rahman Dec 12 '16 at 04:35
  • You are searching for Seattle but in the xpath you are using Renton. Modify your xpath and try to make it reusable across searches. – Grasshopper Dec 12 '16 at 05:18
  • What is "firepath"? [Firebug](https://en.wikipedia.org/wiki/Firebug_%28software%29) (in the body)? [Firebase](https://en.wikipedia.org/wiki/Firebase)? [Firestore](https://en.wikipedia.org/wiki/Firebase#Firebase_Cloud_Firestore)? – Peter Mortensen Nov 14 '22 at 22:59
  • OK, it is probably [FirePath](https://stackoverflow.com/questions/41992056/why-doesnt-firepath-work-anymore-since-firefox-51-0-1). – Peter Mortensen Nov 14 '22 at 23:06

3 Answers3

5

There are actually three mistakes:

  1. The big mistake: The script is not able to find the visible option of the next page.

    In the given screenshot, the result while run given code. This is why the script is unable to find the element.

    The Selenium WebDriver script is only working with the visible area.

    Enter image description here

    Solution:

    Add steps to scroll down the web page:

    JavascriptExecutor jse = (JavascriptExecutor) driver;
    jse.executeScript("window.scrollBy(0, 1000)", "");
    
  2. The XPath expression is not generalized. 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']

  3. There are some interruptions like a pop up, region-based URL. So write code like the following 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 the 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+ versions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sagar007
  • 848
  • 1
  • 13
  • 33
0

Try with the following XPath expression:

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

Note: 2 - for the 2nd page link. Change it as per the page number you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
0

Because the element might not have been loaded at that time in the DOM, and you are using a wait statement after searching for that element.

But the wait statement must be added prior to the actions and searching for the elements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131