Friday, June 12, 2020

Web Scraping Using Selenium - Explicit Wait For Alert

Selenium Wait - Explicit Wait For Alert

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

Selenium Explicit Wait For Alert

  1. Create a file seleniumwaitalert.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_alert(wait_time: int):
        try:
            print("[{}] Waiting for alert".format(str(datetime.now())))
            WebDriverWait(driver, wait_time).until(EC.alert_is_present())
            print("[{}] Alert found".format(str(datetime.now())))
        except TimeoutException as e:
            print("[{}] Alert did not load".format(str(datetime.now())))
    
    This method will wait for the a popup alert. It will print a message if the alert loaded or not.
  5. Add this line
    wait_for_alert(3)
    
    This line will call the method we created and will wait for 3 seconds until it displays Alert did not load.
  6. Add this line
    driver.find_element_by_css_selector("#alertExpectation button").click()
    
    This line will call the find a button and click it to trigger an alert.
  7. Add this line
    wait_for_alert(6)
    
    Again will call the method we created and wait for 6 seconds until it gives a message Alert found.
  8. Add this line
    driver.quit()
    
    The line will close the webdriver as well as the browser.
  9. Run the seleniumwaitalert.py. It should do the following:
    • Open the firefox browser
    • Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
    • Call the Method 1 time which prints messages in the console
    • Finds a button and clicks the button
    • Call the Method 1 time which prints messages in the console
    • Closes the browser
 

Program Sample Output

[2020-06-12 17:43:25.998479] Waiting for alert
[2020-06-12 17:43:29.005027] Alert did not load
[2020-06-12 17:43:29.089615] Waiting for alert
[2020-06-12 17:43:29.105222] Alert found
Output explanations
  1. The code waits for an alert.
  2. The code did not find any alert
  3. The code again waits for an alert
  4. The code finds the alert because a button was clicked that opens an alert popup
 

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_alert(wait_time: int):
    try:
        print("[{}] Waiting for alert".format(str(datetime.now())))
        WebDriverWait(driver, wait_time).until(EC.alert_is_present())
        print("[{}] Alert found".format(str(datetime.now())))
    except TimeoutException as e:
        print("[{}] Alert did not load".format(str(datetime.now())))

wait_for_alert(3)
driver.find_element_by_css_selector("#alertExpectation button").click()
wait_for_alert(6)
driver.quit()

 

Conclusion

Waiting time in selenium can be used for waiting an alert.
 

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...