-1

find_element_by_xpath()to发生了变化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
4

1 回答 1

0

查找元素()

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]")

将返回一个列表,并且列表没有click()方法。因此下一行:

search_btn.click()

引发错误:

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

解决方案

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

于 2022-02-12T21:39:13.857 回答