Skip to content
Advertisement

Unable to locate xpath on Selenium.htmlunit.HtmlUnitDriver

I’m trying to click the "PLAY DEMO" button and and then click the `”C

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement