I'm trying to click the "PLAY DEMO"
button and and then click the "CONFIRM AGE"
button on a pop-up screen on this Website
This will then allow me to enter This Page
But it is not happening. I tried relative xpath and absolute expath. On my code below I'm able to open a website and find the title and also click the cookies confirm button. Can anyone help?
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;
public class CraigTRYtesting {
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
Logger logger = Logger.getLogger("");
logger.setLevel(Level.OFF);
StdErrLog logG = new StdErrLog();
logG.setLevel(StdErrLog.LEVEL_OFF);
Log.setLog(logG);
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
driver.get("https://www.spribe.co/games/aviator");
System.out.println("WEBSITE TITTLE " driver.getTitle());
System.out.println("WEBSITE FOUND");
//COOKIE
driver.findElement(By.xpath("//html[1]/body[1]/app-root[1]/app-layout[1]/app-cookie-accept[1]/div[1]/div[1]/button[1]")).click();
System.out.println("COOKIE BUTTON CLICKED");
//CLICK BUTTON
driver.findElement(By.xpath("//html[1]/body[1]/app-root[1]/app-layout[1]/app-game-details[1]/div[1]/section[1]/div[1]/div[1]/div[1]/a[1]/button[1]")).click();
System.out.println("DEMO BUTTON CLICKED");
//CONFIRM AGE BUTTON
driver.findElement(By.xpath("//html[1]/body[1]/ngb-modal-window[1]/div[1]/div[1]/app-age-permission-modal[1]/div[3]/button[1]")).click();
System.out.println("CONFIRM AGE BUTTON CLICKED");
//ROUND COUNTER
String counter = driver.findElement(By.xpath("///div[@class='coefficient coefficient-sm']")).getText();
System.out.println("ROUND COUNTER FOUND " counter);
driver.quit();
}
}
THE ROUND COUNTER WILL BE ON THIS Page
AFTER CONFIRMING THE AGE.
CodePudding user response:
When you click on CONFIRM AGE
button a new tab is opened. And need to switch to
the new window to extract the required information.
And better to apply some Explicit
waits.
// Imports Required:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
public void spribe() {
System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://www.spribe.co/games/aviator");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()=' Got it']"))).click(); // Cookie pop-up
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Play Demo ']"))).click(); // Play demo button
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='modal-content']//button[1]"))).click(); // Confirm age button
Set<String> windows = driver.getWindowHandles(); // Get all the Windows
List<String> windows1 = new ArrayList<>(windows);
driver.switchTo().window(windows1.get(1)); // Switch to new window
String roundcounter = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(@class,'coefficient')]"))).getText(); // Get the value
System.out.println(roundcounter);
}
1.27x