Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

Python Selenium unexpected search outside the element

When I try to find elements inside the current element, the search occurs in the whole web page

For example I get some element:

year_block = browser.find_element_by_xpath('some condition')

There are several similar elements of such kind but I make sure that I've got the proper element using:

print(year_block.get_attribute('innerHTML'))

And then I need to find list of elements inside "year_block" I use:

plant_id_block_list = year_block.find_elements_by_xpath("//*[contains(text(), 'Plant number')]/following-sibling::input")

But in result I get the list of all such elements on a page instead of such elements only inside year_block

Where is the problem?

question from:https://stackoverflow.com/questions/65856006/python-selenium-unexpected-search-outside-the-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As you are trying to find the elements with respect to year_block within the instead of using // you need to use .// which will initiate the search with respect to the context of year_block. Effectively your line of code will be:

plant_id_block_list = year_block.find_elements_by_xpath(".//*[contains(text(), 'Plant number')]/following-sibling::input")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...