OSError: [WinError 6] Wrong descriptor error using Selenium through Python

OSError: [WinError 6] Wrong descriptor error using Selenium through Python

你能帮我写代码吗?我想解析 phone 个数字,但我需要点击激活按钮。但是这个按钮带有标签,这对我来说是个问题。我该如何解决?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
#from selenium.webdriver.common.touch_actions import TouchActions
#import org.openqa.selenium.interactions.Actions


#TouchActions.tap
def main():
    driver = webdriver.Chrome()
    remote = driver.get("https://www.olx.ua/uk/obyavlenie/68200jk71a-torpedo-pod-airbag-infiniti-g-07-14-infiniti-IDGRpUS.html#d97e6d976d;promoted")
    bt_elem = driver.find_elements_by_id("postNewAdLink")
    #print(bt_elem[0])
    #driver.find_elements_by_class_name("contact-button").click()
    #ActionChains(driver).move_to_element(bt_elem).perform().click()

    #bt_elem.get(0).click()
    #TouchActions.tap(bt_elem)

main()

错误:

Traceback (most recent call last):
  File "C:\Users\radus\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 945, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Users\radus\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1344, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] Wrong descriptor

您的代码看起来不错...您的环境看起来很可疑。如果我冒险猜测(因为它是子进程模块的抱怨),也许 Selenium 在您的 PATH 中找不到 chrome.exe。 Chrome 是否在此异常之前打开?

这个错误信息...

  File "C:\Users\radus\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 945, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Users\radus\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1344, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] Wrong descriptor

...表示 subprocess.Popen() 命令有错误。


根据 Selenium 3.5.0-GeckoDriver 0.18.0-Python 3.6.1 : "OSError: [WinError 6] The handle is invalid" is observed while close() is called through Python PyDev (Eclipse) unittest module this issue was observed even while invoking self.driver.close() through Python's 模块中的讨论。

This is because there is no stdin defined in the `service.py` file for the `subprocess.Popen()` command. Underwater the subprocess tries to create a handle which also looks for stdin under Windows this gets a bit tricky when using `Bash` or `cx_Freeze`. So, `stdin` was defined as well, and the crash is gone. Optionally you can also use:

FNULL = open(os.devnull, 'r')
subprocess.Popen(.... ,stdin=FNULL)

解决方案

该解决方案已从 Also define stdin or it will crash on Python + cx_Freeze: WindowsErro… 拉取请求中合并,并在 Selenium v3.8.1

中可用

理想情况下,您需要确保:

  • Selenium 已升级到当前级别 Version 3.141.59
  • Chrome驱动程序 已更新为当前 ChromeDriver v80.0 级别。
  • Chrome 已更新为当前 Chrome 版本 80.0 级别。 (根据 ChromeDriver v80.0 release notes
  • 通过 IDE 和 [=39] 清理 您的 项目工作区 =]重建你的项目只需要依赖。
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例.

tl;博士

subprocess.Popen._cleanup() "The handle is invalid" error when some old process is gone