1

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>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
J_Coder
  • 707
  • 5
  • 14
  • 32

3 Answers3

1

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!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
1

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']"
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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.

Shashank Bhatt
  • 717
  • 1
  • 11
  • 28