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.

4
  • try to search for "Create account" like explained here: stackoverflow.com/questions/18655765/… Commented Nov 19, 2015 at 13:34
  • 1
    did you try './/span[@id="link-signup"]' ? Commented Nov 19, 2015 at 13:38
  • searching for the a tag containing "Create account" should work like this: //a[contains(text(),'Create account')] Commented Nov 19, 2015 at 13:39
  • Thank you @Sword your suggestion worked Commented Nov 19, 2015 at 13:56

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

try it with following-sibling:

driver.find_element_by_xpath("//span[@id='link-signup']/following-sibling::a").click()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.