user267298
user267298

Reputation: 139

Obtaining XPATH for Selenium

In a previous question a user provides the following solution to the problem.

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='title 
login_title' and text()='Login']//following::div[1]//input[@class='text header_login_text_box 
ignore_interaction']"))).send_keys("[email protected]")

However, when I go into my chrome inspect element, I get the following XPATH by going copy>XPATH, which when added like the following, no longer works. It also doesn't give an error, just no email is typed into the box.

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='__w2_wHsckeIm21_email']"))).send_keys("[email protected]")

What's the difference between the two? Why does only the first one work and how do I obtain this long working version of xpath.

Upvotes: 0

Views: 87

Answers (3)

Rajat Patil
Rajat Patil

Reputation: 31

Well, not a concrete solution as such but do try out ChroPath Plugin. Also available on Chrome Web Store. https://autonomiq.io/chropath/

Upvotes: 1

supputuri
supputuri

Reputation: 14135

First of all you don't need such a long xpath to locate the email element. Simply you can use

//form[@class='inline_login_form']//input[@name='email']

And I don't recommend using id to identify in this case as it's dynamic (meaning the id will change each time you navigate to this page). So it's not a good idea to use the id to locate the element.

There are multiple ways to write locator for this element like

  1. //form[@class='inline_login_form']//input[@name='email']
  2. //input[@name='email'][@class='text header_login_text_box ignore_interaction']
  3. //input[@name='email'][starts-with(@class,'text header_login_text_box')]

I don't want to keep on giving all the possible options. The idea I chosen the //form[@class='inline_login_form']//input[@name='email'] is, it's clear that I am locating the input element with name email under form. If you try to locate the element with only //input[@name='email'], then there are 2 elements and Selenium will pick the first element (which we don't want this case) and it's not intractable.

If you want to learn more about xpath and how to develop the correct xpath for your target element refer to this post

Upvotes: 0

Knut Forkalsrud
Knut Forkalsrud

Reputation: 1174

I suspect the id is not a stable selector for Quora. When I try to repeat your steps today I find the XPath is slightly different, because the ID of the input field is different.

Today: //*[@id="__w2_wtEXFdHr21_email"]
In your example: //*[@id='__w2_wHsckeIm21_email']

XPath is loosely speaking a description of how you navigate the DOM to get to the element(s) of interest. There are many ways to get to a particular element. Chrome's dev tools will give you one way (or two if you count "Copy full XPath").

The question you linked has several answers that suggest different XPath expressions, and also CSS selectors. The ones looking for an input with name = email will find more than one element, where the input you're looking for is not the first.

Upvotes: 0

Related Questions