如何修复OSError:[WinError 6]句柄在Python和Selenium中无效?

时间:2019-11-11 01:00:01

标签: python selenium xpath css-selectors webdriverwait

我正在尝试使用Selenium和Fidelity登录我的Fidelity帐户。我确定我使用的是正确的网络驱动程序(Chrome版本78的版本为78)。我认为这与唯一的Chrome Web驱动程序是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。

1 个答案:

答案 0 :(得分:1)

要将字符序列发送到用户名密码字段,您必须为{诱发 WebDriverWait {1}},您可以使用以下任何Locator Strategies

  • 使用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()