Skip to main content

Posts

Showing posts from 2018

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling  UnexpectedAlertPresentException   Alerts who hates them? I Do!  Who doesn't hate an annoying alert causing your tests / scraping job to fail? I must say they are pretty much on point on the Unexpected part!  Fortunately, there are easy ways to mitigate the issue. 1. Disable alerts completely: driver . execute_script( 'window.alert = function(){};' ); execute this script just before where you anticipate the alert and you're golden. 2. You want to see the alert text but not disturb the execution flow. driver . execute_script( 'window.alert = console.info;' ); Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console) 3. You know exactly when it comes and want to accept the alert and move on. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.s

Selenium + Python: An alternate way of web scraping.

Selenium has always been the choice of web developers to test their applications before launch but it also can be used to collect data. Some of the sites on the Internet require a lot of manual intervention that most of traditional scraping methods will fail to reproduce. What to do then? - Use Selenium! Is it easy to use - Pretty muc h. Prerequisites: Installing Selenium: You can download Python bindings for Selenium from the  PyPI page for selenium package . However, a better approach would be to use  pip  to install the selenium package. Python 3.6 has pip available in the  standard library . Using  pip , you can install selenium like this pip install selenium For Windows (Since Linux already has working python) Install Python using the  MSI available in python.org download page . Start a command prompt using the  cmd.exe  program and run the  pip  command as given below to install  selenium . C: \Python36\Scripts\pip.exe install selenium If