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>
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.
To search for nodes by their CSS selectors, use the shortcut: CMD / Ctrl + F.
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.)
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.
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
We can create a XPath something like the below:
//td[contains(text(), 'unique text')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With