Selenium+PhantomJS+HeadlessChrome对比
Headless Chrome
它是指在headless模式下运行谷歌浏览器。本质就是不用谷歌运行谷歌!它将由Chromium和Blink渲染引擎提供的所有现代网页平台的特征都转化成了命令行。phantomjs无人维护的情况下(Slobodin在Google论坛上发帖表示,鉴于Chrome 59推出了特性,他认为没有理由再继续维护Phantom.js。),headless chrome确实为一个更好的选择!
Headless代码示例
1 2 3 4 5 6 7 8 9 |
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') driver = webdriver.Chrome(chrome_options=chrome_options) ... |
Phantomjs代码示例
1 2 3 4 5 6 7 8 9 10 |
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities driver = webdriver.Remote( command_executor='http://127.0.0.1:8910', desired_capabilities=DesiredCapabilities.PHANTOMJS) driver.get('http://127.0.0.1') driver.find_element_by_css_selector('a[title="hello"]').click() driver.quit() |
Headless和Phantomjs性能对比
图片来源:https://hackernoon.com/benchmark-headless-chrome-vs-phantomjs-e7f44c6956c
selenium如何打开新标签页
1 |
driver.execute_script('window.open("http://127.0.0.1", "_blank")') |