Issue
This is how it looks like when I inspect an element using Firebug:
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();
Solution
There are actually three mistakes:
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.
Solution:
Add steps to scroll down the web page:
JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.scrollBy(0, 1000)", "");
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']
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.
Answered By - Sagar007
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.