gadhet
gadhet

Reputation: 13

Selenium Webdriver: How to find element by locators

I'm using Selenium Webdriver and would like to find this element:

Below is HTML code-

<span ng-class="{'details-section': isSingle(b) && !isExist(b)}">1.00</span>

Tried this one below:

driver.findElement(By.xpath("//span[@ng-class='{'details-section': isSingle(b) && !isExist(b)}']")).getText();

Unfortunatelly the following error I'm receiving:

unknown error: Cannot set property 'name' of undefined

Can you help me with this, please?

Upvotes: 1

Views: 76

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

You need to escape the ' inside the XPATH

driver.findElement(By.xpath("//span[@ng-class='{\'details-section\': isSingle(b) && !isExist(b)}']")).getText();

Also it is not a very good idea to to a full compare, so if possible do a partial one. Because even a small space change will break the code

driver.findElement(By.xpath("//span[contains(@ng-class,'isSingle(b)')][contains(@ng-class,'!isExist(b)')]

Upvotes: 1

Related Questions