【问题标题】:AttributeError: 'list' object has no attribute 'click' using find_element(By.Xpath) Selenium and PythonAttributeError: 'list' 对象没有属性 'click' 使用 find_element(By.Xpath) Selenium 和 Python
【发布时间】:2022-02-13 05:39:13
【问题描述】:

find_element_by_xpath() 更改为 find_element(By.Xpath),现在点击无效。

代码试验:

search_btn = self.driver.find_elements(
    By.XPATH, "//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]").click()
time.sleep(4)
search_btn.click()
time.sleep(5)

错误:

DevTools listening on ws://127.0.0.1:52534/devtools/browser/5aafde31-7b7e-456f-9639-cdfa2bb70678
[16904:17532:0211/180317.139:ERROR:chrome_browser_main_extra_parts_metrics.cc(227)] START: ReportBluetoothAvailability(). If you don't see the END: message, this is crbug.com/1216328.
[16904:17532:0211/180317.140:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] END: ReportBluetoothAvailability()
[16904:17532:0211/180317.140:ERROR:chrome_browser_main_extra_parts_metrics.cc(235)] START: GetDefaultBrowser(). If you don't see the END: message, this is crbug.com/1216328.
[16904:12228:0211/180317.149:ERROR:device_event_log_impl.cc(214)] [18:03:17.148] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[16904:12228:0211/180317.157:ERROR:device_event_log_impl.cc(214)] [18:03:17.157] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[16904:17532:0211/180317.159:ERROR:chrome_browser_main_extra_parts_metrics.cc(239)] END: GetDefaultBrowser()
Traceback (most recent call last):
  File "C:\!!!Angela\Paid screen scraper\data scraping\data scraping\my_ui_scrapy12.py", line 278, in show_page
    By.XPATH, "//*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]").click()
AttributeError: 'list' object has no attribute 'click'
 c:\data scraping\data scraping> [22108:16084:0211/180503.560:ERROR:gpu_init.cc(454)] Passthrough is not supported, GL is disabled, ANGLE is

【问题讨论】:

    标签: python-3.x selenium-webdriver attributeerror selenium4 findelement


    【解决方案1】:

    find_elements()

    find_elements() 查找元素并返回一个列表,给定 By 策略和定位器。

    所以这行代码:

    search_btn = self.driver.find_elements( By.XPATH, "//[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]")
    

    将返回一个 list 并且 list 没有 click() 那样的方法。因此下一行:

    search_btn.click()
    

    引发错误:

    AttributeError: 'list' object has no attribute 'click'
    

    解决方案

    find_elements() 替换为find_element() 就可以了。

    【讨论】:

    • 谢谢你,但我仍然收到错误
    • @VirginiaWalters 我肯定你看不到 AttributeError: 'list' object has no attribute 'click' 了。你能确认一下吗?
    • raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":" //*[@id='viewContainer']/app-listing-container/app-search-container/app-search-form-container/form/div[3]/button[2]"}(会话信息:chrome =98.0.4758.82)
    • 仍然出现点击错误消失了
    • @VirginiaWalters NoSuchElementException 是一个不同的错误,也可以解决。随时根据您的新要求提出新问题。
    猜你喜欢
    • 2018-12-20
    • 2023-01-27
    • 2012-06-28
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多