我試圖使用 selenium 創建一個簡單的 python 程式,該程式需要查找并單擊“關注”按鈕。
這是 HTML 代碼:
<div class=" qF0y9 Igw0E rBNOH YBx95 ybXk5 _4EzTm soMvl " id="f2d16737c928a4">
<button class="sqdOP L3NKy y3zKF " type="button">Abonnieren</button>
</div>
我的python代碼:
example = driver.find_element_by_xpath("//button[contains(@class,'sqdOP L3NKy y3zKF ')]")
example.click
每次我嘗試它都會給我:“selenium.common.exceptions.NoSuchElementException:訊息:沒有這樣的元素:無法定位元素”
如果您想查看該網站,您需要登錄并轉到 Instagram 建議您應該關注的人的建議頁面:https : //www.instagram.com/explore/people/
uj5u.com熱心網友回復:
您共享的 XPATH 似乎是一個隨機字串,并且不會在其他跟隨按鈕之間輕松迭代。我建議通過 HTML 頁面獲取“完整的 XPATH”,這樣您就可以進行回圈操作。有關單擊以下按鈕的回圈,請參閱以下代碼:
count=1
while True:
try:
driver.find_element_by_xpath("/html/body/div[1]/div/div/section/main/div/div[2]/div/div/div[" str(count) ")]/div[3]/button").click()
count=count 1
except:
break
uj5u.com熱心網友回復:
driver.find_element_by_xpath("//button[.='Abonnieren']").click()
只需查找帶有該文本的按鈕并單擊它即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377630.html
上一篇:正則運算式第一場比賽
