text() is not a valid XPath expression

Viewed 312
<div ng-class="{'col-xs-4':sale.Sale.WarrantyExtensionFunctionAvailable ,'col-xs-6':!sale.Sale.WarrantyExtensionFunctionAvailable }" class="ng-binding col-xs-4">
    <b>Reference: </b>ABCD<br>
    <b>Serial number:</b> 12345678<br>
</div>

I want to locate the text ABCD, I tried this

driver.findElements(By.xpath("//div[@ng-class='{'col-xs-4':sale.Sale.WarrantyExtensionFunctionAvailable ,'col-xs-6':!sale.Sale.WarrantyExtensionFunctionAvailable }']/text()")).get(2);

but i get the following error :

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //div[@ng-class='{'col-xs-4':sale.Sale.WarrantyExtensionFunctionAvailable ,'col-xs-6':!sale.Sale.WarrantyExtensionFunctionAvailable }']/text() because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@ng-class='{'col-xs-4':sale.Sale.WarrantyExtensionFunctionAvailable ,'col-xs-6':!sale.Sale.WarrantyExtensionFunctionAvailable }']/text()' is not a valid XPath expression.

Is there any best practice to locate such type of text?

1 Answers

I think this should work here

driver.findElement(By.xpath("(//div[contains(@ng-class,'sale.Sale.WarrantyExtensionFunctionAvailable')]/text())[1]")).getText();
Related