There are three kinds of lazy.
Let’s make sure we’re in the last category.
The Selenium library is a workhorse for automating your web browser. I use it to automate data entry and integration testing, but you could do much more with it as long as the task involves your browser. Scrape the DOM? Yes. Click buttons? Yes. Fill out forms? Yes. Handle alerts? Yes. Switch windows? Yes. Want more? Go read the docs. We’re just going to scrape the surface.
-
From the Windows terminal
123456789$ pip install seleniumCollecting seleniumDownloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)100% |████████████████████████████████| 911kB 2.2MB/sCollecting urllib3 (from selenium)Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)100% |████████████████████████████████| 122kB 344kB/sInstalling collected packages: urllib3, seleniumSuccessfully installed selenium-3.141.0 urllib3-1.24.1 -
Download Chrome Driver
https://sites.google.com/a/chromium.org/chromedriver/downloads
Make sure to get the version that matches your chrome version.
What version of chrome do I have?
Extract the zip and take note of the destination folder. You’ll need the .exe path in the next step. -
Open Python and import selenium
123456789101112131415$ python> from selenium import webdriver# open the browser and go to a page> driver = webdriver.Chrome("C:\\Your\\Path\\Here\\chromedriver_win32\\chromedriver.exe")> driver.get("http://www.caseycodes.com/blog")# get the search box and send it a value> input = driver.find_element_by_xpath("//input[@type='text']")> input.send_keys("essential git")# get the submit button and click it> submit = driver.find_element_by_id("searchsubmit")> submit.click()# get the title of the results and print them> results = driver.find_elements_by_class_name("entry-title")> result0 = results[0].text> print result0
-
Selenium + Unittest
What is the answer to life, the universe and everything?
12345678910111213141516171819202122232425262728293031323334import unittestimport timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysclass TestManager(unittest.TestCase):driver = webdriver.Chrome('C:\Your\Path\Here\chromedriver_win32\chromedriver.exe')def setUp(self):self.driver.get("http://www.Google.com")def tearDown(self):self.driver.close()class TestTheAnswerToLife(TestManager):def runTest(self):# Arrangeexpected_answer = "42"# Actsearch_box = TestManager.driver.find_element_by_xpath("//input[@title='Search']")search_box.send_keys("What is the answer to life the universe and everything?")search_box.send_keys(Keys.ENTER)calculator = TestManager.driver.find_element_by_xpath("//h2[text()='Calculator Result']/following-sibling::div[1]")all_txt = calculator.textlines = all_txt.split('\n')Google_said = lines[1]time.sleep(10)# Assertself.assertEqual(expected_answer, Google_said)if __name__ == '__main__':unittest.main() -
Run TestDemo.py from the CLI
12345678C:\Users\Casey>python TestDemo.py -vrunTest (__main__.TestTheAnswerToLife) ... ok----------------------------------------------------------------------Ran 1 test in 15.080sOK