Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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驱动程序?_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python 如何访问从其他文件打开的selenium驱动程序?

Python 如何访问从其他文件打开的selenium驱动程序?,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在尝试创建一个python文件,以便使用Selenium和python登录Instagram,但它不起作用 以下是目录: . │ main.py │ variables.py │ __init__.py │ ├───stuff │ │ login.py │ │ search.py │ │ startup.py │ │ __init__.py │ │ │ ├───package │ │ │ login.py │ │ │

我正在尝试创建一个python文件,以便使用Selenium和python登录Instagram,但它不起作用

以下是目录:

.
│   main.py
│   variables.py
│   __init__.py
│
├───stuff
│   │   login.py
│   │   search.py
│   │   startup.py
│   │   __init__.py
│   │
│   ├───package
│   │   │   login.py
│   │   │   __init__.py
│   │   │
│   │   └───__pycache__
│   │           login.cpython-39.pyc
│   │           search.cpython-39.pyc
│   │           __init__.cpython-39.pyc
│   │
│   └───__pycache__
│           login.cpython-39.pyc
│           startup.cpython-39.pyc
│           __init__.cpython-39.pyc
│
└───__pycache__
        variables.cpython-39.pyc
代码如下:

login.py

    from .startup import Startup
    from selenium import webdriver
    from time import sleep

    class Login(object):
        """docstring for instagramBot"""
        def __init__(self, username, password):

            self.username = username
            self.password = password
            self.startup = Startup()
            self.driver = self.startup.driver

            sleep(2)

            # send the username to the input box
            self.driver.find_element_by_xpath("//input[@name = 'username']").send_keys(username)

            # send the password to the input box
            self.driver.find_element_by_xpath("//input[@name = 'password']").send_keys(password)

            # click the log in
            self.driver.find_element_by_xpath("//div[contains(text(), 'Log In')]").click()
            sleep(2)
startup.py

from selenium import webdriver
from time import sleep

class Startup(object):

    def __init__(self):
        
        self.opts = webdriver.ChromeOptions()
        self.opts.add_experimental_option("detach", True)
        self.driver = None
        self.driver = webdriver.Chrome(options=self.opts,executable_path='C:\Program Files (x86)\chromedriver.exe')
    
        self.driver.get("https://www.instagram.com/")
基本上,startup启动chromedriver,我尝试访问login.py中的startup.driver,并为驱动程序中的HTML元素提供输入,但它只是打开了一个新的chromedriver,我不知道为什么

版本:Python 3.9.5