正在尝试为 打开/关闭
网站设置 Chrome
。现在我可以打开它,但无法关闭它。
谁能告诉我为什么?非常感谢!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class chromeSetup():
def __init__(self):
self.chrome_path = r'C:\XXXXX\chromedriver.exe'
def searchWeb(self, url="https://www.google.com.hk/"):
driver = webdriver.Chrome(self.chrome_path)
driver.get(url)
def close(self):
self.driver.close()
最佳答案
您没有将 driver
设为实例属性。像这样更改 searchWeb
方法:
def searchWeb(self, url="https://www.google.com.hk/"):
self.driver = webdriver.Chrome(self.chrome_path)
self.driver.get(url)
关于使用 Selenium 打开和关闭网站的 Python 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58970430/