Issue
I am working on a small automation project.
The website that I am working on, has a weird design. It has multiple elements with the same id. And the website works as expected(multiple same id's don't cause any problem on anything)
I need to reach all of these elements, but I can't, since their id's are all the same.
The only way I can reach all of them is to use "full xpath", but this solution won't be a long term solution, since only adding a single div to the website will cause the full xpath not work properly.
I want to reach the element with the outer element like:
elements = driver.find_elements_by_class_name("classs")
for element in elements:
element.find_element_by_id("idOfElement")
#OR
element.find_element_by_class_name("classOfTheElement"
THIS IS NOT A WORKING CODE, I just wanted to make you understand what I want. Is there a way to do this?
Edit: The website has a series of elements which is to upload variants of an object, and the id's in these variants are the same. I need to reach these id'ed elements and send some keys. Here is a similar html tree:
<div class="generalClass">
<div class="someElement">
<input id="idOfInput>
</div>
<div class="someElement">
<input id="idOfInput>
</div>
</div>
Solution
You do not have to use absolute XPaths, this can be clearly done with relative XPath.
Accordingly to your example you can do the following:
outer_element = driver.find_element_by_class_name("classs")
desired_elements = driver.find_elements_by_xpath(".//input[@id='idOfInput']")
or even more simply with a single relative XPath:
desired_elements = driver.find_elements_by_xpath('//div[@class="classs"]//input[@id="idOfInput"]')
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.