J_Coder
J_Coder

Reputation: 737

Finding link using text in CSS Selector is not working

I am trying to locate below link by using a[text='This is a link'] and a[innertext='This is a link'] but both of them are not working.

I know this can be achieved by using XPath or other ways, but I am curious why CSS Selector that I have used is not working. refer this link.

<a title="seleniumframework" href="http://www.seleniumframework.com" target="_blank">This is a link</a>

Upvotes: 1

Views: 3002

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193088

You are trying to locate a link by using the following CssSelectors:

  • a[text='This is a link']
  • a[innertext='This is a link']

As per Issue#987 and Issue#1547:

The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).

You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

Solution

As per the HTML you can use either of the following solutions:

  • CssSelector using the attribute title:

    "a[title='seleniumframework']"
    
  • CssSelector using the attribute href:

    "a[href='http://www.seleniumframework.com']"
    
  • CssSelector using the attributes title and href:

    "a[title='seleniumframework'][href='http://www.seleniumframework.com']"
    

Upvotes: 1

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

The right CSS Selector for your case is:

a[title="seleniumframework"]

or

a[href="http://www.seleniumframework.com"]

You can also use this one: a[target="_blank"], but the ones above are more unique.

Hope it helps you!

Upvotes: 1

Shashank Bhatt
Shashank Bhatt

Reputation: 857

Since you are trying to use attribute selector, you can only select from available attributes that the hyperlink element currently has.

text is not available attribute in html so selecting it this way through css will not work.Rather you should try to use other attribute like a[title=seleniumframework] for example.

Upvotes: 0

Related Questions