Selenium window 在导入 "requests" 模块时立即关闭
Selenium window closes instantly when "requests" module is imported
我正在尝试将 Selenium 和 Beautiful Soup 与 Python 一起使用,但遇到一个相当奇怪的问题 - Selenium window 仅在未导入请求模块时保持打开状态,否则它保持打开状态像 1 秒然后关闭。
重要的是,这只发生在我在另一个文件中创建 class 时 - 当我创建 class在同一个文件中,它保持正常打开状态。
以下是代码的两个版本 - 第一个 window 保持打开状态,第二个 window 立即关闭:
1:作品
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
2:关闭
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
我想知道这是否与 Keep-Alive 和 urllib3 中的连接池有关,即使程序代码中没有使用请求模块。
当您的程序终止时,HTTP 连接被释放并且浏览器 window 关闭。
添加这个,您会看到浏览器 window 保持打开状态,直到程序完成 运行。该程序简单地正常终止,没有错误。
from time import sleep
for _ in range(10):
sleep(1)
[编辑]我有办法!
detach(布尔值)— 当驱动程序发送退出命令时浏览器是否关闭。
见Class: Selenium::WebDriver::Chrome::Options
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from watcher import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=options)
tw = Watcher(driver)
tw.open_browser()
我正在尝试将 Selenium 和 Beautiful Soup 与 Python 一起使用,但遇到一个相当奇怪的问题 - Selenium window 仅在未导入请求模块时保持打开状态,否则它保持打开状态像 1 秒然后关闭。
重要的是,这只发生在我在另一个文件中创建 class 时 - 当我创建 class在同一个文件中,它保持正常打开状态。
以下是代码的两个版本 - 第一个 window 保持打开状态,第二个 window 立即关闭:
1:作品
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
2:关闭
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
我想知道这是否与 Keep-Alive 和 urllib3 中的连接池有关,即使程序代码中没有使用请求模块。
当您的程序终止时,HTTP 连接被释放并且浏览器 window 关闭。
添加这个,您会看到浏览器 window 保持打开状态,直到程序完成 运行。该程序简单地正常终止,没有错误。
from time import sleep
for _ in range(10):
sleep(1)
[编辑]我有办法!
detach(布尔值)— 当驱动程序发送退出命令时浏览器是否关闭。
见Class: Selenium::WebDriver::Chrome::Options
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from watcher import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=options)
tw = Watcher(driver)
tw.open_browser()