-2

I have opened this site https://www.seleniumeasy.com/test/basic-first-form-demo.html

as I open there is popup showing

How to go on that popup & click on close using selenium?

James Z
  • 12,209
  • 10
  • 24
  • 44
reena
  • 1
  • Does this answer your question? [How to handle Pop-up in Selenium WebDriver using Java](https://stackoverflow.com/questions/19403949/how-to-handle-pop-up-in-selenium-webdriver-using-java) – rahul rai Sep 02 '20 at 10:41

2 Answers2

1

Use below code. These are some chances that you are not using proper wait and XPath.

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='at-cv-lightbox-close']")));
    driver.findElement(By.xpath("//*[@id='at-cv-lightbox-close']"));
    driver.quit();
Dilip Meghwal
  • 632
  • 6
  • 15
  • Hi I tried below code pls check in screenshot but still got error https://prnt.sc/ua1em0 – reena Sep 02 '20 at 06:00
  • XPath for user message highlighting two element, so you have to create unique xpath. Here I am using more than one attribute. XPath : //*[@id="user-message" and @class="form-control"] – Dilip Meghwal Sep 02 '20 at 06:37
1

To click and close the popup you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[title='Close']"))).click();
    
  • xpath:

    new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Close']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352