簡體   English   中英

OSError: [WinError 6] 通過 Python 使用 Selenium 時出現錯誤的描述符錯誤

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

你能幫我寫代碼嗎? 我想解析電話號碼,但我需要點擊激活按鈕。 但是這個按鈕有一個標簽,這對我來說是個問題。 我該如何解決?

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

這個錯誤信息...

  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 中的討論:在通過 Python PyDev (Eclipse) unittest 模塊調用 close() 時觀察到“OSError: [WinError 6] The handle is invalid”這個問題即使在通過Python 的模塊調用self.driver.close()時也觀察到。

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)

解決方案

該解決方案是從Alsodefine stdin or it will crash on Python + cx_Freeze: WindowsErro... pull request 中合並的,並且Selenium v3.8.1可用

理想情況下,您需要確保:

  • Selenium升級到當前級別Version 3.141.59
  • ChromeDriver更新到當前ChromeDriver v80.0級別。
  • Chrome已更新到當前的Chrome 版本 80.0級別。 (根據ChromeDriver v80.0 發行說明
  • 通過IDE清理項目工作區並僅使用所需的依賴項重建項目。
  • 始終在tearDown(){}方法中調用driver.quit()以優雅地關閉和銷毀WebDriverWeb Client實例。

tl; 博士

subprocess.Popen._cleanup() 當一些舊進程消失時“句柄無效”錯誤

您的代碼看起來不錯...您的環境看起來很可疑。 如果我冒險猜測(因為它是子進程模塊在抱怨),也許 Selenium 在你的 PATH 中找不到 chrome.exe。 Chrome 是否在此異常之前打開?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM