Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/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 Selenium在同一Firefox窗口中打开URL_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium在同一Firefox窗口中打开URL

Python Selenium在同一Firefox窗口中打开URL,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在使用Python Selenium打开Firefox浏览器并转到URL。我用来做这件事的函数是 def openurl_function(): from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys from selenium imp

我正在使用Python Selenium打开Firefox浏览器并转到URL。我用来做这件事的函数是

def openurl_function():
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get('http://www.example.com')
当我运行该函数时,它总是打开一个新的FireFox实例,有没有办法让它只使用相同的浏览器实例打开


当前,如果我运行该函数10次,那么我将打开10个FireFox浏览器。

只要继续重用相同的
驱动程序即可。每次调用时,您都在创建一个新浏览器

driver = webdriver.Firefox()

此外,由于您从未在驱动程序上退出()
,您可能会让所有浏览器保持孤立状态,因为您在创建新浏览器时删除了它们的句柄。

因此我需要将driver=webdriver.Firefox()移到函数外部,以便它只运行一次吗?是的。只需在全局上下文中创建对象,并每次将其传递给函数(如果对象在类中,则使用对象属性)。