headless browser automation and etc.

I planned to continue researching Gauge framework today, but Fengwei introduced Phantomjs to me, so I did a bit research on it.

The first few things I learnt are:

  • the development of PhantomJs has been stopped.

  • however, PhantomJs has a large amount of stars in github, then it must be a good product ever.

By watching the video on youtube published 3 years ago, I learnt it had been widely used by various big companies for web automations. So I decided to give a try on it even though it had faded out for a reason. but after I finished setting up the environment, and write the following code:


def setUp(self):

self.dirver = webdriver.PhantomJS()

following error message pops up:

UserWarning: Selenium support for PhantomJS has been deprecated.

Then I googled it, and found nowadays, people switched to use headless chrome (with webdriver v59+), and here is how it works:

from seleniumimport webdriver
from selenium.webdriver.chrome.optionsimport Options

chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
url="https://www.xiami.com/"
browser.get(url)
browser.save_screenshot('./music_list.png')
print "finished"
browser.quit()

yeah! Now the code will take the screenshot without popping up chrome browser, and print "finished" before the test finishes.

Here are more about headless Chrome:
The easiest way to get started with headless mode is to open the Chrome binary from the command line. If you've got Chrome 59+ installed, start Chrome with the --headless flag:

chrome 
\--headless \                   # Runs Chrome in headless mode.
--disable-gpu \                # Temporarily needed if running on Windows.
--remote-debugging-port=9222 \
https://www.chromestatus.com   # URL to open. Defaults to about:blank.

Ps. To add chromdriver to the $PATH, here's another way:

sudo vi /etc/paths.d/web_driver

input driver directory to the file then save it.

你可能感兴趣的:(headless browser automation and etc.)