Home > Mobile >  Receiving "org.openqa.selenium.NoSuchElementException" regardless of using Fluent or Expli
Receiving "org.openqa.selenium.NoSuchElementException" regardless of using Fluent or Expli

Time:12-30

I have tried using both Explicit and Fluent wait and could not make it work and still received NoSuchElementException. I want to mention that the element was found when using Thread.sleep and waits right after.

URL: https://vegas.netbet.com/help/ Hovering over "My Account" then clicking on FAQ > Then clicking on point 5, though I believe clicking on any of them will return NoSuchElementException.

This is the HTML

<a ng-click="helpService.toggleAnswer(qa.id)" role="button"  aria-expanded="false" data-toggle="dropdown">
                    <p >"Placeholder Text</p>
                    <div >
                        <span ></span>
                        <span ></span>
                    </div>
                </a>

Fluent Wait:

FluentWait<WebDriver> fWait = new FluentWait<>(getWebDriver())
            .withTimeout(Duration.ofSeconds(10))
            .pollingEvery(Duration.ofMillis(500))
            .ignoring(NoSuchElementException.class); // org.openqa.selenium

Explicit Wait:

WebDriverWait wait = (WebDriverWait) new WebDriverWait(getWebDriver(), Duration.ofSeconds(15))
                .ignoring(NoSuchElementException.class);

So far I have tried most of of the things from ExpectedConditions.

Also, the waits do not seem to ignore the NoSuchElementException as it's still displayed

CodePudding user response:

You can try the below code:

driver.get("https://vegas.netbet.com/help/my-account/faqs");
Thread.sleep(1000);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//p[@class='title grTextRed']")));

JavascriptExecutor js = (JavascriptExecutor) driver;
        
// to scroll the page to view the specific question
int question_num_to_click = 5;
if (question_num_to_click != 1) {
    WebElement scroll_to_ele = driver.findElement(By.xpath("(.//p[@class='title grTextRed'])["   (question_num_to_click - 1)   "]"));
    js.executeScript("arguments[0].scrollIntoView(true)", scroll_to_ele);
}
Thread.sleep(1000);
        
// clicking on the specific question
WebElement question_to_click = driver.findElement(By.xpath("(.//p[@class='title grTextRed'])["   question_num_to_click   "]"));
js.executeScript("arguments[0].click();", question_to_click);
  • Related