Issue
I have a relatively complex problem with xpath on Python. Here is my HTML layout:
<label name=A>
<span name=B>
"Your Salary"
<div name=C>
<div name=D>
<input name=E>
<label name=A>
<span name=B>
"Years of Experience"
<div name=C>
<div name=D>
<input name=E>
Because of the DOM, I can only ever estimate the name of input E. However, there may be multiple input Es which require different entries. My attempt was to find the title of the input at span B and base the different response inputs on that as shown here:
form_number = driver.find_elements_by_xpath('//input[contains(@name, "E")]')
for link in form_number:
if driver.find_elements_by_xpath('//input[contains(@name, "E")]/parent::div/parent::div/preceding-sibling::label/span[1][text()[contains(., "Salary")]]'):
link.send_keys("55000")
else: link.send_keys("2")
The idea here being if the question has anything to do with a "salary," to input "55000." For any other question I'll settle for "2," in this case being "2 years of experience." This seems to work for the first entry, however it will apply the key (in this case 55000) to all inputs, rather than just the first. It seems like Python isn't cycling through the "if - else" loop for each element, rather taking the first condition and pasting it for every element of the list. This is the closest that I have gotten thus far.
Thank you all so much for helping me as I cut my teeth on this API!
(Edit) So after playing around a bit more... it seems like this might be a problem with the API itself. Even if I submit the default value "2" for each input, and then run the function, it will still replace both inputs to 55000. Might have to work-around this by manually iterating through the list number?
Solution
You can take another approach to solve the issue.
First you identify the parent tags
which is label and then use try..except
block to check if element there then set the inputbox
value else go to except block and set the other inputbox
value.
#Fisrt identify the labels tag
parentTags=driver.find_elements_by_xpath('//label[@name="A"]')
#Iterate the loop
for ptag in parentTags:
try:
#identify the tag text of the label, if found then set the input value as 55000
ptag.find_element_by_xpath('.//span[conains(.,"Salary")]')
ptag.find_element_by_xpath('.//following-sibling::div[1]//input').send_keys("55000")
except:
#Not found then set the input value as 2
ptag.find_element_by_xpath('.//following-sibling::div[1]//input').send_keys("2")
Answered By - KunduK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.