Thursday, June 8, 2017

Selenium WebDriver - How to Identify Web Elements Using Selenium XPath (Text Method)

Sometimes, you will need to locate elements that do not contain any attributes, this issue will make it really difficult to locate those elements in the ordinary strategies.

To overcome this issue, we can use the XPath/CSS text methods that will help us to locate elements using a particular text value.

When using the text function, the locator query the element for its text content and if the element contains the relevant text, it will return the element.
תוצאת תמונה עבור ‪selenium webdriver‬‏

Code Examples

<a href="">Quality Assurance</a>

Locating elements using exact text value

On the following example, we will specify the full text of the element which will be used by the XPath locator.

WebElement = Firefox.FindElement(By.XPath("//a[.='Quality Assurance']"));

Locating elements using the text function

The text () will return the full string of the element, and the "Contains" filter will validates if the requested string is matched to the search query.


WebElement = Firefox.FindElement(By.XPath("//a[contains(text(),'Assurance')]"));
WebElement = Firefox.FindElement(By.XPath("//a[contains(text(),'Quality')]"));

1 comment:

  1. Is it alright to post a portion of this on my site essentially present a hyperlink on this page? web design 


My Presentations