我正在嘗試從網站上獲取所有按鈕,但似乎 Selenium 語法已經改變,但沒有更新檔案。我正在嘗試從網站獲取按鈕,如下所示:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
url = 'https://www.programiz.com/python-programming'
driver.get(url)
buttons = driver.find_element(by=By.TAG_NAME("button"))
但是我收到以下錯誤:
TypeError: 'str' object is not callable
如前所述,檔案仍然說要使用find_element_by_tag_name折舊的。有人可以幫忙嗎。謝謝
uj5u.com熱心網友回復:
find_element_by_*命令現在已貶值。
要查找所有<button>元素,您可以使用以下定位器策略:
使用tag_name:
buttons = driver.find_elements(By.TAG_NAME, "button")使用css_selector:
buttons = driver.find_elements(By.CSS_SELECTOR, "button")使用xpath:
buttons = driver.find_elements(By.XPATH, "//button")
uj5u.com熱心網友回復:
問題在于 TAG_NAME 它只是常量而不是可呼叫的方法,檔案的新用法應該是:
driver.find_element(By.TAG_NAME, 'button')
在此處檢查檔案https://www.selenium.dev/selenium/docs/api/py/index.html#example-1
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/461989.html
