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

10,541

Solution 1

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

Solution 2

Use this to get classes element.get_attribute("class") and check if list of classes contains characteristic class (for example "disable") from your html framework which is used to describe unclickable buttons

Share:
10,541

Related videos on Youtube

Ivan Bilan
Author by

Ivan Bilan

NLP & ML Big Data and ETL (PySpark, Hadoop) Microservices & Microfrontends Python, C++, C#, Perl, bash, Kotlin

Updated on June 04, 2022

Comments

  • Ivan Bilan
    Ivan Bilan almost 2 years

    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.

    • Saifur
      Saifur almost 9 years
      Is the element displayed when you reach the last page?
    • alecxe
      alecxe almost 9 years
      It could be that the link is still clickable, but instead of moving to the next page, it does nothing. Can you share the link to the website you are running the code against?
    • Vikas Ojha
      Vikas Ojha almost 9 years
      @Saifur is right. Check what html attributes change for that button when you reac the last page.
    • Ivan Bilan
      Ivan Bilan almost 9 years
      Saifur, the Next page button is still shown, but it is greyed out on the last page. @Vikas Neha Ojha, the class name changes at the last page and so does the xpath to it. How can I detect this change in Python? Or did I understand your advice wrongly. alecxe, yes, that could be true. The loop just goes on forever.
    • Vikas Ojha
      Vikas Ojha almost 9 years
      The loop just goes on forever - Please mention whether it is even navigating through the next webpages. You mentioned that class name changes and I can see that you are trying to find the element by class name only, so still your code should work. Could you please post the html of just the button on initial page and the last page?
    • Ivan Bilan
      Ivan Bilan almost 9 years
      I can see the browser going through each of the page and saving the content of each page until the last one. At the last page, the loop is never broken, the script just keeps running. The code for the next page button on the page before the last one: <li class="next"> <a href="/search/path...">Next</a> ::after </li> On last page: <li class="copy-light next"> "Next" ::after </li>
  • Ivan Bilan
    Ivan Bilan almost 9 years
    I am pretty sure this will solve my problem. I do not have the full code right next to me, but I will test it soon. Only one small thing, the class changes its name at the last page. I'll try to test the code soon. Thanks a lot.
  • Ivan Bilan
    Ivan Bilan almost 9 years
    Thank you, I will test this too. I did not know getAttribute() could be used in such a way.
  • Dan O'Boyle
    Dan O'Boyle almost 9 years
    If you know for certain that the class name changes on the "last page" you could also perform a check to see if elements with that given class name exist: stackoverflow.com/questions/7991522/…
  • Ivan Bilan
    Ivan Bilan almost 9 years
    Sorry it took me so long, I had to rewrite the code until that point (since I don't have the source code of my application now). I wanted to try your first suggestion, but I have a new problem. Whenever I use find_element_by_class_name I get an error: "Message: invalid selector: Compound class names not permitted". Basically, because the name of the class has a space in it, "copy-light next". Any quick suggestion? Thanks.
  • Dan O'Boyle
    Dan O'Boyle almost 9 years
    Try just "next" instead of "copy-light next", the partial string should catch. Otherwise try to catch the object a bit differently, see here: stackoverflow.com/questions/17808521/…
  • Ivan Bilan
    Ivan Bilan almost 9 years
    So, your first suggestion works, I had to use the css_selector (find_element_by_css_selector) to navigate to the button, since I have an error when looking for a class that has a space in its name. Thanks a lot. If you have any suggestion on how to fix that compound class error, I would be even more grateful.
  • Derorrist
    Derorrist over 8 years
    +1 for this answer. Selenium introduced expected conditions with element_to_be_clickable wait function, but for some unknown reason it deemed a button clickable even though it was disabled. Getting the class attribute and searching for 'disabled' solved it for me.
  • Selfcontrol7
    Selfcontrol7 over 3 years
    Still working, and a good answer to this day.