Home > OS >  Automation: How to click on div role="radio" with only aria-label identifier?
Automation: How to click on div role="radio" with only aria-label identifier?

Time:12-03

I have the following code written in Java with Selenium Webdriver, but it's not clicking the div with the star rating

driver.get("https://goo.gl/maps/gLCX3PitJT1cXr9v9");
driver.findElement(By.xpath("//button[@data-value=\"Escribir una opinión\"]")).click();
driver.switchTo().frame(2);
driver.findElement(By.xpath("//textarea")).sendKeys("This is just a test");
driver.findElement(By.xpath("//span[@aria-label=\"Cuatro estrellas\"]")).click();

Between each line, I also added a Thread.sleep(5000) just to ensure the page fully loads.

The only clear identifier that I see is the aria-label.

Full HTML

Manual steps:

  • Open URL: enter image description here

    CodePudding user response:

    To click() on star rating 4 stars as the the desired element is within a <iframe> so you have to:

    • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
    • Induce WebDriverWait for the desired elementToBeClickable.
    • You can use either of the following Locator Strategies:
      • Using cssSelector:

        new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.goog-reviews-write-widget")));
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[aria-label='Four stars']"))).click();
        
      • Using xpath:

        new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='goog-reviews-write-widget']")));
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@aria-label='Four stars']"))).click();
        

    Reference

    You can find a couple of relevant discussions in:

  • Related