假設我有帶有三個鏈接的 html 代碼
<a href="hey123"> Whatever </a>
<a href="hey321"> Whatever </a>
<a href="hi123"> Whatever </a>
我想使用 selenium 來查找所有具有 href 標簽的元素,其中包含字串“hey”(在本例中為前兩個鏈接)。我將如何撰寫完成此任務的 python Selenium 代碼?
uj5u.com熱心網友回復:
這有效:
all_href = driver.find_elements(By.XPATH, "//*[contains(@href, 'hey')]")
print(len(all_href)
uj5u.com熱心網友回復:
這個 XPath 將完成這項作業:
"//a[contains(@href,'hey')]"
要將它與 Selenium 一起使用,您將需要一個find_elementsorfindElements方法,這取決于您與 Selenium 一起使用的語言系結。
對于 Python 中的 Selenium,這將為您提供所有此類元素的串列:
all_hey_elements = driver.find_elements(By.XPATH, "//a[contains(@href, 'hey')]")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436009.html
上一篇:如何從滑塊/幻燈片中抓取影像?
