Selenium / Python - select using css selector

Problem:

Cannot select from css selector special element. You must verify that the registered user can successfully change their password. I tried different attributes of the class to call it. The result is an exception error in the method when trying with the first two examples. The last attempt calls an instance of the first class and resets the password fields (failure).

I tried:

driver.find_element_by_css_selector("value.Update").click() driver.find_element_by_css_selector("type.submit").click() driver.find_element_by_css_selector("input.test_button4").click() 

Goal:

I need to select elements that have one class. As you can see below, the class is generic.

 form id="changepw_form" name="changepw" action="#" method="post"> <div class="field3"> <div class="field3"> <div class="field3"> <input class="test_button4" type="reset" value="Reset" style"font-size:21px"=""> <input class="test_button4" type="submit" value="Update" style"font-size:21px"=""> 
+6
source share
2 answers
 driver.find_element_by_css_selector(".test_button4[value='Update']").click() 

EDIT: Since the selector needs a class , id or tagname , but value.Update alone is not one of them.

.test_button4 provides the class name for the match, and from there [value='Update'] indicates which specific matches to select.

+12
source
 test_button4 = driver.find_elements_by_class_name('test_button4') # notice its "find_elementS" with an S submit_element = [x for x in test_button4 if x.get_attribute('value') == 'Update'] #this would work if you had unlimited things with class_name == 'test_button4', as long as only ONE of them is value="Update" if len(submit_element): # using a non-empty list as truthiness print ("yay! found updated!") 

This is something that I almost never see anyone document, explain or use.

(a bad use name, for example, because its simplest)

find_element_by_name() returns a single element or find_element_by_name() an exception if it does not find it

find_elements_by_name() returns a list of elements. if no items are found, the list is empty.

so if you do find_elements_by_class_name() and it returns a list with X elements in it, all that is left at that point to narrow down what you need is some kind of old-fashioned understanding of the list (for example, as I used in your answer) or some indexing if you for some reason know which element you want.

also get_attribute() also seriously underused. it analyzes the inside of the html elements using what is before = , and returns what after =

+2
source

Source: https://habr.com/ru/post/953084/


All Articles