即使在selenium中使用分离,浏览器也会关闭



我的问题我使用了分离方法来停止浏览器在代码运行后自动关闭。它不起作用。下面我给出了代码供您参考。请帮帮我。我也尝试过隐性等待,但不起作用感谢

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

浏览器会自动关闭。我没有对关闭浏览器发表任何评论。

您没有正确设置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)

谢谢。它在工作browser=webdriver。Chrome(服务=s,选项=Chrome_options(这是我需要更新的东西。我更改了代码,现在运行良好。

最新更新