Friday, June 12, 2020

Web Scraping Using Selenium - Explicit Wait For Element Selection

Selenium Wait - Explicit Wait For Element Selection

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 to be selected.
But before that, please make sure you have read the first blog on this series to do the prerequisites.

Selenium Explicit Wait For Selection

  1. Create a file seleniumwaitelementselection.py and paste the following codes
    from selenium import webdriver
    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_selection(wait_time: int, element, is_selected: bool):
        try:
            print("[{}] Waiting element".format(str(datetime.now())))
            WebDriverWait(driver, wait_time).until(
                EC.element_selection_state_to_be(element, is_selected)
            )
            print("[{}] Element found".format(str(datetime.now())))
        except TimeoutException as e:
            print("[{}] Element not found".format(str(datetime.now())))
    
    This method will wait for the element to be selected. It will print a message if the element did loaded or not.
  5. Add these lines
    check_el = driver.find_element_by_css_selector("div#elementSelection input[name='forSelected']")
    uncheck_el = driver.find_element_by_css_selector("div#elementSelection input[name='notSelected']")
    
    This lines will find the checkbox that we will use.
  6. Add this line
    wait_for_element_selection(3, check_el, True)
    
    This line will call the method we created and will wait within 3 seconds until it displays Element found. True parameter means that the checkbox must be checked.
  7. Add this line
    wait_for_element_selection(6, check_el, False)
    
    This line will call the method we created and will wait within 6 seconds until it displays Element not found. False parameter means that the checkbox must NOT be checked.
  8. Add this line
    wait_for_element_selection(9, uncheck_el, True)
    
    This line will call the method we created and will wait within 9 seconds until it displays Element not found. True parameter means that the checkbox must NOT be checked.
  9. Add this line
    wait_for_element_selection(12, uncheck_el, False)
    
    This line will call the method we created and will wait within 12 seconds until it displays Element found. False parameter means that the checkbox must be checked.
  10. Add this line
    driver.close()
    
    The line will close the webdriver as well as the browser.
  11. Run the seleniumwaitelementselection.py. It should do the following:
    • Open the firefox browser
    • Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
    • Finds 2 checkboxes and put them in variables
    • Call the Method 4 time which prints messages in the console
    • Closes the browser
 

Program Sample Output

[2020-06-12 23:28:01.282843] Waiting element
[2020-06-12 23:28:01.336237] Element found
[2020-06-12 23:28:01.336237] Waiting element
[2020-06-12 23:28:07.710786] Element not found
[2020-06-12 23:28:07.710786] Waiting element
[2020-06-12 23:28:16.846971] Element not found
[2020-06-12 23:28:16.846971] Waiting element
[2020-06-12 23:28:16.846971] Element found
Output explanations
  1. The code waits for a checkbox element which must be checked.
  2. The code found a checkbox is checked
  3. The code waits for a checkbox element which must be NOT checked.
  4. The code did not find the checkbox within 6 seconds because it is checked.
  5. The code waits for a checkbox element which must be checked.
  6. The code did not find the checkbox within 6 seconds because it is NOT checked.
  7. The code waits for a checkbox element which must be NOT checked.
  8. The code found the checkbox that is NOT checked within 12 seconds
 

Final Selenium Code

from selenium import webdriver
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_selection(wait_time: int, element, is_selected: bool):
    try:
        print("[{}] Waiting element".format(str(datetime.now())))
        WebDriverWait(driver, wait_time).until(
            EC.element_selection_state_to_be(element, is_selected)
        )
        print("[{}] Element found".format(str(datetime.now())))
    except TimeoutException as e:
        print("[{}] Element not found".format(str(datetime.now())))

check_el = driver.find_element_by_css_selector("div#elementSelection input[name='forSelected']")
uncheck_el = driver.find_element_by_css_selector("div#elementSelection input[name='notSelected']")

wait_for_element_selection(3, check_el, True)
wait_for_element_selection(6, check_el, False)
wait_for_element_selection(9, uncheck_el, True)
wait_for_element_selection(12, uncheck_el, False)
driver.close()

 

Conclusion

Waiting time in selenium can be used for waiting a selected 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...