Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to locate an element by value using css selector in webdriver?

For the following element, how to find it by the value of the td using css selector? In this case it's "unique text"

<td class="someclass" colspan="3">
   unique text
</td>
like image 252
Daniel Wu Avatar asked Jul 30 '14 10:07

Daniel Wu


People also ask

How do you find the element based on the CSS selector engine with WebDriver?

Using inner text helps identify and create CSS Selectors in Selenium WebDriver by utilizing a string pattern that the HTML Tag manifests on the web page. This mechanism is used most frequently to locate web elements on account of its simplified syntax.

How do I search for an element in CSS?

To search for nodes by their CSS selectors, use the shortcut: CMD / Ctrl + F.

How do you access the nth element using the CSS selector in Selenium?

You can use “tag:nth-of-type(n)”. It will select the nth tag element of the list. Syntax: . tree-branch>ul>li:nth-of-type(3) (Selects 3rd li element.)

Which is the best way to locate an element in Selenium?

ID locator in Selenium is the most preferred and fastest way to locate desired WebElements on the page. ID Selenium locators are unique for each element in the DOM. Since IDs are unique for each element on the page, it is considered the fastest and safest method to locate elements.


2 Answers

You could use something like this,

With CSS Selector,

 By.cssSelector("td[class='someclass'][value='unique text']");

For more information on using css selector, See here

like image 139
Vignesh Paramasivam Avatar answered Oct 03 '22 08:10

Vignesh Paramasivam


We can create a XPath something like the below:

//td[contains(text(), 'unique text')]
like image 29
Sitam Jana Avatar answered Oct 03 '22 07:10

Sitam Jana