Reputation: 602
Lets say I have this kind of html
<form method post="some.page">
.
.
<span class="warning"
<img class"something_img" src="some.jpg" title="jpg title">
</span>
.
.
<p class="input">
<input class="input" type="submit" value="Click Here" name="action">
</p>
.
.
</form>
<form method post="some.page">
.
.
<p class="input">
<input class="input" type="submit" value="Click Here" name="action">
</p>
.
.
</form>
The jest is, the page is made out of lots of forms, but I need to locate only submit buttons in forms where the class "warning" exist and skip the inputs where it doesn't.. there can be a lot! of this forms some of them have the "warning" class and some of them don't and I need to click on the first that does have it... (script will than do it stuff, and come back when done to the main page where it again have to look for the input of form where the warning is...it will be on next form as script will solve the issue with old warning )
I'm not sure how to locate that reliably with selenium and python.
Upvotes: 0
Views: 103
Reputation: 424
I would first filter out all the forms that don't have an element with the 'warning' class, and then get the submit button(s) in the valid forms via their XPaths:
# find all forms
forms = driver.find_elements_by_tag_name("form")
# create list of forms that only contain elements with 'warning' class
forms_with_warnings = [form for form in forms if len(form.find_elements_by_class_name("warning")) > 0]
# create list of buttons and fill it with found inputs in valid forms
buttons = []
for form in forms_with_warnings:
buttons.extend(form.find_elements_by_xpath('//input[type="submit"]'))
I hope something like this helps! You could also do the filtering via the filter function instead of a list comprehension; whatever you're more comfortable with.
Upvotes: 1