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


HTML:
<a href="http://www.machtested.com/p/blog-page_11.html">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.

Code:

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



No comments:

Post a Comment

My Presentations