1

I tried using Explicit Wait with different conditions but none works. In addition, I tried with JavascriptExecutor but here also the element was not clicked. In the case of the code below, the element is not clicked even though the .click() command is used

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#gwt-uid-6")));
    driver.findElement(By.cssSelector("#gwt-uid-6")).click();

And in the case of the one below with the use of Thread.sleep the .click() command is executed correctly

    Thread.sleep(8000);
    driver.findElement(By.cssSelector("#gwt-uid-6")).click();

HTML Code

<li class="v-action" id="gwt-uid-69" aria-labelledby="gwt-uid-68" tabindex="0" style=""><span class="v-icon icon-add-item"></span><span class="v-text" id="gwt-uid-68" for="gwt-uid-69">Add page</span></li>

What is causing this and what other way than Thread.sleep can this problem be solved?

Ar2r
  • 55
  • 5
  • ImplicitWait also does not work, because despite the command driver.findElement(By.cssSelector("#loaderBtn")).click(); the button is still not pressed and instead the code that is in the lower lines is run – Ar2r Jul 12 '23 at 13:06

1 Answers1

1

Given the HTML:

<li class="v-action" id="gwt-uid-69" aria-labelledby="gwt-uid-68" tabindex="0" style="">
    <span class="v-icon icon-add-item"></span>
    <span class="v-text" id="gwt-uid-68" for="gwt-uid-69">Add page</span>
</li>

The id attribute values like gwt-uid-69, gwt-uid-68 are dynamically generated and is bound to change sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.


Solution

As the element is a GWT enabled element so 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("li.v-action[id^='gwt-uid'][aria-labelledby^='gwt-uid'] span.v-text[id^='gwt-uid'][for^='gwt-uid']"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='v-action'][starts-with(@id, 'gwt-uid') and starts-with(@aria-labelledby, 'gwt-uid')]//span[@class='v-text' and text()='Add page']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352