HTML:

我創建了一個 xpath
driver.find_element(By.XPATH('//h4[contains(text(),"This weekend")]'))
但每次我得到
TypeError: 'str' object is not callable
似乎沒有得到xpath。
可能是 xpath 似乎錯了。這里的任何人都可以指導
HTML:
<a class="css-t8q75u e19c3kf61" xpath="1"><div class="css-ekw2cu eh8fd9012"><div class="css-3ov7b7 eh8fd9011"><div class="css-95s8c1 eh8fd9010"><header class="css-1p61d40 eh8fd909">** are not unique
<a class="css-t8q75u e19c3kf61" xpath="1"><div class="css-ekw2cu eh8fd9012"><div class="css-3ov7b7 eh8fd9011"><div class="css-95s8c1 eh8fd9010"><header class="css-1p61d40 eh8fd909"><span class="css-m74hjb e19c3kf60"><span class="icon css-1ih0xvs e1ouvt3m1"><svg aria-label="CalendarWeekend" fill-rule="evenodd" clip-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="1.414" xmlns="http://www.w3.org/2000/svg" role="presentation" focusable="false" viewBox="0 0 32 32" preserveAspectRatio="xMidYMid meet" class="css-1lisf4l e1ouvt3m0"><path d="M28 14v9a5.006 5.006 0 01-4.783 4.995L23 28H9a5.006 5.006 0 01-4.995-4.783L4 23v-9h24zm-4 3h-1a1 1 0 00-1 1v1a1 1 0 001 1h1a1 1 0 001-1v-1a1 1 0 00-1-1zm-5 0h-1a1 1 0 00-1 1v1a1 1 0 001 1h1a1 1 0 001-1v-1a1 1 0 00-1-1zm3-13a1 1 0 01.993.883L23 5v1a5.006 5.006 0 014.995 4.783L28 11v1H4v-1a5.006 5.006 0 014.783-4.995L9 6V5a1 1 0 011.993-.117L11 5v1h10V5a1 1 0 011-1z"></path></svg></span></span></header><h4 class="css-11rlpdz eh8fd905">This weekend</h4><div class="css-1bu4bq eh8fd908"><h5 class="css-j3w7e9 eh8fd904">Local events taking place on Friday, Saturday and Sunday</h5></div></div></div></div></a>
uj5u.com熱心網友回復:
這不是真正的答案,而是確認 OP 的錯誤。需要明確的是,這是 selenium 4.1.0、Python 3.9.9、macOS 12.0.1
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-extensions')
with webdriver.Chrome(options=options) as driver:
driver.implicitly_wait(5)
driver.get('https://google.com')
driver.find_element(By.XPATH('//h4[contains(text(),"This weekend")]'))
顯然,我不希望找到帶有“本周末”作為文本的 h4。這不是重點。報告的錯誤是:
回溯(最近一次呼叫):檔案“/Volumes/G-DRIVE Thunderbolt 3/PythonStuff/Oct07.py”,第 9 行,在 driver.find_element(By.XPATH('//h4[contains(text(),”)這個周末")]')) 型別錯誤:'str' 物件不可呼叫
這對我來說似乎是一個錯誤
uj5u.com熱心網友回復:
在DeprecationWarning之后硒4 ...
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
find_element_by_*命令在最新的 Selenium Python 庫中已被棄用,您必須find_element()改用。
要將字符序列發送到文本欄位,您可以使用以下任一定位器策略:
您需要添加以下匯入:
from selenium.webdriver.common.by import By
使用xpath:
driver.find_element(By.XPATH, '//h4[contains(text(),"This weekend")]')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376136.html
標籤:Python 硒网络驱动程序 路径 自动化 硒铬驱动器
