1

I am trying to click on "create account" for gmail using xpath.

<span id="link-signup">
<a href="https://accounts.google.com/SignUp?service=mail&amp;continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&amp;ltmpl=default">
  Create account
  </a>
  </span> 

My code in python:

elem=driver.find_element_by_xpath("//*[@span='link-signup']").click()

I'm not sure what the error is. I know how to click the link by id but I want to learn how to do it by the xpath for future purposes.

royalblue
  • 439
  • 2
  • 8
  • 18

3 Answers3

1

Try using :
the './/' selects a child node with the tag span and the square brackets are used to select it by attribute i.e id.

elem=driver.find_element_by_xpath('.//span[@id="link-signup"]').click()
Hypothetical Ninja
  • 3,920
  • 13
  • 49
  • 75
0

try it with following-sibling:

driver.find_element_by_xpath("//span[@id='link-signup']/following-sibling::a").click()
drkthng
  • 6,651
  • 7
  • 33
  • 53
0

Here are a couple of example XPaths to retrieve the 'Create account' link:

//Returns an a element with the exact text 'Create account'
//a[.='Create account']

//Returns an 'a' element that is the child of a span element with the id 'link-signup'
//span[@id='link-signup']/a

The XPath given in your question //*[@span='link-signup'] is not a valid XPath because the @span='link-signup' predicate is trying to find an element which has a span attribute equal to 'link-signup', whereas the HTML given in your question has a span element with an id attribute equal to 'link-signup'.

Additionally, the XPath given in your question does not attempt to return the a element that contains the link you want to click.

Tom Trumper
  • 472
  • 2
  • 8