Home > OS >  how to enter data with send.keys to 2 fields with same xpath in selenium python
how to enter data with send.keys to 2 fields with same xpath in selenium python

Time:02-08

i'm trying to submit password field and a confirm password field. they both dont have ID and have same xpath, the only difference is that first field with label Enter password xpath have [1] and the second field with label Confirm password xpath have [2].

here is the first one:

/html/body/div[1]/div/div[2]/div/div[2]/div/div[1]/label/input

and here is the second one:

/html/body/div[1]/div/div[2]/div/div[2]/div/div[2]/label/input

Xpath helper chrome plugin also give the same.

From the first one:

/html/body/div[@class='app']/div[@class='layout']/div[@class='router- 
view']/div[@class='signup-form']/div[@class='onboarding password- 
form']/div/div[@class='password-form__input-pass'][1]/label[@class='cs- 
input']/input[@class='cs-textstyle-input-text cs-input__input cs-input__input-- 
medium']

and from the second one:

/html/body/div[@class='app']/div[@class='layout']/div[@class='router- 
view']/div[@class='signup-form']/div[@class='onboarding password- 
form']/div/div[@class='password-form__input-pass'][2]/label[@class='cs- 
input']/input[@class='cs-textstyle-input-text cs-input__input cs-input__input-- 
medium']

so for now i have this code which submit the first field (Enter password):

password = driver.find_element(By.XPATH, "//input[@class='cs-textstyle-input-text cs- 
input__input cs-input__input--medium']")

password.send_keys("ewra5RT#T6")

how can i do for the second field to confirm the password?

I suppose it should be something like:

password_confirm = driver.find_element(By.XPATH, "//input[@class='cs-textstyle-input- 
text cs-input__input cs-input__input--medium'][2]") 

but I can not figure out the right syntax...?

Please help and thanks.

HTML for both fields are also the same, here:

for the first one:

<input data-v-6484adb0="" type="password" required="required" 
>

and second:

<input data-v-6484adb0="" type="password" required="required" 
>

CodePudding user response:

maybe I'm missing something, but why you don't use the full xpaths you mentioned above? or xpath axis and those label elements you mentioned?

but if you still want to use your approach, then simply replace

[2]

with

position() = 2

in appropriate manner

  •  Tags:  
  • Related