Selenium - Python - AttributeError: 'WebDriver' オブジェクトに属性 'find_element_by_name' がありません 質問する

Selenium - Python - AttributeError: 'WebDriver' オブジェクトに属性 'find_element_by_name' がありません 質問する

Selenium を Chrome で動作させようとしているのですが、このエラー メッセージ (および同様のエラー メッセージ) が繰り返し表示されます。

AttributeError: 'WebDriver' オブジェクトに属性 'find_element_by_name' がありません

、などでも同じ問題が発生しますfind_element_by_id()find_element_by_class()

私も電話できませんでしたsend_keys()

私は提供されているテストコードを実行しているだけですChromeDriver - Chrome 用 WebDriver - はじめに

import time

from selenium import webdriver

driver = webdriver.Chrome("C:/Program Files/Chrome Driver/chromedriver.exe")  # Path to where I installed the web driver

driver.get('http://www.google.com/');

time.sleep(5) # Let the user actually see something!

search_box = driver.find_element_by_name('q')

search_box.send_keys('ChromeDriver')

search_box.submit()

time.sleep(5) # Let the user actually see something!

driver.quit()

私はGoogle Chromeバージョン103.0.5060.53を使用しており、ChromeDriver 103.0.5060.53を以下からダウンロードしました。ダウンロード

コードを実行すると、Chrome が開いて google.com に移動しますが、次の出力が表示されます。

C:\Users\Admin\Programming Projects\Python Projects\Clock In\clock_in.py:21: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome("C:/Program Files/Chrome Driver/chromedriver.exe")  # Optional argument, if not specified will search path.

DevTools listening on ws://127.0.0.1:58397/devtools/browser/edee940d-61e0-4cc3-89e1-2aa08ab16432
[9556:21748:0627/083741.135:ERROR:device_event_log_impl.cc(214)] [08:37:41.131] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2)
[9556:21748:0627/083741.149:ERROR:device_event_log_impl.cc(214)] [08:37:41.148] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.156:ERROR:device_event_log_impl.cc(214)] [08:37:41.155] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.157:ERROR:device_event_log_impl.cc(214)] [08:37:41.156] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[9556:21748:0627/083741.157:ERROR:device_event_log_impl.cc(214)] [08:37:41.156] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Traceback (most recent call last):
  File "C:\[REDACTED]", line 27, in <module>
    search_box = driver.find_element_by_name('q')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'
[21324:19948:0627/083937.892:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled, ANGLE is

注: この投稿のファイル パスを置き換えました。

DevTools のリスニング セクションはこの問題とは関係ないと思いますが、念のため含めておこうと思いました。

ベストアンサー1

Selenium はバージョン でそのメソッドを削除しました4.3.0。変更点をご覧ください:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout

次に以下を使用する必要があります:

driver.find_element("name", "q")

あなたの例では、次のようになります。

search_box = driver.find_element("name", "q")

search_box.send_keys('ChromeDriver')

search_box.submit()

信頼性を向上させるには、WebDriverWaitと組み合わせて使用​​することを検討してくださいelement_to_be_clickable

おすすめ記事