在数据块中的Chrome驱动程序问题

huangapple go评论101阅读模式
英文:

Chrome driver issue in data bricks

问题

我在运行以下代码时遇到了Web驱动程序异常。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver"  # 用实际的Chrome二进制文件路径替换
driver = webdriver.Chrome(options=chrome_options)
print(driver.service.process.path)

错误信息如下:

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

以下是集群上安装的库:

在数据块中的Chrome驱动程序问题

我尝试了多种方法,手动下载了Chrome和驱动程序,但仍然没有成功。之后,我尝试了使用Firefox,但遇到了以下问题:

%sh 
wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz -O /tmp/geckodriver.tar.gz

%sh
tar -xvzf /tmp/geckodriver.tar.gz -C /tmp

%sh
ls /tmp/gec*

%sh 
/usr/bin/yes | sudo apt update --fix-missing > /dev/null 2>&1

%sh 
sudo apt-get --yes --force-yes install firefox > /dev/null 2>&1

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path='/tmp/geckodriver')

错误信息如下:

<command-1051731704975926>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Firefox(options=options, executable_path='/tmp/geckodriver')
WebDriverException: Message: Service /tmp/geckodriver unexpectedly exited. Status code was: 1
英文:

I am facing Web driver exception while running below code.
Code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

#driver = webdriver.Chrome()
#print(driver.service.executable_path)
chrome_options= Options()
chrome_options.add_argument(&quot;--headless&quot;)
chrome_options.binary_location = &quot;/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver&quot;  # Replace with the actual path to your Chrome binary
driver = webdriver.Chrome(options=chrome_options)
print(driver.service.process.path)

Error:

ebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn&#39;t exist)
  (The process started from chrome location /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

below are the libraries installed on cluster:
在数据块中的Chrome驱动程序问题

have tried multiple things and tried to download manually chrome and driver but still no luck. After that have tried to do using firefox but for that getting below issue. Please suggest how to achieve this problem.

Firefox code:

%sh 
wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz -O /tmp/geckodriver.tar.gz

%sh
tar -xvzf /tmp/geckodriver.tar.gz -C /tmp

%sh
ls /tmp/gec*

%sh 
/usr/bin/yes | sudo apt update --fix-missing &gt; /dev/null 2&gt;&amp;1

%sh 
sudo apt-get --yes --force-yes install firefox &gt; /dev/null 2&gt;&amp;1

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options =Options()
options.headless =True
driver=webdriver.Firefox(options=options, executable_path =&#39;/tmp/geckodriver&#39;)

Error :

&lt;command-1051731704975926&gt;:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver=webdriver.Firefox(options=options, executable_path =&#39;/tmp/geckodriver&#39;)
WebDriverException: Message: Service /tmp/geckodriver unexpectedly exited. Status code was: 1

答案1

得分: 0

你可以按照这个方法进行操作。

运行下面的代码来设置选项。

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
binary_path = "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver"

接下来,运行以下命令来安装稳定版本的 Chrome。

%sh
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb https://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable

在这里,你可以看到 Chrome 的版本是 114.0.5735,与你在集群中安装的 Chrome 驱动程序相同。

然后,获取浏览器对象。

browser = webdriver.Chrome(service=Service(binary_path), options=options)
print(browser.service.process.args)

希望这些信息对你有帮助。

英文:

you can follow this approach.

Run below code for setting up options.

from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument(&#39;--headless&#39;)
options.add_argument(&quot;--no-sandbox&quot;)
options.add_argument(&quot;--disable-dev-shm-usage&quot;)
options.add_argument(&quot;--disable-gpu&quot;)
binary_path = &quot;/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/chromedriver_binary/chromedriver&quot;

Next, run below for installing stable chrome version.

%sh
sudo  curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo  echo  &quot;deb https://dl.google.com/linux/chrome/deb/ stable main&quot; &gt;&gt; /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable

在数据块中的Chrome驱动程序问题

Here, you can see the chrome version is 114.0.5735, it is same with the chromebinary you installed in cluster.

在数据块中的Chrome驱动程序问题

Then, get the browser object.

browser = webdriver.Chrome(service=Service(binary_path), options=options)
print(browser.service.process.args)

在数据块中的Chrome驱动程序问题

huangapple
  • 本文由 发表于 2023年6月22日 17:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定