简体   繁体   English

chromedriver 打开一个网站然后立即崩溃使用 selenium 4 in vs code

[英]chromedriver opens a website then immediately crashes using selenium 4 in vs code

from selenium import webdriver
PATH= "C:\Program Files (x86)\chromedriver.exe"
driver= webdriver.Chrome(PATH)
driver.get("https://www.ieltsadvantage.com/writing-task-1/")

after running the code it opens a website but immediately crashes and window closes运行代码后,它打开一个网站,但立即崩溃,window 关闭

With selenium 4 you need to set the "detach" option to True when starting chromedriver:对于selenium 4 ,您需要在启动 chromedriver 时将“分离”选项设置为 True:

from selenium import webdriver
PATH= "C:\Program Files (x86)\chromedriver.exe"
opts = webdriver.ChromeOptions() # Create options
opts.add_experimental_option("detach", True) # add detach
driver = webdriver.Chrome(PATH, options=opts) # bind options in Chrome()
driver.get("https://www.ieltsadvantage.com/writing-task-1/")

However, executable_path is deprecated.但是, executable_path已弃用。 Here's an updated code:这是更新的代码:

from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome_executable = webdriver.chrome.service.Service(executable_path=PATH) # use services instead
opts = webdriver.ChromeOptions()
opts.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=chrome_executable, options=opts) # bind service
driver.get("https://www.ieltsadvantage.com/writing-task-1/")

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

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