繁体   English   中英

Selenium 自动化 Chrome 浏览器打开和关闭并显示错误消息

[英]Selenium Automation Chrome browser opens and closes with error mesage

美好的一天。 在 Stackoverflow 中搜索了大量帖子后,我尝试使用 selenium 和 python 编程来编写和实现自动化。 我尝试了如下的第一个代码,但出现错误。 我的浏览器打开和关闭速度非常快。 它在 output 中引发错误。

代码:-

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

s=Service(ChromeDriverManager().install())
chrome_driver = webdriver.Chrome(service=s)

browser = webdriver.Chrome("/home/halovivek/Documents/Automation/selenium_driver/")
browser.implicitly_wait(5)
browser.get("https://www.google.com")
#browser.get("https://kite.zerodha.com/")
browser.implicitly_wait(5)

错误:-

/home/halovivek/PycharmProjects/yearcoding/venv/bin/python /home/halovivek/PycharmProjects/yearcoding/06092022_selenium1.py 
/home/halovivek/PycharmProjects/yearcoding/06092022_selenium1.py:9: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Chrome("/home/halovivek/Documents/Automation/selenium_driver/")
Traceback (most recent call last):
  File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/lib/python3.10/subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1842, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/home/halovivek/Documents/Automation/selenium_driver/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/halovivek/PycharmProjects/yearcoding/06092022_selenium1.py", line 9, in <module>
    browser = webdriver.Chrome("/home/halovivek/Documents/Automation/selenium_driver/")
  File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 89, in __init__
    self.service.start()
  File "/home/halovivek/PycharmProjects/yearcoding/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 86, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: '' executable may have wrong permissions. Please see https://chromedriver.chromium.org/home


Process finished with exit code 1

请帮我

我不明白为什么这条线来了

browser = webdriver.Chrome("/home/halovivek/Documents/Automation/selenium_driver/")

在您已经正确创建了 webdriver 实例之后

s=Service(ChromeDriverManager().install())
chrome_driver = webdriver.Chrome(service=s)

?
看起来您的代码应如下所示:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)

driver.implicitly_wait(5)
driver.get("https://www.google.com")

UPD
要使您的浏览器保持打开状态,请添加"detach", True选项,如下所示:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s, chrome_options=chrome_options)

driver.implicitly_wait(5)
driver.get("https://www.google.com")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM