英文:
I can't get Selenium code to open a tab in an existing browser
问题
我正在尝试让Selenium在Chrome中作为现有浏览器运行,我的代码非常简单,我只是不知道为什么它不起作用。 (PS,我使用Mac)
这是我尝试遵循的YouTube视频链接:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64', chrome_options=opt)
url = 'https://monkeytype.com/'
driver.get(url)
print("test")
然后我在终端中输入这个命令,再运行这段代码:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=8989
终端会打开一个Chrome标签,但当我点击运行时什么也不会发生,它不会打印出"test",而是出现了一个很长的错误信息。DeprecationWarning: executable_path已被弃用,请传递一个Service对象;
它指出代码有问题,我尝试使用不同的executable_path
,但还是不起作用,我真的不知道问题出在哪里。
英文:
I am trying to get Selenium to work in Chrome as an existing browser and my code is pretty simple I just do not know why it won't work. (PS I use a Mac)
https://www.youtube.com/watch?v=Zrx8FSEo9lk&t=161s&ab_channel=Mukeshotwani
here is the link to the YouTube video I was trying to follow
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.add_experimental_option("debuggerAddress","localhost:8989")
driver = webdriver.Chrome(executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64',chrome_options=opt)
url = 'https://monkeytype.com/'
driver.get(url)
print("test")
so they before I run this code I type this into terminal
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=8989
and terminal opens up a chrome tab, but then when I click run nothing happens, it doesn't print out "test"
instead it gives this really big error
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64',chrome_options=opt)
/Users/adeelsyed/Desktop/Summer Grind Projects/UpdatedMonkType.py:9: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64',chrome_options=opt)
Traceback (most recent call last):
File "/Users/adeelsyed/Desktop/Summer Grind Projects/UpdatedMonkType.py", line 9, in <module>
driver = webdriver.Chrome(executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64',chrome_options=opt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 84, in __init__
super().__init__(
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
super().__init__(
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at localhost:8989
from chrome not reachable
Stacktrace:
0 chromedriver 0x0000000104da78ac chromedriver + 4257964
1 chromedriver 0x0000000104d9ff40 chromedriver + 4226880
2 chromedriver 0x00000001049dc854 chromedriver + 280660
3 chromedriver 0x00000001049cb004 chromedriver + 208900
4 chromedriver 0x0000000104a08550 chromedriver + 460112
5 chromedriver 0x0000000104a017bc chromedriver + 432060
6 chromedriver 0x0000000104a3f3dc chromedriver + 685020
7 chromedriver 0x0000000104a3ec08 chromedriver + 683016
8 chromedriver 0x0000000104a0b98c chromedriver + 473484
9 chromedriver 0x0000000104a0c98c chromedriver + 477580
10 chromedriver 0x0000000104d66900 chromedriver + 3991808
11 chromedriver 0x0000000104d6a354 chromedriver + 4006740
12 chromedriver 0x0000000104d6a940 chromedriver + 4008256
13 chromedriver 0x0000000104d7033c chromedriver + 4031292
14 chromedriver 0x0000000104d6af34 chromedriver + 4009780
15 chromedriver 0x0000000104d43490 chromedriver + 3847312
16 chromedriver 0x0000000104d889f4 chromedriver + 4131316
17 chromedriver 0x0000000104d88b4c chromedriver + 4131660
18 chromedriver 0x0000000104d99230 chromedriver + 4198960
19 libsystem_pthread.dylib 0x000000018196606c _pthread_start + 148
20 libsystem_pthread.dylib 0x0000000181960e2c thread_start + 8
What is wrong with my code?
Anyways I tried using a different executable_path='/Users/adeelsyed/Downloads/chromedriver_mac64\ 2/chromedriver' because that is what the terminal spat out, but that did not work either, and I really just have no clue what is wrong here.
答案1
得分: 1
以下是翻译好的内容:
同样的问题在一个月前我也遇到过,当时我需要在单个浏览器中测试多个应用程序。我不得不在每次测试之前都登录,这非常让人恼火。
我花了两天半的时间解决了这个问题。让我与您分享解决方案。
在运行脚本之前,您无需手动运行任何命令。只需按照以下步骤操作:
步骤 1。确保您已使用以下命令安装了最新版本的 selenium 和 webdriver-manager:
pip install selenium webdriver-manager
。
步骤 2。确保您已安装了最新版本的 Chrome 浏览器。
解决方案:
以下是在 Windows 机器上经过测试并正常运行的代码:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
driverPath = ChromeDriverManager().install()
subprocess.Popen(["start","chrome","--remote-debugging-port=9089","--user-data-dir=" + os.getcwd() + "/chromeProfile",],shell=True)
options = Options()
options.add_argument("--start-maximized")
options.add_experimental_option("debuggerAddress", "localhost:9089")
service = Service(executable_path=driverPath)
driver = webdriver.Chrome(service=service,options=options)
现在,您可以使用上面的驱动程序实例打开任何网址。
url = 'https://monkeytype.com/'
driver.get(url)
print("test")
当您运行此脚本时,它将在当前工作目录中创建一个名为 "chromeProfile" 的文件夹。所有浏览器数据和历史记录都将保存在该文件夹中。您将不需要再次登录同一个 Web 应用程序。
就是这样。。
如果您仍然遇到任何问题,请告诉我。
英文:
The same problem I was facing a month ago when I need to test multiple apps in a single browser. I had to login every time before the test and it was very irritating.
I solved it after investing 2 and a half days. Let me share the solution with you.
You don't need to run any command manually before run the script. Just follow the steps below:
Step 1. Make sure you have installed the latest version of selenium and webdriver-manager using this command:
pip install selenium webdriver-manager
.
Step 2. Make Sure You have installed the latest version of Chrome.
Solution:
Here is the code tested and working fine on a Windows machine:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
driverPath = ChromeDriverManager().install()
subprocess.Popen(["start","chrome","--remote-debugging-port=9089","--user-data-dir=" + os.getcwd() + "/chromeProfile",],shell=True)
options = Options()
options.add_argument("--start-maximized")
options.add_experimental_option("debuggerAddress", "localhost:9089")
service = Service(executable_path=driverPath)
driver = webdriver.Chrome(service=service,options=options)
Now You can open any url using above driver instance.
url = 'https://monkeytype.com/'
driver.get(url)
print("test")
When you run this script it will create a folder name "chromeProfile" in your current working directory. All the browser data and history will save into that folder. You will not need to login again and agian for the same web app.
That's it ..
Let me know if you still facing any issue.
答案2
得分: 0
对于 Mac
正如我们所知,subprocess
模块使用我们的操作系统终端来执行命令。
因此,如果你使用的是 Mac,那么上面的代码在你的机器上不会起作用,因为终端命令不同。
你只需要根据你的操作系统更改 subprocess
命令。这行代码的主要目的是使用任何开放的端口(9089)以远程调试模式打开 Chrome,以便 Selenium 可以检测并绑定驱动程序与已打开的 Chrome 实例。
要将代码修改为在 Mac 上启用远程调试的 Chrome,你可以使用 subprocess.run
函数。以下是修改后的代码:
import subprocess
import os
chrome_command = [
"open",
"-a",
"Google Chrome",
"--args",
"--remote-debugging-port=9089",
"--user-data-dir=" + os.getcwd() + "/chromeProfile",
]
subprocess.run(chrome_command)
让我解释一下上面的代码:
- 使用
subprocess.run
代替了subprocess.Popen
。这简化了代码并提供了一种更直接的执行命令的方式。 - 在 macOS 上打开 Chrome 的命令是 "
open
"。它与-a
标志一起使用,以指定要打开的应用程序,这里是 "Google Chrome
"。 - 使用
--args
标志表示接下来的参数应传递给应用程序。 - "
--remote-debugging-port=9089
" 和 "--user-data-dir=
" +os.getcwd()
+ "/chromeProfile
" 参数保持不变,启用端口 9089 上的远程调试,并在当前工作目录中的 "chromeProfile" 文件夹中使用单独的用户配置文件。
通过使用这些修改,代码应该能够成功在 Mac 上启动具有启用远程调试的 Chrome。
注意
所有其余的代码行与上面的答案中的相同。
英文:
For Mac
As we know subprocess
module uses our OS terminal to execute the commands.
So, if you are on Mac then the above code will not work on your machine due to the differences in terminal commands.
All you need to change the subprocess
command with respect to your OS. The main purpose of this line of code is to open Chrome in remote debugging mode using any open-free port (9089) so that Selenium can detect and bind the driver with the opened Chrome instance.
To convert the code for launching Chrome with remote debugging enabled on a Mac, you can use the subprocess.run
function instead. Here's the modified code:
import subprocess
import os
chrome_command = [
"open",
"-a",
"Google Chrome",
"--args",
"--remote-debugging-port=9089",
"--user-data-dir=" + os.getcwd() + "/chromeProfile",
]
subprocess.run(chrome_command)
Let me explain the above code:
subprocess.run
is used instead ofsubprocess.Popen
. This
simplifies the code and provides a more straightforward way to
execute the command.- The command to open Chrome on macOS is "
open
". It is used in
combination with the-a
flag to specify the application to open,
which is "Google Chrome
" in this case. - The
--args
flag is used to indicate that the following arguments
should be passed to the application. - "
--remote-debugging-port=9089
" and "--user-data-dir=
" +os.getcwd()
+
"/chromeProfile
" arguments remain the same, enabling remote debugging
on port 9089 and using a separate user profile stored in the
"chromeProfile" folder within the current working directory.
By using these modifications, the code should successfully launch Chrome with remote debugging enabled on a Mac.
Note
All the remaining lines of code will be the same as in the above answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论