I'm new to automation, using JAVA with Selenium to do some basic tests on a website. I've stumbled upon a Cookies pop-up.
Seems like the element is not visible on the page when I'm trying to click it(waitForElementToBeVisible is not doing it either).
I've read all the related posts on SO and YT videos on how to bypass this, it doesn't seem to work for me.
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class Login {
private static final String BASE_URL = "https://www.demo.guru99.com/V4/";
private static final By USER_ID = By.name("uid");
private static final By PASSWORD = By.name("password");
private static final By LOGIN_BTN = By.name("btnLogin");
private static final By ACCEPT_PRIVACY = By.xpath("//*[@id=\"save\"]/span[1]/div");;
private WebDriver driver;
public Login(WebDriver driver) {
this.driver = driver;
}
public void navigate() {
driver.get(BASE_URL);
}
public void setUsername(String username) {
driver.findElement(USER_ID).clear();
driver.findElement(USER_ID).sendKeys(username);
}
public void setPassword(String password) {
driver.findElement(PASSWORD).clear();
driver.findElement(PASSWORD).sendKeys(password);
}
public void clickLogin() {
driver.findElement(LOGIN_BTN).click();
}
public void clickAcceptPrivacy() {
driver.findElement(ACCEPT_PRIVACY).click();
}
// public String popupText() {
// String text = driver.findElement(ACCEPT_PRIVACY).getText();
// return text;
// }
//
// public void waitAcceptPrivacy() {
// new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.elementToBeClickable(ACCEPT_PRIVACY)).click();
// }
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import pageObjects.Login;
import java.util.concurrent.TimeUnit;
public class TestDrive {
private final WebDriver driver = new ChromeDriver();
private final Login login = new Login(driver);
@Before
public void setup() {
//use Chrome driver
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void Test1() throws InterruptedException {
login.navigate();
Thread.sleep(1000);
login.clickAcceptPrivacy();
login.setUsername("mngr473114");
login.setPassword("ajybEvu");
Thread.sleep(2000);
login.clickLogin();
}
}
}
This is the error I'm getting:
org.openqa.selenium.NoSuchElementException: Unable to find element with locator By.xpath: //*[@id="save"]/span[1]/div
Tried different xpaths, css; tried ExpectedConditions.
I'm expecting to close the pop-up by clicking the Accept All button.
Thank you!
CodePudding user response:
I didn't get any popups on this page. But probably this popup contained in 'iframe' block if so, you should switch to 'iframe' at first and only then do some action with element
//Store the web element
WebElement iframe = driver.findElement(By.cssSelector("iframe path"));
//Switch to the frame
driver.switchTo().frame(iframe);
//Now we can click the button
driver.findElement(ACCEPT_PRIVACY).click();
Check this out https://www.selenium.dev/documentation/webdriver/interactions/frames/
CodePudding user response:
You could try is to use an explicit wait to wait for the element to be present before interacting with it. You can use the ExpectedConditions class to wait for the element to be visible and clickable before interacting with it.
public void clickAcceptPrivacy() {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement acceptPrivacy = wait.until(ExpectedConditions.elementToBeClickable(ACCEPT_PRIVACY));
acceptPrivacy.click();
}