0

I'm getting an error while handling default popups with selenium scripts.

Here is the Code :

@Test
public void handlePop() {

    WebElement pop1 = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceHolder1_PopupCTRLMain_imgNext']"));
    pop1.click();

    WebElement pop2 = driver.findElement(By.xpath("//*[@id='ctl00_ContentPlaceHolder1_PopupCTRLMain_Image2']"));
    pop2.click();
}

Following is the error I am getting while running the code:

org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='ctl00_ContentPlaceHolder1_PopupCTRLMain_Image2']
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Juhil Somaiya
  • 873
  • 7
  • 20
  • What is the error? – Ryan Wilson Feb 14 '18 at 17:41
  • org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='ctl00_ContentPlaceHolder1_PopupCTRLMain_Image2'] – Juhil Somaiya Feb 14 '18 at 17:42
  • That means that your element can not be found. Have you opened the developer tools in your browser and searched for that element? – Ryan Wilson Feb 14 '18 at 17:45
  • yes, I tried. I also tried with other options like with cssPath, htmlPath – Juhil Somaiya Feb 14 '18 at 17:48
  • Actually, the scenario is like two different popups are there, by clicking next button of the first popup I am jumping to another one. at that time I am getting this error – Juhil Somaiya Feb 14 '18 at 17:50
  • hope this will help you https://stackoverflow.com/questions/48736196/selenium-based-scraping-code-fails-with-the-error-nosuchelementexception/48761092#48761092 – Pradeep hebbar Feb 14 '18 at 18:05
  • 1
    More than likely your code is executing too quickly, it's clicking the first popup button, but you have no delay between that and the second popup click, I would wait until the second popup button is there before trying to click it. – Ryan Wilson Feb 14 '18 at 18:06
  • Have a look at this https://stackoverflow.com/questions/48707886/exception-handling-in-paypal-checkout-where-not-able-to-access-id-of-iframe-elem/48709719#48709719 – Pradeep hebbar Feb 14 '18 at 18:24

1 Answers1

0

You could probably add if condition to check if the pop is displayed, like

try { 
if (pop1.getSize > 0) {
  pop1.click();
 } catch (e)
}
Raa
  • 121
  • 2
  • 13