Monday, May 25, 2020

Web Scraping Using Selenium - Explicit Wait

Selenium Wait - Explicit Wait

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

Selenium Explicit Wait

  1. Create a file seleniumexplicitwait.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 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")
    
    The line will got to the website (https://slackingslacker.github.io/seleniumindex).
  4. Add this function as is
    def wait_for_the_element(wait_time: int, el_id: str):
        try:
            print("[{}] Finding element {}".format(str(datetime.now()), el_id))
            WebDriverWait(driver, wait_time).until(
                EC.presence_of_element_located((By.ID, el_id))
            )
            print("[{}] Element found".format(str(datetime.now())))
        except Exception as e:
            print("[{}] Element did not load".format(str(datetime.now())))
    
    This method will wait for the element to load at a given waiting time. It will print a message if the element loaded or not.
  5. Add this line
    wait_for_the_element(3, "navMenuId")
    
    This line will call the method we created and will display Element found.
  6. Add this line
    wait_for_the_element(6, "noneExistentId")
    
    This line will call the method we created and wait for 6 seconds until it gives an error.
  7. Add this line
    wait_for_the_element(9, "anotherNoneExistentId")
    
    Again will call the method with a non existing element this time it is 9 seconds.
  8. Add this line
    driver.close()
    
    The line will close the webdriver as well as the browser.
  9. Run the seleniumexplicitwait.py. It should do the following:
    • Open the firefox browser
    • Browser goes to https://slackingslacker.github.io/seleniuminde
    • Call the Method 3 times which prints messages in the console
    • Closes the browser
 

Program Sample Output

[2020-05-31 23:55:38.889027] Finding element navMenuId
[2020-05-31 23:55:38.920301] Element found
[2020-05-31 23:55:38.920301] Finding element noneExistentId
[2020-05-31 23:55:44.931274] Element did not load
[2020-05-31 23:55:44.931274] Finding element anotherNoneExistentId
[2020-05-31 23:55:53.968452] Element did not load
Output explanations
  1. The code looks for an element with id navMenuId
  2. The code found the element
  3. The code looks for an element with id noneExistentId
  4. The code does not find the element within 6 seconds
  5. The code looks for an element with id anotherNoneExistentId
  6. The code does not find the element within 9 seconds
As you may have noticed that the waiting time varies to what you have supplied to the WebDriverWait class.
 

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 datetime import datetime

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

def wait_for_the_element(wait_time: int, el_id: str):
    try:
        print("[{}] Finding element {}".format(str(datetime.now()), el_id))
        WebDriverWait(driver, wait_time).until(
            EC.presence_of_element_located((By.ID, el_id))
        )
        print("[{}] Element found".format(str(datetime.now())))
    except Exception as e:
        print("[{}] Element did not load".format(str(datetime.now())))

wait_for_the_element(3, "navMenuId")
wait_for_the_element(6, "noneExistentId")
wait_for_the_element(9, "anotherNoneExistentId")
driver.close()
 

Conclusion

Waiting time in selenium can be set per 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...