How to fix OSError: [WinError 6] The handle is invalid with Python and Selenium?

How to fix OSError: [WinError 6] The handle is invalid with Python and Selenium?

我正在尝试使用 Selenium 和 Fidelity 登录我的 Fidelity 帐户。我已确定我使用的是正确的网络驱动程序(我的 Chrome 版本 78 是版本 78)。我认为这与唯一的 Chrome webdriver 是 32 位的而我使用的是 64 位有关。这是我遇到的最后一个错误。它打开网页,输入我的用户名和密码,然后我认为在按钮上单击它会崩溃或就在它之前。

from selenium import webdriver


def test_bot(username, password):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
    br = webdriver.Chrome(chrome_options=chrome_options)
    br.get("https://www.fidelity.com")
    br.implicitly_wait(10)

    user = br.find_element_by_id('userId-input')
    user.clear()
    user.send_keys(username)

    pwd = br.find_element_by_id('password')
    pwd.clear()
    pwd.send_keys(password)

    btn = br.find_element_by_id('fs-login-button')
    btn.click()


test_bot("MYUSERNAME", "MYPASSWORD")

这是我遇到的错误。

Exception ignored in: <function Popen.__del__ at 0x03957270>
Traceback (most recent call last):
  File "C:\Users\Notebook\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 860, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Users\Notebook\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1216, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

我正在使用 Pycharm 和 Selenium。

要将字符序列发送到用户名密码字段,您必须诱导WebDriverWait 用于 element_to_be_clickable() 并且您可以使用以下任一项 :

  • 使用CSS_SELECTOR:

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    #options.add_experimental_option("excludeSwitches", ["enable-automation"])
    #options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.fidelity.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userId-input"))).send_keys("Brandon")
    driver.find_element_by_css_selector("input#password").send_keys("Jacobson")
    driver.find_element_by_css_selector("button#fs-login-button").click()
    
  • 使用XPATH:

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    #options.add_experimental_option("excludeSwitches", ["enable-automation"])
    #options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.fidelity.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userId-input']"))).send_keys("Brandon")
    driver.find_element_by_xpath("//input[@id='password']").send_keys("Jacobson")
    driver.find_element_by_xpath("//button[@id='fs-login-button']").click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC