2

I tried driver.switchTo().alert().accept(); but still i am unable to accept the alert message. please let me know the code to accept below message.

enclosed screenshot of message and XML

enter image description here

dzieciou
10.5k9 gold badges49 silver badges102 bronze badges
asked Nov 19, 2018 at 14:07
2
  • Hi Narendra please see below URL : expreview.exchange.uk.com/Public/Login/… Commented Nov 20, 2018 at 10:29
  • @NarendraR If you have an answer, please post an answer. Comments are not the place for answers. Thanks! Commented Nov 20, 2018 at 20:41

3 Answers 3

2

As far as I can tell from your screenshot, you do not face an alert. The "Cookie control" is just a HTML element on your website and is styled to overlay the website.

So you just need to click on the "Cancel" button like on any other WebElement.

Maybe there are some challanges you will face. Like when does this popup appears? Maybe you need to implement a logic like the following, when you visit the website the very first time:

WAIT FOR THE POPUP TO APPEAR
IF THE POPUP IS PRESENT:
 CLICK THE CANCEL BUTTON

You can use the WebDriverWaits to hande this: https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

Another option would be to set a cookie before you visit the website. Maybe the popup does not appears when a certain cookie is set. This way you do not need to interact with the popup at all, which saves you execution time and makes your tests more stable.

answered Nov 21, 2018 at 11:35
4
  • thanks for the info. Any chance can you write the code for above logic. I have used WebDriver Waits logic as well but still i was getting error Commented Nov 21, 2018 at 11:38
  • @Arjun What exactly was the error? Maybe it is more helpful than just providing the code here. Commented Nov 26, 2018 at 8:15
  • Errror message is : error message Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: Commented Nov 26, 2018 at 9:35
  • @Arjun This exception will be thrown, when the element was not found after the set timeout. You can ignore this by using another wait class. Example: FluentWait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(10)) .pollingEvery(Duration.ofMillis(200)) .ignoring(NoSuchElementException.class); Commented Nov 27, 2018 at 10:22
0

A common reason of failure it might be that webdriver is trying to click to accept an alert before is present in the DOM or clickable, using a fluent wait should works:

public void acceptAlert() {
 try {
 new WebDriverWait(driverProvider.get(), 100).until(ExpectedConditions.alertIsPresent());
 switchTo().alert().accept();
 } catch (Exception e) {
 e.printstacktrace());
 }
 }

Although what i see in the developer console it doesn't looks like an alert but a form button what you want to click instead, have you tried something like this? :

WebElement inputButton = driver.findElement(By.xpath("//input[contains(@class, 'btnCancelCookie')]"));
 inputButton.click();
answered Dec 28, 2018 at 13:00
0

But for your case, it seems it is not alert popup. It seems like cookies just an HTML element from the web page.

I am recommending the below code to handle the cookies:

for (Cookie cook : driver.manage().getCookies()) {
 String writeup = cook.getName();
 driver.manage().deleteCookie(cook); 
}

This is for handling alert window to accept it.

Alert alert = driver.switchTo().alert();
WebUtil.waitFor(500);
alert.accept();
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Dec 28, 2018 at 11:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.