嘿,大家都相信你很好,我正在嘗試按 class_name 查找元素并遍歷它們,但是,它們都有相同的class_name。
我發現它們包含不同的索引號,我正在嘗試利用它來回圈它們
元素和索引的示例:
<div class="member-2gU6Ar container-1oeRFJ clickable-28SzVr" aria-controls="popout_4188" aria-expanded="false" tabindex="-1" colorroleid="987314373729067059" index="0" role="listitem" data-list-item-id="members-987320208253394947___0">
<div class="member-2gU6Ar container-1oeRFJ clickable-28SzVr" aria-controls="popout_4184" aria-expanded="false" tabindex="-1" colorroleid="987324577870929940" index="1" role="listitem" data-list-item-id="members-987320208253394947___1">
我的代碼:
users = bot.find_elements(By.CLASS_NAME, 'member-2gU6Ar')
time.sleep(5)
try:
for user in users:
user.click()
message = bot.find_element(By.XPATH, '//body[1]/div[1]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div[5]/div[1]/input[1]')
time.sleep(5)
message.send_keys('Automated' Keys.ENTER)
except NoSuchElementException:
skip


uj5u.com熱心網友回復:
你在這里看到的類member-2gU6Ar container-1oeRFJ clickable-28SzVr不是一個類,它是多個類的組合,用空格分隔。
所以使用member-2gU6Ar不會按預期作業。
您可以洗掉空格并放置 a.以進行操作CSS selector。
div.member-2gU6Ar.container-1oeRFJ.clickable-28SzVr
我不會真的建議,因為我看到它包含字母數字字串,這可能會隨著時間而改變。
這里我寫了一個xpath:
//div[starts-with(@class,'member') and contains(@class, 'container') and @index]
這應該匹配所有具有指定屬性的 div。
您可以像這樣使用它:
users = bot.find_elements(By.XPATH, "//div[starts-with(@class,'member') and contains(@class, 'container') and @index]")
i = 1
time.sleep(5)
try:
for user in users:
ele = bot.find_element(By.XPATH, f"//div[starts-with(@class,'member') and contains(@class, 'container') and @index= '{i}']")
ele.click()
message = bot.find_element(By.XPATH, '//body[1]/div[1]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div[5]/div[1]/input[1]')
time.sleep(5)
message.send_keys('Automated' Keys.ENTER)
i = i 1
except NoSuchElementException:
skip
但是,我建議您使用相對 xpath 而不是絕對 xpath //body[1]/div[1]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div[5]/div[1]/input[1]。
希望這可以幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493898.html
標籤:Python python-3.x 硒 硒网络驱动程序 不和谐
