Using Selenium and Python, how do I check if a button is still pressed?

so I am doing some scraping with Selenium with Python and I have a problem. I click the Next button to go to the next page on a specific website, but I need to stop clicking it when I get to the last page. Now my idea to do this would be to just use some_element.click () on the try / except statement and wait until it gives me an error while the button is no longer clicked. .Click () doesn't seem to emit any signal, it doesn't throw an error when the button cannot be pressed, and it doesn't emit any true or false signal.

snippet of code i tried to use:

while True:
    try:
       button = driver.find_element_by_class_name('Next_button')
       button.click()
    except:
       break

      

Is there another way? Thank you and welcome.

+3


source to share


2 answers


Without knowing more about your target example, the minimum that can be said is that the clickable attribute will have an "href" attribute.
You can use an get_attribute

element property :

button = driver.find_element_by_class_name('Next_button')
href_data = button.get_attribute('href')
if href_data is None:
  is_clickable = False

      

Gets the specified attribute or property on the element.

This method will first try to return the value of the property with the given name. If a property with that name does not exist, it returns the value of the attribute with the same name. If there is no attribute with this name, None is returned.

Values ​​that are considered to be true, that is, "true" or "false", are returned as booleans. All other values ​​other than None are returned as strings. None is returned for attributes or properties that do not exist.



More about using get_attribute

You can also try is_displayed

oris_enabled

+5


source


Use this to get the classes element.get_attribute ("class") and check if the class list contains an attribute class (like "disable") from the html framework that is used to describe invisible buttons



+4


source







All Articles