Brandon Jacobson
Brandon Jacobson

Reputation: 159

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

I'm trying to log into my Fidelity account, using Selenium, and Fidelity. I've made sure that I'm using the correct webdriver (version 78 for my Chrome version 78). I think this has to do with the only Chrome webdriver is 32-bit and I'm using 64-bit. This is the last error I'm getting. It opens the webpage, put my user name and password, and then I think on the button click it crashes or right before it.

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")

Here's the error I'm getting.

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

I'm using Pycharm and Selenium.

Upvotes: 1

Views: 4384

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193108

To send character sequence to the Usename and Password field you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using 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()
    
  • Using 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()
    
  • Note : You have to add the following imports :

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

Upvotes: 1

Related Questions