简体   繁体   中英

Using Selenium and Python, how to check whether the button is still clickable?

so I am doing some web scraping using Selenium with Python and I am having a problem. I am clicking a Next button to move to the next page on a certain website, but I need to stop clicking it when I reach the last page. Now, my idea of doing it would be just to use some_element.click() in a try/except statement and wait until it gives me an error while the button is not clickable anymore. It seems though, .click() doesn't emit any signal of any kind, it doesn't throw an error when the button can't be clicked and it doesn't emit any true or false signal.

a code snippet I tried using:

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

Is there any other way? Thanks and cheers.

Without knowing more about your example target, the minimal that can be said is that a clickable attribute would have an 'href' attribute.
You can use the get_attribute property of an element:

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 given attribute or property of the element.

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

Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.

More on Using get_attribute

You could also try is_displayed or is_enabled

使用它来获取类element.get_attribute(“class”)并检查类列表是否包含来自html框架的特征类(例如“disable”),用于描述不可点击的按钮

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM