使用 Selenium 打开和关闭网站的 Python 类

标签 python selenium class

正在尝试为 打开/关闭 网站设置 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/

相关文章:

java - 在类中创建类的对象

java - 如何找到包含类定义的jar文件?

python - 您如何实现基于网络的直接存款/电子支票支付系统?

python - 反转字典中的键值(Python 中的高级反转字符串)

python - 如何根据另一个元素访问字典的子元素?

python - 在Python中读取文件时抑制错误消息

java - 无法获取使用selenium xpath动态显示的双引号中的值

python - 如何使用 selenium-webdriver 和 python 在浏览器的单独选项卡中打开多个网页

python - 查找页面上文本的部分内容,然后使用 Selenium 在 Python 中返回该 <div> 的完整内容

c++ - 在 C++ 中将类变量传递给异常