lxg1434334642 发表于 2024-1-25 16:44:42

谷歌浏览器多开两个独立的网页窗口

编程实现:想用Python代码实现谷歌浏览器多开个独立的网页窗口,并且数据不互通,完全独立。

FishC 发表于 2024-1-25 16:46:23

要实现用Python代码在谷歌浏览器中打开多个独立的网页窗口,并且数据不互通,我们可以使用selenium库来模拟用户操作浏览器。

首先,你需要安装selenium库。你可以使用以下命令在终端或命令提示符中安装它:

pip install selenium

然后,你需要下载一个谷歌浏览器驱动程序,以便selenium可以控制浏览器。你可以从这里下载适合你谷歌浏览器版本的驱动程序:https://sites.google.com/a/chromium.org/chromedriver/downloads

下载后,将驱动程序解压缩到一个合适的位置,并将其路径添加到环境变量中。

接下来,你可以使用以下代码来实现在谷歌浏览器中打开多个独立的网页窗口:

from selenium import webdriver

# 创建第一个浏览器窗口
driver1 = webdriver.Chrome()# 这里的路径是你下载的谷歌浏览器驱动程序的路径
driver1.get("https://www.google.com")# 打开第一个网页

# 创建第二个浏览器窗口
driver2 = webdriver.Chrome()# 这里的路径是你下载的谷歌浏览器驱动程序的路径
driver2.get("https://www.baidu.com")# 打开第二个网页

# 程序暂停,保持窗口打开状态
input("按任意键结束...")

# 关闭浏览器窗口
driver1.quit()
driver2.quit()

通过调用`webdriver.Chrome()`创建一个Chrome浏览器实例,然后使用`get()`方法访问指定的网页。你可以根据需求创建更多的浏览器窗口。

这样,你就可以在谷歌浏览器中打开多个独立的网页窗口了,它们之间的数据是完全独立的。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 10:34:44

FishC 发表于 2024-1-25 16:46
要实现用Python代码在谷歌浏览器中打开多个独立的网页窗口,并且数据不互通,我们可以使用selenium库来模拟 ...

请问这个括弧里面写路径么?driver1 = webdriver.Chrome()

FishC 发表于 2024-1-26 10:35:24

lxg1434334642 发表于 2024-01-26 10:34
请问这个括弧里面写路径么?driver1 = webdriver.Chro ...

是的,括号中的内容是指定驱动程序的路径。当你调用`webdriver.Chrome()`时,它会在你的系统上搜索Chrome浏览器驱动程序,并使用找到的第一个驱动程序。如果驱动程序不在系统路径中,你需要手动指定驱动程序的路径,例如:driver1 = webdriver.Chrome("C:/path/to/chromedriver")。在这个例子中,"C:/path/to/chromedriver"应该替换为你实际下载的谷歌浏览器驱动程序的路径。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 10:41:16

FishC 发表于 2024-1-26 10:35
是的,括号中的内容是指定驱动程序的路径。当你调用`webdriver.Chrome()`时,它会在你的系统上搜索Chro ...

请问这个是什么原因?>>> driver1.get("https://www.xuexi.cn/")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'driver1' is not defined

FishC 发表于 2024-1-26 10:41:48

