【问题标题】:how to stop the automatic tab closing in selenium?如何停止在 selenium 中自动关闭标签页?
【发布时间】:2022-11-22 23:32:29
【问题描述】:
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\\Users\\DELL\\PycharmProjects\\drivers\\chromedriver.exe")
driver.get("http://www.letskodeit.com")
我写了这样的代码并且代码执行时没有任何错误,但问题是它通过“get”命令自动打开的网站在打开后自动关闭。
请帮我解决这个问题
请帮我解决这个问题。
【问题讨论】:
标签:
python
selenium
selenium-webdriver
selenium-chromedriver
【解决方案1】:
如果你想让驱动程序保持打开状态,你必须在创建驱动程序实例时使用detach选项。
如下:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options, executable_path="C:\Users\DELL\PycharmProjects\drivers\chromedriver.exe")
driver.get("http://www.letskodeit.com")