Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行脚本时,浏览器会打开两次_Python_Google Chrome_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Python 运行脚本时,浏览器会打开两次

Python 运行脚本时,浏览器会打开两次,python,google-chrome,selenium,selenium-webdriver,webdriver,Python,Google Chrome,Selenium,Selenium Webdriver,Webdriver,我正在开发一个web驱动程序,并试图在代码中实现类。我有这个工作,但当我把它变成一个类,我的浏览器开始打开两次,当我运行程序。它将打开一个浏览器,然后打开第二个浏览器,运行命令,然后保持第一个浏览器打开。有人能告诉我为什么会这样吗?这只是一个如何格式化的示例 import selenium import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.web

我正在开发一个web驱动程序,并试图在代码中实现类。我有这个工作,但当我把它变成一个类,我的浏览器开始打开两次,当我运行程序。它将打开一个浏览器,然后打开第二个浏览器,运行命令,然后保持第一个浏览器打开。有人能告诉我为什么会这样吗?这只是一个如何格式化的示例

import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

class About(object):
    def __init__(self, driver):
        self.driver = driver

    def pause(self):
        time.sleep(1)

    def run(self):
        self.driver.get('https://google.com')
        self.pause()

    def tearDown(self):
        self.driver.quit()

go = About(webdriver.Chrome())

if __name__ == '__main__':
    go.run()
    go.tearDown()

这对我来说是正确的,我做的唯一更改是将
chromedriver.exe
path作为参数添加到
webride.Chrome('here')
,您使用的是Chrome驱动程序吗?很抱歉,我没有注意到这一点。我得到一个
“chromedriver”未定义
错误您应该下载chromedriver.exe,然后将其路径作为参数添加到webderiver.Chrome()函数,您这样做了吗?如果我完全按照编写的方式运行您的代码,我只得到一个浏览器。它对我来说工作正常,我所做的唯一更改是将
chromedriver.exe
path作为参数添加到
webride.Chrome('here')
,您正在使用Chrome驱动程序吗?很抱歉,我没有注意到这一点。我得到一个
“chromedriver”未定义
错误您应该下载chromedriver.exe,然后将其路径作为参数添加到webderiver.Chrome()函数,您这样做了吗?如果我完全按照编写的方式运行您的代码,我只会得到一个浏览器。