Home > Software design >  Every Element on the page has the same XPATH: Python Selenium
Every Element on the page has the same XPATH: Python Selenium

Time:10-20

I am writing automated test cases for an open source app called "Featurehub" on github. It has to be run through docker container. I am having the problem in obtaining xpaths for elements because all of them have a same path or css selector and selenium cannot distinguish between them. For example, I had to implement the login test case using an enter(return) statement to get into the password field:

def step_impl(context, email, password):
    context.driver.find_element(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]").send_keys(email)
    context.driver.find_element(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]").send_keys(Keys.ENTER)
    context.driver.find_element(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]").send_keys(password)
    context.driver.find_element(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]").send_keys(Keys.ENTER)
    time.sleep(3)

I have attached an image below in which you can see that there are no unique xpaths and nothing is in the source code either. Screenshot: Chrome Dev Tools I tried recording it through Selenium IDE and it also shows same css selector for all clicks. Screenshot: Selenium IDE

CodePudding user response:

You should use .find_elements instead of .find_element:

elements = context.driver.find_elements(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]")

And then iterate through the list of elements:

for element in elements:
  element.click()

Or just access element of the list by index:

elements[0].send_keys(email)
elements[1].send_keys(password)

CodePudding user response:

I am not able to check the website but try the below code:

You said all the XPaths are the same, in that case:

for email field, try:

(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[1]")

for password field, try:

(By.XPATH, "(//body[@id='app-container']/flt-glass-pane)[2]")

You don't need to pass Enter key.

  • Related