這個
PS:我想指出我的問題僅用于個人和資訊研究目的。我是 Python 新手,現在正在學習 Selenium。我不想使用垃圾郵件,也不想在實際情況下使用腳本。尊重 Facebook 規則。正如我所說,我的問題只是出于個人研究的原因,以了解我的這種好奇心的代碼。
uj5u.com熱心網友回復:
您正在嘗試以過于復雜的方式發送給定數量的請求并跳過錯誤的請求。這可能是最簡單的方法:
- 檢查用戶是否有“添加朋友”按鈕
- 如果是,請單擊按鈕
- 如果出現錯誤框,請將其關閉,否則將發送的請求數加 1
- 如果是,請單擊按鈕
- 如果發送了所需數量的請求,則停止,否則轉到下一個用戶
.
requests_to_send = 20
requests_sent = 0
requests_failed = 0
i = 0 # number of users checked
users = []
while requests_sent < requests_to_send:
# if necessary, wait until new users are loaded
while i >= len(users):
users = driver.find_elements(By.CSS_SELECTOR, 'div[aria-label=Reactions] div:nth-child(3) div[data-visualcompletion=ignore-dynamic]')
time.sleep(.5)
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', users[i])
button = users[i].find_elements(By.CSS_SELECTOR, 'div[aria-label="Add Friend"]')
if button:
button[0].click()
time.sleep(2)
cant_send = driver.find_elements(By.CSS_SELECTOR, 'div[aria-label=OK]')
if cant_send:
requests_failed = 1
cant_send[0].click()
# wait until popup is closed
while driver.find_elements(By.CSS_SELECTOR, 'div[aria-label=OK]'):
time.sleep(.5)
else:
requests_sent = 1
i = 1
print(f'users checked {i} - req. sent {requests_sent} - req. failed {requests_failed}', end='\r')
ps fanne buono uso!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480785.html
標籤:Python python-3.x 硒 循环 硒网络驱动程序
