0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Works fantastic, could you explain to me this line: form for form in forms if len(form.find_elements_by_class_name("reportwarning")) > 0 ?
Sure thing, I'm glad it works :) basically that is what's called a list comprehension. See how that statement is in brackets? That means that we are going to be building a list based on that statement. In this case we are saying, for every form in forms, add this form to the list forms_with_warning if it has at least one element inside it with the "warning" class.
This was super useful, many thanks.. what confused me was that we can use form..find_elements_by_class_name... to look in group just contained in that form, until now I did not realize that it can work that way, as I always used it against the driver...., basically the whole page
Yep web elements are pretty neat once you realize you can act on each of them individually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.