how to know if a button is enabled using selenium?

Viewed 62

I am trying to know whether a button is enabled or disabled so i can click on it. I have tried this code but it seems of no use:

button = browser.find_elements_by_id("join_button_input")[0].click()
            print(button)
            if button == None:
                k = 1
                while (k<=10):
                    time.sleep(10)
                    browser.refresh()
                    button
                    k+=1

Here is the html text that I am using:

<input id="join_button_input" type="button" value="Join session" class="btn btn-primary" onclick="M.mod_bigbluebuttonbn.rooms.join('http://live.webcentreindia.com/mod/bigbluebuttonbn/bbb_view.php?action=join&id=893&bn=890');" disabled="true">
2 Answers

Just add an answer refers to the comment with a bit explanation.

You can check the attribute value by .get_attribute('disabled') method.

And I think you should use .find_element* (without s) not .find_elements*, because find by id is unique:

disable_val = driver.find_element_by_id('join_button_input').get_attribute('disabled')
if disable_val == 'true':
    #perform
    ....
    ....

BeautifulSoup won't help here because the page uses javascript to render the text (selenium will help though). Or you can use re/json module to extract the data:

import re
import json
import requests

ExampleSite = "https://throwbin.io/liWq2S3"
html_doc = requests.get(ExampleSite).text

d = re.search(r'}\((.*?)\)\);', html_doc).group(1)
d = json.loads('[' + d + ']')

print('Title:', d[4])
print('-' * 80)
print(d[5])

Prints:

Title: Soup
--------------------------------------------------------------------------------
Apple
Orange
Cake
Pizza
Related