[英]How to click multiple buttons with same class using Appium with Python
我正在嘗試創建一個機器人,它會自動取消關注 Depop 上所有當前關注的用戶。
只要取消關注已關注用戶列表中的第一個用戶,我就可以得到它。
問題是,所有取消關注按鈕都使用與文本“關注”相同的類
我創建了一個 while 循環,我希望它可以單擊當前顯示的所有包含文本“Following”的按鈕。 然而; 它只會“點擊”第一個按鈕。
unfollow = 'yes'
while unfollow == 'yes':
if driver.find_element_by_class_name('android.widget.Button'):
followBtn = driver.find_element_by_class_name('android.widget.Button')
btnText = followBtn.text
if btnText == 'Following':
followBtn.click()
else:
unfollow = 'no'
您應該使用driver.find_elements()
並將所有元素存儲在字典中並迭代它們。 最后,再次檢查是否存在任何按鈕,您也不能使用 driver.find() 作為條件,您應該使用 try/except 塊來達到此目的。
所以這就是答案:
unfollow = 'yes'
while unfollow == 'yes':
try:
driver.find_element_by_class_name('android.widget.Button')
except:
return
elements = driver.find_elements_by_class_name('android.widget.Button')
for el in elements:
btnText = el.text
if btnText == 'Following':
el.click()
else:
unfollow = 'no'
return
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.