简体   繁体   English

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

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

Good Day Ahead.美好的一天。 After searching numerious posts in Stackoverflow, I tried to write and implement the automation using selenium and python programming.在 Stackoverflow 中搜索了大量帖子后,我尝试使用 selenium 和 python 编程来编写和实现自动化。 I tried my first code as below and i am getting error.我尝试了如下的第一个代码,但出现错误。 My browser opens and closes very fast.我的浏览器打开和关闭速度非常快。 It throws error in the output.它在 output 中引发错误。

Code:-代码:-

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)

Error:-错误:-

/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

Please help me请帮我

I don't understand why this line comes我不明白为什么这条线来了

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

After you already properly created webdriver instance with在您已经正确创建了 webdriver 实例之后

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

? ?
Looks like your code should be as following:看起来您的代码应如下所示:

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 UPD
To make your browser stay opened add "detach", True option, as following:要使您的浏览器保持打开状态,请添加"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