我用最簡單的句式測試程式:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.google.com.hk//')
driver.implicitly_wait(6)
driver.find_element_by_xpath('//*[@id="gb"]/div/div[1]/div/div[2]/a').click()
但只能開啟網頁,不能點按圖片按鈕, 及回應如下:
Traceback (most recent call last):
File "C:\Users\CKH\Desktop\python\test10.py", line 6, in
driver.find_element_by_xpath('//*[@id="gb"]/div/div[1]/div/div[2]/a').click()
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
DevTools listening on ws://127.0.0.1:50407/devtools/browser/dab26d82-e39e-4486-a407-250a1c3537b3
[2436:8420:0329/194839.136:ERROR:device_event_log_impl.cc(222)] [19:48:39.140] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: 連結到系統的某個裝置失去作用。 (0x1F)
[2436:8420:0329/194839.141:ERROR:device_event_log_impl.cc(222)] [19:48:39.141] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: 連結到系統的某個裝置失去作用。 (0x1F)
請幫忙?
https://selenium-python-zh.readthedocs.io/en/latest/locating-elements.html
要補上 import By , 改用 find_element(By.XPATH...), 這樣的方式.
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')