[英]Can't open web browser using selenium chromedriver on windows 10
I'm trying to open a web browser using Selenium Chromedriver on Windows 10 with python in jupyter notebooks through an ubuntu command prompt.我正在尝试通过 ubuntu 命令提示符在 Windows 10 上使用 Selenium Chromedriver 和 jupyter 笔记本中的 python 打开网络浏览器。 I've read stack overflow posts and tried to solve based on their answers, but I'm stuck in a loop where I keep receiving the same 3 errors.我已阅读堆栈溢出帖子并尝试根据他们的答案解决问题,但我陷入了一个循环,我一直收到相同的 3 个错误。
Here is what I have installed:这是我安装的:
OS - Windows 10, 1709, 64-bit Selenium - 3.8.1 Chromedriver - 2.45 Chrome - Version 71.0.3578.98 Python - 3.5.2操作系统 - Windows 10、1709、64 位 Selenium - 3.8.1 Chromedriver - 2.45 Chrome - 版本 71.0.3578.98 Python - 3.5.2
I tried various websites.我尝试了各种网站。 The goal is to eventually get to a social media login page, but i'm stuck at opening a new blank web browser.目标是最终进入社交媒体登录页面,但我一直在打开一个新的空白网络浏览器。
Here is my starting code:这是我的起始代码:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.imdb.com/")
WebDriverException: Message: 'chromedriver' executable may have wrong permissions. WebDriverException:消息:“chromedriver”可执行文件可能具有错误的权限。
Then I tried the following:然后我尝试了以下方法:
from selenium import webdriver
chromedriver = "C:/Users/xxxx/AppData/Local/lxss/home/xxxx/chromedriver.exe"
browser = webdriver.Chrome(chromedriver)
browser.get('https://www.imdb.com/')
WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. WebDriverException:消息:'chromedriver.exe' 可执行文件需要在 PATH 中。
Here are the steps I have taken:以下是我采取的步骤:
A.一种。
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx)
driver.get("https://www.imdb.com/")
B. B.
from selenium import webdriver
chromedriver = r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.imdb.com/")
WebDriverException: Message: 'C:\\Users\\xxxx\\AppData\\Local\\lxss\\home\\xxxx' executable may have wrong permissions. WebDriverException:消息:'C:\\Users\\xxxx\\AppData\\Local\\lxss\\home\\xxxx' 可执行文件可能具有错误的权限。
Then I did the following: - Went to file Properties, under General, took off Read-only (Windows permissions) - Went to file Properties, under Security and changed permissions to Full Control - In the C:\\Users\\xxxxx\\AppData\\Local\\lxss\\home\\xxxx file, I changed the permissions using chmod 777 -R in my command prompt.然后我做了以下操作: - 转到文件属性,在常规下,取消只读(Windows 权限) - 转到文件属性,在安全性下并将权限更改为完全控制 - 在 C:\\Users\\xxxxx\\AppData\\ Local\\lxss\\home\\xxxx 文件,我在命令提示符中使用 chmod 777 -R 更改了权限。 Then I tried the following code:然后我尝试了以下代码:
from selenium import webdriver
import os
chromedriver = r'C:\Users\xxxx\AppData\Local\lxss\home\xxxx\chromedriver.exe'
driver = webdriver.Chrome(os.path.join(os.getcwd(), 'chromedriver.exe'))
driver.get("https://www.imdb.com/")
WebDriverException: Message: Service /home/ariggs/chromedriver.exe unexpectedly exited. WebDriverException:消息:服务 /home/ariggs/chromedriver.exe 意外退出。 Status code was: 1状态代码是:1
I am stuck between these three error messages.我被困在这三个错误消息之间。 Does anyone have another suggestion for a beginner?有没有人对初学者有其他建议?
You can actually start Windows executables from a linux subsystem as it is described here https://docs.microsoft.com/en-us/windows/wsl/interop .您实际上可以从 linux 子系统启动 Windows 可执行文件,如此处所述https://docs.microsoft.com/en-us/windows/wsl/interop 。
But you have to keep in mind that Selenium and ChromeDriver communicate over a network connection.但您必须记住,Selenium 和 ChromeDriver 通过网络连接进行通信。 Actually chromedriver starts its own http server and Selenium sends requests and receives responses over http.实际上 chromedriver 启动自己的 http 服务器,Selenium 通过 http 发送请求和接收响应。 (see https://sqa.stackexchange.com/questions/28358/how-does-chromedriver-exe-work-on-a-core-and-fundamental-level ) (参见https://sqa.stackexchange.com/questions/28358/how-does-chromedriver-exe-work-on-a-core-and-fundamental-level )
According to Microsoft, WSL and Windows share the same IP address and network connections via localhost are supported.根据微软的说法,WSL 和 Windows 共享相同的 IP 地址,并且支持通过 localhost 的网络连接。 But in your case there seems to be a problem during the startup.但是在您的情况下,启动过程中似乎存在问题。
You can start a remote webdriver on windows with Python and connect to that.您可以使用 Python 在 Windows 上启动远程 webdriver 并连接到它。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import subprocess
subprocess.run(["d:\\develop\\remotewebdriver.cmd", ""])
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://www.google.in/')
driver.close()
You need a windows script remotewebdriver.cmd for the remote webdriver that is called from Python:您需要一个用于从 Python 调用的远程 webdriver 的 Windows 脚本 remotewebdriver.cmd:
SET JAVA_HOME=D:\develop\Java\jdk-11.0.2
d:
cd \develop
start D:\develop\Java\jdk-11.0.2\bin\java -Dwebdriver.chrome.driver=d:\develop\chromedriver.exe -jar selenium-server-standalone-3.141.59.jar
You have to adapt the path to your own environment.您必须根据自己的环境调整路径。 This setup works for me.这个设置对我有用。
You can do this way.你可以这样做。
Step 1: Download chrome driver from this link (download the specific version as chrome): http://chromedriver.chromium.org/downloads第 1 步:从此链接下载 chrome 驱动程序(将特定版本下载为 chrome): http : //chromedriver.chromium.org/downloads
Important : check your chrome version first.重要提示:首先检查您的 chrome 版本。 Go to help -> about Google Chrome, to see the version of your chrome.转到帮助 -> 关于 Google Chrome,查看您的 chrome 版本。
Step 2: After downloading extract and save the chromedriver
file in a specific folder like, C:\\selenium
go to environment variable and add a new path, C:\\selenium
第 2 步:下载chromedriver
文件后,将其解压并保存在特定文件夹中,例如C:\\selenium
转到环境变量并添加新路径C:\\selenium
Step 3 Double click on the chromedriver
application and then restart your command prompt.第 3 步双击chromedriver
应用程序,然后重新启动命令提示符。 (If you are using conda environment.) (如果您使用的是 conda 环境。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.