Dia*_*e12 3 python ubuntu selenium amazon-ec2 selenium-chromedriver
我正在尝试将 Chromedriver 与 Ubuntu(AWS 实例)结合使用。我已经让 Chromedriver 在本地实例中正常工作,但在远程实例中却遇到了很多很多问题。
我正在使用以下代码:
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=options)
Run Code Online (Sandbox Code Playgroud)
但是,我不断收到此错误:
Traceback (most recent call last):
File "test.py", line 39, in <module>
driver = webdriver.Chrome()
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: 'chromedriver'
Run Code Online (Sandbox Code Playgroud)
我相信我正在使用最新版本的 Selenium、Chrome 和 Chromedriver。
Chrome 版本是:Version 78.0.3904.70 (Official Build) (64-bit)
硒:
ubuntu@ip-172-31-31-200:/usr/bin$ pip3 show selenium
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: /home/ubuntu/.local/lib/python3.6/site-packages
Requires: urllib3
Run Code Online (Sandbox Code Playgroud)
最后,对于 Chromedriver,我几乎可以肯定我在这里下载了最新版本:https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/。这是 mac_64 版本(我在 Mac 上使用 Ubuntu)。然后我放入chromedriver
,/usr/bin
因为我读到这是常见的做法。
我不知道为什么这不起作用。我能想到的几个选择:
某种访问问题?我是命令行和 ubuntu 的初学者 - 我应该以“root”用户身份运行它吗?
Chromedriver 和 Chrome 版本不匹配?有没有办法确定chromedriver
我拥有哪个版本?
我看到 Chromedriver 和 Selenium 位于不同的位置。Selenium 位于:Location: /home/ubuntu/.local/lib/python3.6/site-packages
我已经转移chromedriver
到:/usr/bin
。这会引起问题吗?
Ubuntu 服务器 18.04 LTS(64 位 Arm):
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get -f install
google-chrome --version
wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip
sudo apt install unzip
解压 chromedriver,如果需要请安装 unzip :unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
chromedriver
sudo apt install default-jre
sudo pip3 install selenium
创建测试文件,nano test.py
内容如下。按 CTRL+X 退出,按 Y 保存。执行你的脚本 -python3 test.py
#!/usr/bin/python3
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('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
try:
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.google.com")
s = driver.find_element_by_name("q")
assert s.is_displayed() is True
print("ok")
except Exception as ex:
print(ex)
driver.quit()
Run Code Online (Sandbox Code Playgroud)
使用 Docker 和 selenium/standalone-chrome-debug 的示例:
sudo docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-xenon
命令,不同的选项在这里 options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Remote(command_executor="http://your_instance_ip:4444/wd/hub",
desired_capabilities=options.to_capabilities())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10388 次 |
最近记录: |