Selenium+python automation 87-Chrome browser silent mode startup (headless)

foreword

Selenium+phantomjs can open a browser without an interface, and start the browser in silent mode to complete automated testing. This mode is excellent and does not need to occupy the computer screen.

However, there are still many pits in phantomjs, and you can't see the page when you encounter problems, so you can't troubleshoot the problem.

In fact, the Chrome browser can also implement a silent mode, which does not display the page on the computer, and can also implement automated testing.

Xiaobian environment:
python 3.6
selenium 3.6.0
chrome 63.0.3239.84
chromdriver 2.33.506120

(My configuration is feasible for personal testing, if other versions are too low, it is estimated that it will not work)

headless

1. If the browser you don't want to watch is running when you start the browser, load the silent mode of the browser and let it run secretly in the background.

option = webdriver.ChromeOptions()
option.add_argument('headless')

2. Through comparison, it is found that after starting the silent mode, the speed of running the code to start the browser is faster.

Reference Code

1.chromedriver.exe needs to be added to the environment variable path, this is common sense, so I won't say more

# coding:utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument( ' headless ' )   #Silent mode #Open chrome browser driver = webdriver.Chrome( 
chrome_options 
= option)
driver.get("https://www.cnblogs.com/yoyoketang")
print(driver.title)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476466&siteId=291194637