我准备在centOS7上使用selenium控制已打开的chrome页面。
目前我在mac上测试一切正常,但是将代码上传到centOS7上就会有问题,报错:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:8412
from unknown error: unable to discover open pages
我按照网上的文章确认了驱动与chrome的版本,都是正确的。
使用curl http://127.0.0.1:8412/json确认是有返回值的,但是代码中运行到browser = webdriver.Chrome(options=chromeOptions, executable_path="chromedriver")时还是会卡住,然后过大概分钟就报上面的错误。
下面是代码截图:
引用 皆我百晓生 小程序回复内容作答:
这个问题可能是由于SELinux的限制引起的。您可以尝试使用以下步骤来解决:
检查SELinux状态:
sestatus
如果SELinux状态为Enforcing,那么SELinux可能是导致问题的原因。
临时禁用SELinux:
sudo setenforce 0
这将临时禁用SELinux。然后重新尝试您的代码,看是否仍然报错。
永久禁用SELinux:
编辑/etc/selinux/config
文件:
sudo vi /etc/selinux/config
将SELINUX=enforcing
改为SELINUX=disabled
,保存并退出。
重新启动CentOS7系统以使更改生效。
请注意,禁用SELinux可能会降低系统的安全性,请根据自己的需求决定是否采取此步骤。
1、yum安装chrome浏览器
配置yum源
在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
cd /ect/yum.repos.d/
vim google-chrome.repo
写入如下内容:
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
安装google chrome浏览器:
yum -y install google-chrome-stable
Google官方源可能在中国无法使用,导致安装失败或者在国内无法更新,可以添加以下参数来安装:
yum -y install google-chrome-stable --nogpgcheck
找到chrome路径,并做个软连接,方便使用:
which google-chrome-stable
ln -s xxx /bin/chrome
查看chrome版本:chrome -version
2、或者安装chromium:
注: Chromium 是 Google 的Chrome浏览器背后的引擎,其目的是为了创建一个安全、稳定和快速的通用浏览器。 Chromium是Google为发展自家的浏览器Google Chrome(以下简称Chrome)而开启的计划,所以Chromium相当于Chrome的工程版或称实验版(尽管Chrome自身也有β版阶段),新功能会率先在Chromium上实现,待验证后才会应用在Chrome上,故Chrome的功能会相对落后、稳定。Chromium的更新速度很快,每隔数小时即有新的开发版本发布,而且可以免安装,下载zip封装版后解压缩即可使用(Windows下也有安装版)。Chrome虽然理论上也可以免安装,但Google仅提供安装版。
安装chromium
yum -y install epel-release
yum -y install chromium
chromium-browser --version #查看版本
3、安装chromedriver
在http://chromedriver.storage.googleapis.com/index.html根据你安装的chrome版本查找对应的chromedriver的版本
cd /usr/local/src/ # 进入到软件要下载的目录
wget -c http://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
4、 写python脚本测试下(脚本文件名testselenium.py,内容如下)
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome( \
chrome_options=chrome_options, \
service_args=['--verbose', '--log-path=chromedriver.log'])
driver.get("https://www.baidu.com/")
time.sleep(1)
print(driver.current_url)
cookie = driver.get_cookies()
print(cookie)
driver.quit()
【以下回答由 GPT 生成】
对于这个问题,可以尝试以下几个解决方案。
确认Chrome浏览器版本与ChromeDriver版本匹配。在CentOS上,打开Chrome浏览器并输入chrome://version/
,查看"ChromeDriver版本"和"浏览器版本"是否匹配。确保下载的ChromeDriver与浏览器版本一致。
确认CentOS上是否安装了Chrome浏览器。在CentOS上执行google-chrome-stable
命令,如果命令不存在,则需要先安装Chrome浏览器。
使用ChromeOptions配置合适的启动参数。在创建WebDriver实例时,可以使用ChromeOptions来配置启动参数。尝试添加如下参数:
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-dev-shm-usage')
sudo firewall-cmd --state
如果防火墙已启用,可以尝试禁用防火墙或添加相应的规则允许连接到Chrome。
如果以上解决方案都不起作用,则可能需要进行更详细的排查,包括检查系统日志、查看ChromeDriver日志等。如果还有其他问题,请提供更多细节,以便进一步帮助解决。
1、你代码连接的是 1024 端口,报错又是报连接8412端口,你看看代码哪些地方是要连8412端口的
2、linux系统最好使用无界面形式去运行你的自动化脚本
chrome_options = Options()
if platform.system().lower() == 'linux':
# 如果是linux系统运行,就采用无界面的方式执行自动化脚本
# 使用headless无界面浏览器模式
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless') # //增加无界面选项 适用于linux系统执行
chrome_options.add_argument('--disable-gpu') # //如果不加这个选项,有时定位会出现问题
3、你这是Selenium的调试模式吧,按理来说是要提前打开一个浏览器,并指定端口,以供客户端连接的
用命令行执行一下这句 ,再执行脚本 (./chrome --remote-debugging-port=1024)
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
这个问题可能是由于在远程服务器上运行ChromeDriver时,Chrome浏览器没有以非默认端口启动。你可以尝试以下方法:
1、 确保你的Chrome浏览器已经以非默认端口启动。你可以在Chrome浏览器中输入chrome://version
查看浏览器的详细信息,找到"Google Chrome"部分下的"Launcher",查看"Host"和"Port"字段。
2、 在你的Python代码中,使用options.add_argument()
方法添加一个参数,指定Chrome浏览器的端口。例如,如果你的Chrome浏览器在端口9222
上启动,你可以这样修改你的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--remote-debugging-port=9222") # 添加这一行,指定端口
browser = webdriver.Chrome(options=chrome_options, executable_path="chromedriver")
3、 如果问题仍然存在,你可以尝试使用其他端口,直到找到一个可用的端口。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
CentOS 7通常启用了SELinux,这可能会影响Selenium的运行。您可以尝试禁用SELinux或者配置SELinux策略来允许Selenium运行。
sudo setenforce 0
参考结合GPT4.0、文心一言,如有帮助,恭请采纳。
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:8412
from unknown error: unable to discover open pages
selenium.com.mon.exceptions.WebDriverException:消息:未知错误:无法连接到127.0.0.1:8412的chrome
来自未知错误:无法发现打开的页面
根据报错提供下面几个思路:
1、如果你将ChromeDriver放在/usr/bin/chromedriver,那么你需要这样设置:
from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver')
2、检查并确保这些规则允许Selenium连接到Chrome。
尝试使用不同的Selenium版本:有可能是由于Selenium的版本问题导致的这个问题。你可以尝试更新或降级你的Selenium版本,看是否可以解决问题。
3、使用xvfb:如果你在没有图形界面的服务器上运行Selenium,你可能需要使用xvfb(X Virtual Framebuffer)来模拟一个图形界面。你可以查看xvfb的官方文档来了解如何安装和使用。
安装的版本?
你executable_path参数值不完整,把chromedriver文件后缀名带上,另外你检查一下你代码目录下有没有chromedriver
结合GPT给出回答如下请题主参考
这个问题可能是由于CentOS上缺少Chrome浏览器或者ChromeDriver驱动,或者是ChromeDriver版本不兼容,导致无法连接到Chrome浏览器。
以下是一些解决方案:
确保CentOS上安装了Chrome浏览器和ChromeDriver驱动,并且版本兼容。可以在命令行输入以下命令来安装:
Chrome浏览器:yum install -y google-chrome-stable
ChromeDriver驱动:下载对应版本的ChromeDriver,并将其解压到某个目录中,然后将目录添加到系统的PATH环境变量中。
如果已经安装了Chrome浏览器和ChromeDriver驱动,还是无法连接到浏览器,可以尝试指定ChromeDriver的路径,例如:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=chrome_options)
如果仍然无法解决问题,可以尝试升级Selenium和ChromeDriver的版本,或者使用其他浏览器和对应的驱动程序。
希望以上解决方案对你有所帮助。
已解决Message: unknown error: cannot connect to chrome at 127.0.0.1:9222