[英]Selenium in Python: Select the second element with given link text
我正在嘗試單擊網頁上包含鏈接文本“查看此文本中的所有匹配”的所有鏈接。 這是網頁上的一些html的樣子:
<a href="/searchCom.do?offset=24981670&entry=4&entries=112&area=Poetry&forward=textsCom&queryId=../session/1380145118_2069"><b>View all hits in this text</b>
<br>
</a>
[...]
<a href="/searchCom.do?offset=25280103&entry=5&entries=112&area=Poetry&forward=textsCom&queryId=../session/1380145118_2069"><b>View all hits in this text</b>
<br>
</a>
如果頁面上只有一個這樣的鏈接,我知道我可以使用以下內容點擊它:
driver.find_element_by_link_text('View all hits in this text').click()
不幸的是,此方法只識別並點擊網頁上的第一個鏈接,其中包含鏈接文本“查看此文本中的所有匹配”。 因此,我想問:是否有一種方法可以用來點擊第二個(或第n個)鏈接,鏈接文本“查看此文本中的所有匹配”? 我有一種感覺,我可能需要使用xpath,但我還沒有弄清楚我應該如何在我的腳本中實現xpath。 我會很感激任何其他人可以借出的建議。
有find_elements_by_link_text()
( docs ):
links = driver.find_elements_by_link_text('View all hits in this text')
for link in links:
link.click()
此外,您可以使用xpath獲取具有指定文本的所有鏈接:
links = driver.find_elements_by_xpath("//a[text() = 'View all hits in this text']")
for link in links:
link.click()
希望有所幫助。
試試以下代碼:
driver.find_elements_by_link_text('linktext')[1].click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.