Chrome 即使在Selenium中使用分离后,浏览器也会关闭

rsaldnfx  于 2022-12-06  发布在  Go
关注(0)|答案(2)|浏览(146)

我的问题我已经使用了分离方法来阻止浏览器在代码运行后自动关闭。它不工作。下面我给你的代码参考。请帮助我。我也尝试了隐式等待,但它不工作,谢谢

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
s = Service(ChromeDriverManager().install())
# chrome_options = Options()
# chrome_options.add_experimental_option("detach",True)
"""
The browser will be started.
"""
browser = webdriver.Chrome(service=s)
browser.maximize_window()
chrome_options = Options()
chrome_options.add_experimental_option("detach",True)
browser.get("https://rahulshettyacademy.com")
print(browser.title)
browser.get("https://rahulshettyacademy.com/angularpractice/")
print(browser.title)
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[1]/input[1]").send_keys("OVM")
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[2]/input[1]").send_keys("ovm@gmail.com")
browser.find_element(By.ID,"exampleInputPassword1").send_keys("123456789")
browser.find_element(By.XPATH,"//input[@id='exampleCheck1']").click()
browser.find_element(By.XPATH,"//select[@id='exampleFormControlSelect1']").click()
browser.find_element(By.XPATH,"//input[@id='inlineRadio2']").click()
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[7]/input[1]").send_keys("01/01/2000")
browser.implicitly_wait(20000)

结果:-

Rahul Shetty Academy
ProtoCommerce

Process finished with exit code 0

浏览器自动关闭。我没有给予任何关闭浏览器的评论。

628mspwn

628mspwn1#

您没有正确设置detach选项。下面的代码对我有效:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
s = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_experimental_option("detach",True)
"""
The browser will be started.
"""
browser = webdriver.Chrome(service=s, options=chrome_options)
browser.maximize_window()
browser.get("https://rahulshettyacademy.com")
print(browser.title)
browser.get("https://rahulshettyacademy.com/angularpractice/")
print(browser.title)
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[1]/input[1]").send_keys("OVM")
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[2]/input[1]").send_keys("ovm@gmail.com")
browser.find_element(By.ID,"exampleInputPassword1").send_keys("123456789")
browser.find_element(By.XPATH,"//input[@id='exampleCheck1']").click()
browser.find_element(By.XPATH,"//select[@id='exampleFormControlSelect1']").click()
browser.find_element(By.XPATH,"//input[@id='inlineRadio2']").click()
browser.find_element(By.XPATH,"//body/app-root[1]/form-comp[1]/div[1]/form[1]/div[7]/input[1]").send_keys("01/01/2000")
browser.implicitly_wait(20000)
fcwjkofz

fcwjkofz2#

谢谢。它的工作浏览器= webdriver。Chrome(service=s,options=chrome_options)这是我需要更新的东西。我改变了代码,它现在工作正常。

相关问题