运行 Selenium with Headless Chrome Webdriver
Running Selenium with Headless Chrome Webdriver
所以我正在用 selenium 尝试一些东西,我真的希望它快点。
所以我的想法是运行使用无头 chrome 使我的脚本更快。
首先,这个假设是否正确,或者如果我 运行 我的脚本带有无头驱动程序是否无关紧要?
无论如何,我仍然想让它工作到 运行 无头,但我不能,我尝试了不同的东西,大多数人建议它会像 10 月更新中所说的那样工作
但是当我尝试这样做时,我得到了奇怪的控制台输出并且它似乎仍然不起作用。
感谢任何小费。
到运行 chrome-headless只需通过chrome_options.add_argument
添加--headless
,即:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
#chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works
driver = webdriver.Chrome(options=chrome_options)
start_url = "https://duckgo.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
# b'<!DOCTYPE html><html xmlns="http://www....
driver.quit()
So my thought is that running it with headless chrome would make my
script faster.
尝试使用 chrome 选项,如 --disable-extensions
或 --disable-gpu
并对其进行基准测试,但我认为不会有太大改进。
参考文献:headless-chrome
Note: As of today, when running chrome headless on Windows., you should include the --disable-gpu
flag
See crbug.com/737678
如果您使用的是 Linux 环境,您可能还需要添加 --no-sandbox
以及特定的 window 大小设置。如果您正确设置了用户容器,则 Windows 上不需要 --no-sandbox
标志。
仅在 Windows 上使用 --disable-gpu
。其他平台不再需要它。 --disable-gpu
标志是临时解决一些错误的方法。
//Headless chrome browser and configure
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("disable-gpu");
// chromeOptions.addArguments("window-size=1400,2100"); // Linux should be activate
driver = new ChromeDriver(chromeOptions);
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path="./chromedriver", options=chrome_options)
url = ""
driver.get(url)
sleep(5)
h1 = driver.find_element_by_xpath("//h1[@itemprop='name']").text
print(h1)
然后我 运行 在本地机器上编写脚本
➜ python script.py
Running Selenium with Headless Chrome Webdriver
它正在工作并且是无头的Chrome。
Todo(在无头服务器 Debian Linux 9.4 上测试):
这样做:
# install chrome
curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
apt-get -y update
apt-get -y install google-chrome-stable
# install chrome driver
wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/chromedriver
chown root:root /usr/bin/chromedriver
chmod +x /usr/bin/chromedriver
安装硒:
pip install selenium
和运行这个Python代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("no-sandbox")
options.add_argument("headless")
options.add_argument("start-maximized")
options.add_argument("window-size=1900,1080");
driver = webdriver.Chrome(chrome_options=options, executable_path="/usr/bin/chromedriver")
driver.get("https://www.example.com")
html = driver.page_source
print(html)
安装并运行容器化Chrome:
docker pull selenium/standalone-chrome
docker run --rm -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome
连接使用 webdriver.Remote
:
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.CHROME)
driver.set_window_size(1280, 1024)
driver.get('https://www.google.com')
安装 selenium 和网络驱动程序后。下面在 linux 集群上使用无头 Chrome 为我工作:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option("prefs",{"download.default_directory":"/databricks/driver"})
driver = webdriver.Chrome(chrome_options=options)
所以我正在用 selenium 尝试一些东西,我真的希望它快点。
所以我的想法是运行使用无头 chrome 使我的脚本更快。
首先,这个假设是否正确,或者如果我 运行 我的脚本带有无头驱动程序是否无关紧要?
无论如何,我仍然想让它工作到 运行 无头,但我不能,我尝试了不同的东西,大多数人建议它会像 10 月更新中所说的那样工作
但是当我尝试这样做时,我得到了奇怪的控制台输出并且它似乎仍然不起作用。
感谢任何小费。
到运行 chrome-headless只需通过chrome_options.add_argument
添加--headless
,即:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
#chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works
driver = webdriver.Chrome(options=chrome_options)
start_url = "https://duckgo.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
# b'<!DOCTYPE html><html xmlns="http://www....
driver.quit()
So my thought is that running it with headless chrome would make my script faster.
尝试使用 chrome 选项,如 --disable-extensions
或 --disable-gpu
并对其进行基准测试,但我认为不会有太大改进。
参考文献:headless-chrome
Note: As of today, when running chrome headless on Windows., you should include the
--disable-gpu
flag See crbug.com/737678
如果您使用的是 Linux 环境,您可能还需要添加 --no-sandbox
以及特定的 window 大小设置。如果您正确设置了用户容器,则 Windows 上不需要 --no-sandbox
标志。
仅在 Windows 上使用 --disable-gpu
。其他平台不再需要它。 --disable-gpu
标志是临时解决一些错误的方法。
//Headless chrome browser and configure
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("disable-gpu");
// chromeOptions.addArguments("window-size=1400,2100"); // Linux should be activate
driver = new ChromeDriver(chromeOptions);
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path="./chromedriver", options=chrome_options)
url = ""
driver.get(url)
sleep(5)
h1 = driver.find_element_by_xpath("//h1[@itemprop='name']").text
print(h1)
然后我 运行 在本地机器上编写脚本
➜ python script.py
Running Selenium with Headless Chrome Webdriver
它正在工作并且是无头的Chrome。
Todo(在无头服务器 Debian Linux 9.4 上测试):
这样做:
# install chrome curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list apt-get -y update apt-get -y install google-chrome-stable # install chrome driver wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip unzip chromedriver_linux64.zip mv chromedriver /usr/bin/chromedriver chown root:root /usr/bin/chromedriver chmod +x /usr/bin/chromedriver
安装硒:
pip install selenium
和运行这个Python代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("no-sandbox") options.add_argument("headless") options.add_argument("start-maximized") options.add_argument("window-size=1900,1080"); driver = webdriver.Chrome(chrome_options=options, executable_path="/usr/bin/chromedriver") driver.get("https://www.example.com") html = driver.page_source print(html)
安装并运行容器化Chrome:
docker pull selenium/standalone-chrome
docker run --rm -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome
连接使用 webdriver.Remote
:
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.CHROME)
driver.set_window_size(1280, 1024)
driver.get('https://www.google.com')
安装 selenium 和网络驱动程序后。下面在 linux 集群上使用无头 Chrome 为我工作:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option("prefs",{"download.default_directory":"/databricks/driver"})
driver = webdriver.Chrome(chrome_options=options)