Home > Software design >  Not able to click Solve Puzzle button of amazon using selenium webdriver in java
Not able to click Solve Puzzle button of amazon using selenium webdriver in java

Time:02-06

Not able to click Solve Puzzle button of amazon using selenium webdriver in java. Here is the html code

enter image description here

i have used fluent wait. But it's not working

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(5))
        .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(driver1 -> {
    return driver1.findElement(By.xpath("//button[@id='home_children_button' and contains(text(),'Solve Puzzle')]"));

});
foo.click();

CodePudding user response:

For the vast majority of cases, you should use WebDriverWait instead of FluentWait. WebDriverWait inherits FluentWait and is a simpler version of it that uses ExpectedConditions that contains all the common things you would want to wait for.

For your locator, all you should need is the ID to specify that element.

Your code can be rewritten simply as

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("home_children_button"))).click();

CodePudding user response:

Apparently I don't see any major issue in your lines of fluent wait code.

However, you can modify the locator a bit and use:

WebElement foo = wait.until(driver1 -> {
        return driver1.findElement(By.xpath("//button[@id='home_children_button' and contains(.,'Solve Puzzle')]"));
        

Or even

WebElement foo = wait.until(driver1 -> {
        return driver1.findElement(By.xpath("//button[@id='home_children_button' and @aria-describedby='descriptionVerify'][contains(.,'Solve Puzzle')]"));

Alternative

As an alternative to FluentWait you can also use WebDriverWait which is a specialization of FluentWait that uses WebDriver instances.

To click on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#home_children_button[aria-describedby='descriptionVerify']"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='home_children_button' and @aria-describedby='descriptionVerify'][contains(.,'Solve Puzzle')]"))).click();
    
  • Related