1

I cannot detect a button inside an article of below code:

<article id="ride-f6ba24ca-d847-44b7-987e-81db6e6dee47" class="DetailPage__container--1VLdd"><div class="DetailPage__highlights--1uyrQ"><section></section><form aria-label="Offer highlights" class="DetailPage__section--qtXxV"><button type="submit"><span>Accept offer</span></button></form></div></article>

I try :

driver.findElement(By.xpath("//*[text()='Details']"))
driver.findElement(By.xpath("//button[.//span[text()='Accept offer']]"))

with no luck

I cannot detect the element Accept offer with selenium in java

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Stefanidis
  • 61
  • 1
  • 6
  • In a frame? Loading dynamically (so you need to wait)? Those are the usual culprits... – orde Jun 10 '19 at 19:33

2 Answers2

1

The desired element is an dynamic element so to locate the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("article[class^='DetailPage__container--'][id^='ride-']>div[class^='DetailPage__highlights--'] button[type='submit']>span")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[starts-with(@class, 'DetailPage__container--') and starts-with(@id, 'ride-')]/div[starts-with(@class, 'DetailPage__highlights--')]//button[@type='submit']/span[text()='Accept offer']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Finally your method works with the below method

private void waitForElement(By by, long delay) {
    LocalDateTime end = LocalDateTime.now().plusSeconds(delay);
    while (LocalDateTime.now().compareTo(end) <= 0) {
        if (driver.findElements(by).size() > 0) {
            break;
        }
    }
}

i run method wait first and then i check if ther is a list with elements like this below

if (driver.findElements(By.xpath("//article[starts-with(@class, 'DetailPage__container--') and starts-with(@id, 'ride-')]/div[starts-with(@class, 'DetailPage__highlights--')]//button[@type='submit']/span[text()='Accept offer']")).size() > 0){//Here i call the element}

So if the element exists i can call it inside to if else there is no timeexception more

Stefanidis
  • 61
  • 1
  • 6