Surendra Anand
Surendra Anand

Reputation: 67

Unable to locate an element using and condition in xpath

When use the condition in Console it says the condition is true but when I run the script, I'm getting the unable to find the element.

Below is the HTML:

<div class="ipc-button__text">Sign In</div>

And the XPath I'm trying to use:

(//div[@class='ipc-button__text']) and (//div[contains(text(),'Sign')])

Upvotes: 1

Views: 119

Answers (3)

Vikas Rathour
Vikas Rathour

Reputation: 261

There might be the reason that XPath of the element is not loaded when we trying to perform action instead of doing simple use explicit wait condition before performing any action try to use the below approach.

    // xpath = //*[contains(text(),"Sign In")]
        WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
        WebElement element 
         = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
    // Perform any action
       element.click();

Upvotes: 0

Justin Lambert
Justin Lambert

Reputation: 978

Please use following xpath ---- //*[contains(text(),"Sign In")]

Upvotes: 0

zx485
zx485

Reputation: 29022

Try combining the expressions.

//div[@class='ipc-button__text' and contains(text(),'Sign')]

This should fix the issue.

Otherwise each //div expression would be evaluated separately and result in the whole expression being true (because both //div[@class='ipc-button__text'] and //div[contains(text(),'Sign')] are true somewhere in the document), but without a specific div element selected matching both sub-expressions.

Upvotes: 2

Related Questions