Home > Net >  NoSuchElementException: no such element: Unable to locate element: {"method":"xpath&q
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath&q

Time:11-11

I'm having this strange error (sometimes works sometimes does not):

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="card-id-oidc-i"]/a"}

My button has this:

<a href="/auth/realms/Global-Realm/broker/id-oidc-i/login?client_id=web&amp;tab_id=Doz54nelUC0&amp;session_code=gwAePmGfpQ2hBLommJO7Rswc1gNkB90Ctc4">
                                <div style="width:100%;height: 40px;">
                                <span ></span>
                                </div>
                                <div  style="background-repeat: no-repeat;margin:auto; width:115px;height:120px"></div>
                                <div style="margin-top: 10px;min-width:170px">
                                    <h4 style="text-align:center;"><b>log in</b></h4> 
                                </div>
                                </a>

It's XPATH is:

//*[@id="card-id-oidc-i"]/a

I did this:

driver.findElement(By.xpath("//*[@id=\"card-id-oidc-i\"]/a")).click();

It is strange because sometimes works just fine but sometimes it fails.

Why?

CodePudding user response:

You probably missing a delay.
Try using this:

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='card-id-oidc-i']/a"))).click();

BTW your locator is based on some parent element with id = card-id-oidc-i while you shared here only the child a element HTML.
Also, no need to put \ before " inside a String. You can simply use ' instead as I do here.

CodePudding user response:

NoSuchElementException error may occur when :

  1. HTML element may not be present in a DOM yet. So you have to implement WebDriverWait to wait until element is present and visible in a web page.
  2. HTML element may not be inside frame or iframe.

Maybe in your case it is not in the DOM yet, try to wait until it is visible and on clickable position.

  • Related