lxg1434334642 发表于 2024-01-26 10:41
请问这个是什么原因?>>> driver1.get("https://www.x ...

这个错误提示说明变量 `driver1` 未被定义。这可能是因为在调用 `driver1.get("https://www.xuexi.cn/")` 前没有创建名为 `driver1` 的对象。

请确保你已经正确创建了浏览器对象,可以按照以下步骤进行操作:

1. 导入selenium库:`from selenium import webdriver`
2. 创建浏览器对象:`driver1 = webdriver.Chrome()` (确保已经正确安装了Chrome浏览器驱动程序并加入了环境变量)
3. 使用 `get()` 方法打开指定网页:`driver1.get("https://www.xuexi.cn/")`

请注意,在执行代码之前应该按照上述步骤正确创建和调用浏览器对象。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 10:46:00

FishC 发表于 2024-1-26 10:41
这个错误提示说明变量 `driver1` 未被定义。这可能是因为在调用 `driver1.get("https://www.xuexi.cn/" ...

请问这个是什么原因?driver1 = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chrome.exe")
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
File "C:\Users\Lenovo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\common\selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    driver1 = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chrome.exe")
File "C:\Users\Lenovo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
File "C:\Users\Lenovo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\chromium\webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
File "C:\Users\Lenovo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
AttributeError: 'str' object has no attribute 'capabilities'

FishC 发表于 2024-1-26 10:46:39

lxg1434334642 发表于 2024-01-26 10:46
请问这个是什么原因?driver1 = webdriver.Chrome("C: ...

这个错误是因为你在创建Chrome浏览器实例时,传递了错误的参数。在`webdriver.Chrome()`中,你需要传递谷歌浏览器驱动程序的路径,而不是浏览器应用程序的路径。

请修改代码如下:
driver1 = webdriver.Chrome("path_to_chromedriver")
其中,`path_to_chromedriver` 是你下载的谷歌浏览器驱动程序的路径。

另外,注意确保你已经正确安装了Chrome浏览器驱动程序并将其路径添加到环境变量中。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 10:53:56

FishC 发表于 2024-1-26 10:46
这个错误是因为你在创建Chrome浏览器实例时,传递了错误的参数。在`webdriver.Chrome()`中,你需要传递 ...

怎么下载谷歌浏览器驱动程序

FishC 发表于 2024-1-26 10:54:38

lxg1434334642 发表于 2024-01-26 10:53
怎么下载谷歌浏览器驱动程序

要下载谷歌浏览器驱动程序,你可以按照以下步骤进行:

1. 打开浏览器,进入谷歌浏览器驱动程序的下载页面:https://sites.google.com/a/chromium.org/chromedriver/downloads
2. 在页面上找到与你的谷歌浏览器版本对应的驱动程序,并点击下载链接。驱动程序的版本号应与你所用的谷歌浏览器版本相匹配,以避免出现兼容性问题。
3. 下载完成后,将驱动程序解压缩到一个合适的位置。确保记住该位置,因为你需要在代码中指定驱动程序的路径。

请注意,下载并使用谷歌浏览器驱动程序时,需要注意以下几点:
- 谷歌浏览器和驱动程序的版本必须匹配,否则可能会出现兼容性问题。
- 将驱动程序的路径添加到环境变量中,这样你就可以在代码中直接使用驱动程序文件名,而不需要指定完整的路径。
- 在使用驱动程序之前,确保已经安装了谷歌浏览器。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 10:57:37

FishC 发表于 2024-1-26 10:54
要下载谷歌浏览器驱动程序,你可以按照以下步骤进行:

1. 打开浏览器,进入谷歌浏览器驱动程序的下 ...

该网址无法打开,https://sites.google.com/a/chromium.org/chromedriver/downloads

FishC 发表于 2024-1-26 10:58:09

lxg1434334642 发表于 2024-01-26 10:57
该网址无法打开,https://sites.google.com/a/chromiu ...

很抱歉,我给出的链接可能已经过时了。不过,你可以尝试以下方法来下载谷歌浏览器驱动程序:

1. 打开谷歌浏览器的官方网站:https://www.google.com/chrome/。
2. 在页面上找到并点击下载按钮,以下载最新版本的谷歌浏览器。
3. 安装最新版本的谷歌浏览器。
4. 在浏览器中输入以下网址,即可下载与你的谷歌浏览器版本相匹配的驱动程序:
   https://chromedriver.chromium.org/home
5. 在该网页上,你可以找到合适的驱动程序版本,并选择适合你操作系统的下载链接。
6. 下载完成后,将驱动程序解压缩到一个合适的位置,并将其路径添加到环境变量中。

请注意,确保下载并使用与你的谷歌浏览器版本匹配的驱动程序,以确保兼容性。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 14:20:05

FishC 发表于 2024-1-26 10:58
很抱歉,我给出的链接可能已经过时了。不过,你可以尝试以下方法来下载谷歌浏览器驱动程序:

1. 打 ...

请问如何安装谷歌驱动程序并将其路径添加到环境变量中

FishC 发表于 2024-1-26 14:21:06

lxg1434334642 发表于 2024-01-26 14:20
请问如何安装谷歌驱动程序并将其路径添加到环境变量中

安装谷歌驱动程序并将其路径添加到环境变量中需要以下几个步骤:

1. 下载适合你的操作系统的谷歌驱动程序,可以在这里找到对应版本:https://chromedriver.chromium.org/home

2. 解压下载的文件,得到一个可执行文件。

3. 将该可执行文件移动到一个你方便管理的目录下,比如你可以创建一个名为 "webdriver" 的文件夹,并将驱动程序移动到其中。

4. 打开你的计算机的系统环境变量设置。在 Windows 上,你可以按 Win + X 键,然后选择 "系统",再选择 "高级系统设置"。点击 "环境变量" 按钮。

5. 在 "系统变量" 部分,找到名为 "Path" 的变量,并点击编辑。

6. 在 "编辑环境变量" 对话框中,点击 "新建",然后输入你刚刚移动的驱动程序所在的完整路径。比如,如果你将驱动程序放在 "webdriver" 文件夹下,路径可能是 "C:\webdriver"。

7. 点击 "确定" 关闭所有对话框。

8. 重新打开你的命令提示符或终端窗口,并输入 "chromedriver" 来测试安装是否成功。如果安装成功,你应该能够看到驱动程序的版本信息。

完成以上步骤后,你就成功安装了谷歌驱动程序并将其路径添加到了环境变量中。这样,你就可以使用selenium库来控制谷歌浏览器了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 14:33:12

FishC 发表于 2024-1-26 14:21
安装谷歌驱动程序并将其路径添加到环境变量中需要以下几个步骤:

1. 下载适合你的操作系统的谷歌驱 ...

怎么重装selenium

FishC 发表于 2024-1-26 14:34:22

lxg1434334642 发表于 2024-01-26 14:33
怎么重装selenium

如果你需要重装selenium库,可以按照以下步骤进行:

1. 在终端或命令提示符中,使用以下命令卸载已安装的selenium库:
pip uninstall selenium

2. 确保你的计算机上已经安装了Python解释器和pip包管理器。如果没有,请先安装它们。

3. 重新安装selenium库,使用以下命令:
pip install selenium

4. 如果你希望安装特定版本的selenium库,可以在上述命令后面添加版本号。例如,要安装3.141.0版本的selenium,可以使用以下命令:
pip install selenium==3.141.0

5. 安装完成后,你可以通过在Python脚本中导入selenium来测试是否成功安装。例如,可以尝试运行以下代码:
import selenium
print(selenium.__version__)
如果能够打印出所安装的selenium版本号,则表示安装成功。

通过以上步骤,你就可以重新安装selenium库了。注意在安装之前,建议确保你的网络连接正常,以便能够正常下载并安装所需的软件包。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 14:42:31

FishC 发表于 2024-1-26 14:34
如果你需要重装selenium库,可以按照以下步骤进行:

1. 在终端或命令提示符中,使用以下命令卸载已 ...

在安装selenium库,出现下面的代码,什么原因ERROR: Exception:
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 438, in _error_catcher
    yield
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 561, in read
    data = self._fp_read(amt) if not fp_closed else b""
         ^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 527, in _fp_read
    return self._fp.read(amt) if amt is not None else self._fp.read()
         ^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 98, in read
    data: bytes = self.__fp.read(amt)
                  ^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\http\client.py", line 472, in read
    s = self.fp.read(amt)
      ^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\socket.py", line 707, in readinto
    return self._sock.recv_into(b)
         ^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1249, in recv_into
    return self.read(nbytes, buffer)
         ^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\ssl.py", line 1105, in read
    return self._sslobj.read(len, buffer)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\base_command.py", line 180, in exc_logging_wrapper
    status = run_func(*args)
             ^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\cli\req_command.py", line 245, in wrapper
    return func(self, options, args)
         ^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\commands\install.py", line 377, in run
    requirement_set = resolver.resolve(
                      ^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\resolver.py", line 95, in resolve
    result = self._result = resolver.resolve(
                            ^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 546, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 397, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\resolvelib\resolvers.py", line 173, in _add_to_criteria
    if not criterion.candidates:
         ^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\resolvelib\structs.py", line 156, in __bool__
    return bool(self._sequence)
         ^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 155, in __bool__
    return any(self)
         ^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\found_candidates.py", line 47, in _iter_built
    candidate = func()
                ^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 182, in _make_candidate_from_link
    base: Optional = self._make_base_candidate_from_link(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\factory.py", line 228, in _make_base_candidate_from_link
    self._link_candidate_cache = LinkCandidate(
                                       ^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 293, in __init__
    super().__init__(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 156, in __init__
    self.dist = self._prepare()
                ^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 225, in _prepare
    dist = self._prepare_distribution()
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\resolution\resolvelib\candidates.py", line 304, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 519, in prepare_linked_requirement
    metadata_dist = self._fetch_metadata_only(req)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 371, in _fetch_metadata_only
    return self._fetch_metadata_using_link_data_attr(
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 391, in _fetch_metadata_using_link_data_attr
    metadata_file = get_http_url(
                  ^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\operations\prepare.py", line 109, in get_http_url
    from_path, content_type = download(link, temp_dir.path)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\download.py", line 147, in __call__
    for chunk in chunks:
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_internal\network\utils.py", line 63, in response_chunks
    for chunk in response.raw.stream(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 622, in stream
    data = self.read(amt=amt, decode_content=decode_content)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 560, in read
    with self._error_catcher():
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(value)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\urllib3\response.py", line 443, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

FishC 发表于 2024-1-26 14:43:25

lxg1434334642 发表于 2024-01-26 14:42
在安装selenium库,出现下面的代码,什么原因ERROR: E ...

出现该错误可能是由于下载selenium库的过程中出现了网络连接问题,导致读取超时。

你可以尝试以下解决方案:

1. 检查你的网络连接是否正常,确保能够访问互联网。
2. 确保你使用的pip版本是最新的。你可以使用以下命令将pip更新到最新版本:`pip install --upgrade pip`
3. 可以尝试使用国内镜像源来安装selenium库。例如,可以使用清华大学的镜像源来替换默认的pip源。使用以下命令来配置清华大学的镜像源:
   - 对于使用命令提示符的Windows用户:`pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple`
   - 对于使用终端的Linux或Mac用户:`pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple`
   然后再次运行安装selenium的命令:`pip install selenium`
4. 如果上述步骤都没有解决问题,你可以考虑使用其他的下载方式,例如使用`pip`的`--proxy`参数指定代理服务器,或者直接下载selenium的安装包文件(.whl文件)并通过本地安装。

希望以上方法能够解决你的问题。如果问题仍然存在,请提供更多信息以便进一步帮助。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

lxg1434334642 发表于 2024-1-26 15:01:02

FishC 发表于 2024-1-26 14:43
出现该错误可能是由于下载selenium库的过程中出现了网络连接问题,导致读取超时。

你可以尝试以下解 ...

请这个是什么原因driver1 = webdriver.Chrome("path_to_chromedriver")
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path
    path = SeleniumManager().driver_location(options) if path is None else path
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
AttributeError: 'str' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    driver1 = webdriver.Chrome("path_to_chromedriver")
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path
    msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
AttributeError: 'str' object has no attribute 'capabilities'

FishC 发表于 2024-1-26 15:01:45

lxg1434334642 发表于 2024-01-26 15:01
请这个是什么原因driver1 = webdriver.Chrome("path_t ...

出现该错误是因为你传递给`webdriver.Chrome()`的参数应该是一个字典对象,而不是一个字符串。要解决这个问题,你需要将`webdriver.Chrome()`的参数修改为字典形式,其中包含指定路径的键值对。例如:driver1 = webdriver.Chrome(executable_path="path_to_chromedriver")在这里,"path_to_chromedriver"应该替换为实际的谷歌浏览器驱动程序的路径。请确保提供的路径是正确的,并且将路径放在双引号或单引号中。通过这样的修改,你应该能够成功创建一个谷歌浏览器实例。如果问题仍然存在,请提供更多详细信息以便进一步帮助。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1] 2
查看完整版本: 谷歌浏览器多开两个独立的网页窗口