**将bin目录下的exe文件复制到项目目录下**
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
path = 'phantomjs.exe'
browser = webdriver.PhantomJS(path)
url = "https://www.baidu.com"
browser.get(url)
input = browser.find_element(By.ID, "kw")
input.send_keys("海贼王")
button = browser.find_element(By.ID, 'su')
button.click()
time.sleep(2)
browser.save_screenshot("海贼王.png")
Chrome handless(推荐使用)
**Google针对浏览器59版新增的一种模式,可以在不打开ui界面的情况下使用Chrome浏览器**
**系统要求**
**chrome**
- Unix\Linux 系统需要chrome >= 59
- Windows系统需要chrome >= 60
**python3.6**
- selenium==3.4.*
- ChrmeDriver==2.31
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def share_browser():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
path = r'C:\Users\c\AppData\Local\Chromium\Application\chrome.exe'
chrome_options.binary_location = path
browser = webdriver.Chrome(chrome_options=chrome_options)
return browser
url = 'https://www.baidu.com'
browser = share_browser()
browser.get(url)
browser.save_screenshot("baidu.png")