deep
deep

Reputation: 1

Get text of About link, on Google.com using CSS selector : Selenium Web driver

I am new to Selenium Webdriver. This might seem naive, but as a part of my learning I am trying to get the text of "About" link on google.com. I wanna do this only using cssSelector.hyperlink So far I have tried multiple variations of the following:

WebElement we= driver.findElement(By.cssSelector("#fbar>a[href='/intl/en/about.html']"));

Somehow every time, I hit an error, of not able to locate the element.

Can anyone solve this and help me explain how can we achieve this using cssSelector?

Thanks

Upvotes: 0

Views: 224

Answers (1)

Richard
Richard

Reputation: 9019

There's two issues with your CSS:

  1. You didn't put a space between #fbar>a, so it's looking for an a element that is directly a child of an element with id="fbar"
  2. The href has more text than what you're trying to match

This CSS selector should work for you:

#fbar a[href^='/intl/en/about.html']

This fixes 1. by telling the selector to look for an a element that is a child of an element with id="fbar" (and not necessarily a direct descendant) and 2. looking for a match for href which starts with the text you're looking for.

This is a solid reference for CSS selectors: http://www.w3schools.com/cssref/css_selectors.asp

Upvotes: 1

Related Questions