Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

If an xpath corresponds to multiple elements on a page, how do you know the number of those elements #1401

Answered by mdmintz
chenqinggang001 asked this question in Q&A
Discussion options

I want to know the number of elements and click on them

You must be logged in to vote

Example with button:

Find the number of button elements on a page:

len(self.find_visible_elements("//button"))

To click all the buttons:

self.click_visible_elements("//button")

Replies: 3 comments 2 replies

Comment options

Example with button:

Find the number of button elements on a page:

len(self.find_visible_elements("//button"))

To click all the buttons:

self.click_visible_elements("//button")

You must be logged in to vote
0 replies
Answer selected by mdmintz
Comment options

Thanks

You must be logged in to vote
0 replies
Comment options

I need to click the buttons in order, The way I do it now is I get the length and I concatenate the elements in order, Is there a more elegant way to do it
@mdmintz
The way I use it now:

for i in range(1, len(self.find_visible_elements("//button"))):
 self.click(f'("//button")[{i}]', timeout=10, delay=1)
 self.sleep(10)

Because I do something after every click, this method is not for me
self.click_visible_elements("//button")

You must be logged in to vote
2 replies
Comment options

If you have to perform an action between the clicks, then your way should work. I recommend changing the sleep() to waiting for an actual element, as that would save time from unnecessary sleeping/waiting.

Comment options

ok,Thank you for reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /