Friday, June 12, 2020

Web Scraping Using Selenium - Explicit Wait For Clickable Element

Selenium Wait - Explicit Wait For Clickable Element

Waiting in selenium can be done in different ways. In this tutorial, we will use the explicit wait functionality for waiting if the element is clickable.
But before that, please make sure you have read the first blog on this series to do the prerequisites.

Selenium Explicit Wait For Clickable

  1. Create a file seleniumwaitclickable.py and paste the following codes
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    from datetime import datetime
    
    The codes above imports the required library that we will use.
  2. Add this line
    driver = webdriver.Firefox(executable_path="geckodriver.exe")
    
    The code above will create a webdriver instance for Firefox.
  3. Add this line
    driver.get("https://slackingslacker.github.io/seleniumindex#/seleniumwait")
    
    The line will got to the website (https://slackingslacker.github.io/seleniumindex#/seleniumwait).
  4. Add this function as is
    def wait_for_element_clickable(wait_time: int, id: str):
        try:
            print("[{}] Waiting clickable".format(str(datetime.now())))
            WebDriverWait(driver, wait_time).until(
                EC.element_to_be_clickable((By.ID, id))
            )
            print("[{}] Element clickable".format(str(datetime.now())))
        except TimeoutException as e:
            print("[{}] Element not clickable".format(str(datetime.now())))
    
    This method will wait for the element to be clickable. It will print a message if the element did loaded or not.
  5. Add this line
    wait_for_element_clickable(3, "clickableBtn")
    
    This line will call the method we created and will wait within 3 seconds until it displays Element clickable.
  6. Add this line
    wait_for_element_clickable(6, "unclickableBtn")
    
    Again will call the method we created and wait for 6 seconds until it gives a message Element not clickable.
  7. Add this line
    driver.close()
    
    The line will close the webdriver as well as the browser.
  8. Run the seleniumwaitclickable.py. It should do the following:
    • Open the firefox browser
    • Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
    • Call the Method 2 time which prints messages in the console
    • Closes the browser
 

Program Sample Output

[2020-06-12 21:17:02.989380] Waiting clickable
[2020-06-12 21:17:06.081625] Element not clickable
[2020-06-12 21:17:06.081625] Waiting clickable
[2020-06-12 21:17:12.254618] Element not clickable
Output explanations
  1. The code waits for an clickable element.
  2. The code found the a clickable button
  3. The code looks for another clickable element
  4. The code does not find the element within 6 seconds because the button is not clickable cause it is disabled
 

Final Selenium Code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from datetime import datetime

driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://slackingslacker.github.io/seleniumindex#/seleniumwait")

def wait_for_element_clickable(wait_time: int, id: str):
    try:
        print("[{}] Waiting clickable".format(str(datetime.now())))
        WebDriverWait(driver, wait_time).until(
            EC.element_to_be_clickable((By.ID, id))
        )
        print("[{}] Element clickable".format(str(datetime.now())))
    except TimeoutException as e:
        print("[{}] Element not clickable".format(str(datetime.now())))

wait_for_element_clickable(3, "clickableBtn")
wait_for_element_clickable(6, "unclickableBtn")
driver.close()

 

Conclusion

Waiting time in selenium can be used for waiting a clickable element.
 

No comments:

Post a Comment

Programming

Basic Web Scraping Using Python - A Beginner's Guide to using Requests and Selenium

Beginner Guide to Web Scraping Using Python For Requests and Selenium (Live Examples)   Web scraping is gathering da...