So I use xpath locators and slowly converting to CSS.
I haven't found a way to do an exact match based on text.
For example converting //a[text()='Log Out']
.
I know you can do css=a:contains('Log Out')
but I want to match exactly to the text. Also I know I can do link=Log Out
but looking for a solution with CSS.
6 Answers 6
css=a[text='Log Out'] or a[innertext='Log Out']
Can you please try this one out?
Or if that doesn't work and you still don't want to use xpath because it's slow, you can always try: link=Log Out
. That's still better then xpath.
EDIT:
So i found a possible solution for you mate. If you are trying to find an exact String you could always use Regular expression like this:
css=div:contains("^ab$")
Just replace div with a and there you go. This will find ONLY AB in whatever text div it looks for. OFC if you have more then one links with text AB (which is a bad thing :P ) then it will find them all..
Try this and see if it helps. :)
-
1Neither of those work unfortunately. I know I can use link=Log Out but just seeing if there was a solution in CSS.Brian O'Neill– Brian O'Neill2011年05月10日 21:33:57 +00:00Commented May 10, 2011 at 21:33
-
Sorry to hear that :( Unfortunately I don't think CSS has match exact param. But I might be mistaken. Hope you find an answer soon mate. :) Cheers.Hannibal– Hannibal2011年05月10日 21:36:41 +00:00Commented May 10, 2011 at 21:36
-
1Sorry, I made a mistake... please find edited answer... hope THAT helps. :)Hannibal– Hannibal2011年05月10日 21:38:07 +00:00Commented May 10, 2011 at 21:38
-
Wow, that worked! I really didn't think it would. Nice job proving me wrong.Brian O'Neill– Brian O'Neill2011年05月11日 00:46:46 +00:00Commented May 11, 2011 at 0:46
-
12:contains() is not part of the current CSS3 specification so it will not work on all browsers, only ones that implemented it before it was pulled. (see w3.org/TR/css3-selectors)Ardesco– Ardesco2011年05月18日 12:48:13 +00:00Commented May 18, 2011 at 12:48
This is a nice place for a few CSS selectors.
http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/
Thought it might be useful for people following this thread.
-
nice explanation of differences, thanks for the linkPeter M. - stands for Monica– Peter M. - stands for Monica2014年10月28日 17:17:59 +00:00Commented Oct 28, 2014 at 17:17
-
2Its very error prone sharing a link. Content of that link could change.paul– paul2019年03月14日 11:16:59 +00:00Commented Mar 14, 2019 at 11:16
For those who are looking to do Selenium css text selections this script might be of some use
Trick is to select parent of element of one that you are looking for and then search for child that has the text.
public static IWebElement FindByText(this IWebDriver driver, string text)
{
var list = driver.FindElement(By.CssSelector("#RiskAddressList"));
var element = ((IJavaScriptExecutor)driver).ExecuteScript(string.Format(" var x = $(arguments[0]).find(\":contains('{0}')\"); return x;", text), list);
return ((System.Collections.ObjectModel.ReadOnlyCollection<IWebElement>)element)[0];
}
this will return first element if there is more than one since it's always one element in my case.
-
1This looks to be dependent on jQuery being available to execute the javascript?David– David2015年01月22日 23:25:26 +00:00Commented Jan 22, 2015 at 23:25
-
@David it absolutely is, but it should be possible to rewrite it in pure JavaScript.Matas Vaitkevicius– Matas Vaitkevicius2015年01月23日 07:19:02 +00:00Commented Jan 23, 2015 at 7:19
As per the following discussions:
- CSS selector :contains doesn't work with Selenium
- css pseudo-class :contains() no longer allows anchors
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Python based solution
To locate the element with text as Log Out you can use either of the following Locator Strategies:
Using
link_text
:element = driver.find_element(By.LINK_TEXT, "Log Out")
Using
xpath
:element = driver.find_element(By.XPATH, "//a[text()='Log Out']")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
Using
LINK_TEXT
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "Log Out")))
Using
XPATH
:element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[text()='Log Out']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Java based solution
To identify the element with text as Log Out you can use either of the following Locator Strategies:
linkText
:WebElement element = driver.findElement(By.linkText("Log Out"));
xpath
:WebElement element = driver.findElement(By.xpath("//a[text()='Log Out']"));
Ideally, to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated()
and you can use either of the following Locator Strategies:
linkText
:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Log Out")));
xpath
:WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Log Out']")));
References
You can find a couple of relevant detailed discussions in:
Angular, Protractor:
If you have any class or Id it would be easy like this:
element(by.cssContainingText('.anyClass', 'Log Out'));
-
It would be helpful if you added what language your example is in.Lee Jensen– Lee Jensen2021年05月04日 14:19:58 +00:00Commented May 4, 2021 at 14:19
-
@Lee, That's either JavaScript/TypeScript code. Protractor based example.Harisha K P– Harisha K P2021年05月10日 19:23:00 +00:00Commented May 10, 2021 at 19:23
Per https://www.selenium.dev/documentation/webdriver/elements/finders/#get-element, you should get all elements that might contain your text, and then run a for loop to search for which of them match your text.
For example, in Python one could do:
elements = driver.find_elements(By.CSS_SELECTOR, '.a') # try to narrow this down more if you can
for element in elements:
if element.text == 'Log Out':
break
a:contains('Log Out')
doesn't seem to be valid CSS?