简体   繁体   中英

Python class to open and close website using Selenium

Trying to setup Chrome for the open / close a website. Now i can open it, But failed to close it.

Can anyone tell me why? Many thanks!

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()

You are not making driver an instance attribute. Change searchWeb method like this:

def searchWeb(self, url="https://www.google.com.hk/"):
    self.driver = webdriver.Chrome(self.chrome_path)
    self.driver.get(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM