iT邦幫忙

0

Python Selenium 定位到的元素為多個時,如何取得數量

com 2020-04-28 14:37:163120 瀏覽

目標網頁

<a href="https://www.google.com" target="_blank">GOOGLE</a>
<a href="https://www.google.com" target="_blank">GOOGLE-2</a>
<a href="https://www.google.com" target="_blank">GOOGLE-3</a>
<a href="https://www.google.com" target="_blank">GOOGLE-4</a>

我的Python程式

from selenium import webdriver
import time
browser = webdriver.Chrome('./chromedriver.exe')
browser.find_elements_by_partial_link_text('GOOGLE')[n].click()
browser.close()

因為html程式中有多個符合GOOGLE前置詞
我想要點擊第n個,由於我的n是隨機產生,以目標網頁來說n需要小於4,
請問前輩
定位到的元素為多個時,如何取得元素數量

froce iT邦大師 1 級 ‧ 2020-04-28 19:45:32 檢舉
就len()啊...debug的好幫手之一
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
listennn08
iT邦高手 5 級 ‧ 2020-04-28 14:41:29
最佳解答
txtGoogleList = browser.find_elements_by_partial_link_text('GOOGLE')
txtGoogleListLength = len(txtGoogleList)
if n < txtGoogleListLength:
    txtGoogleList[n].click
com iT邦新手 4 級 ‧ 2020-04-28 14:44:21 檢舉

感謝前輩回覆~

我要發表回答

立即登入回答