Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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 如何在登录的slenium中执行类,而不是打开新的chrome实例?_Python_Python 3.x_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python 如何在登录的slenium中执行类,而不是打开新的chrome实例?

Python 如何在登录的slenium中执行类,而不是打开新的chrome实例?,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,因此,脚本运行BasicBot(usr,psswd)登录到该站点,并且在登录时应该转到其他站点,然后搜索X post是否符合给定的条件,如果符合,则是这样,如果不符合,我应该刷新该站点并再次检查。在搜索类中,首先创建一个新的chromedriver实例:self.driver=webdriver.Chrome('chromedriver.exe'),这就是为什么它会打开另一个具有新状态的窗口 相反,将搜索类修改为 以webdriver的现有实例为例 使用脚本窗口打开一个新窗口。打开而不是get:

因此,脚本运行
BasicBot(usr,psswd)
登录到该站点,并且在登录时应该转到其他站点,然后搜索X post是否符合给定的条件,如果符合,则是这样,如果不符合,我应该刷新该站点并再次检查。

在搜索类中,首先创建一个新的chromedriver实例:
self.driver=webdriver.Chrome('chromedriver.exe')
,这就是为什么它会打开另一个具有新状态的窗口

相反,将搜索类修改为

  • 以webdriver的现有实例为例
  • 使用脚本
    窗口打开一个新窗口。打开
    而不是
    get
  • 在BasicBot类中,修改最后一行以传递驱动程序:

    class Search:
        def __init__(self, driver):
            self.driver = driver
            # Open a new window
            self.driver.execute_script("window.open('https://site2')")
            sleep(2) # visually check to make sure it opened
    
            # Switch to new window
            self.driver.switch_to.window(self.driver.window_handles[-1])
            # After this you can start doing self.driver.find_element_by_xpath and the rest
    
    
    最后,您可以使用“刷新”来刷新站点2:

    Search(self.driver)
    
    希望有帮助。祝你好运

    Search(self.driver)
    
    else:
        print("Not match")
        sleep(20)
        self.driver.refresh()