我想用 selenium 和 python 制作一個自動 instagram 跟隨腳本,但我有點卡住了。我已經在互聯網上搜索了所有內容,但對我來說不清楚我想輸入用戶關注者并單擊關注所有關注者。如果我已經在關注用戶,我如何讓系統向下滾動。我試過這樣但我得到了這個錯誤:元素點擊被攔截
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time, urllib.request
import requests
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome(r"F:\132\instagram\chromedriver.exe")
driver.get("https://www.instagram.com/")
sleep(4)
driver.find_element_by_xpath('//button[text()="Accept All"]')\
.click()
sleep(4)
driver.find_element_by_xpath(("//input[@name=\"username\"]"))\
.send_keys(("#"))
driver.find_element_by_xpath(("//input[@name=\"password\"]"))\
.send_keys(("#"))
driver.find_element_by_xpath('//button[@type="submit"]')\
.click()
sleep(7)
driver.find_element_by_xpath('//button[text()="Not Now"]')\
.click()
sleep(5)
driver.find_element_by_xpath('//button[text()="Not Now"]')\
.click()
#searchbox
time.sleep(5)
searchbox=driver.find_element_by_css_selector("input[placeholder='Search']")
searchbox.clear()
searchbox.send_keys("delianomad")
time.sleep(5)
searchbox.send_keys(Keys.ENTER)
time.sleep(5)
searchbox.send_keys(Keys.ENTER)
sleep(6)
driver.find_element_by_partial_link_text("followers").click()
buttons = driver.find_element_by_xpath("//*[text()='Follow']")
scroll = 0
sleep(10)
fBody = driver.find_element_by_xpath("//div[@class='isgrP']")
for btn in buttons:
if btn.text == 'Follow':
driver.execute_script("arguments[0].click();", btn)
sleep(60)
else :
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop arguments[0].offsetHeight;', fBody)
print("Sesiune terminata")
uj5u.com熱心網友回復:
這部分代碼沒有意義:
buttons = driver.find_element_by_xpath("//*[text()='Follow']")
scroll = 0
sleep(10)
fBody = driver.find_element_by_xpath("//div[@class='isgrP']")
for btn in buttons:
if btn.text == 'Follow':
driver.execute_script("arguments[0].click();", btn)
sleep(60)
else :
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop arguments[0].offsetHeight;', fBody)
請注意,首先您將獲取所有帶有“跟隨”文本的元素到buttons,然后檢查每個元素的文本是否實際上等于Follow。嘗試為這些按鈕找到不同的定位器,例如
//div[@aria-label='Followers']//ul//button
這樣,您將獲得按鈕串列(帶有和不帶有“關注”文本),然后您可以根據文本是否等于“關注”來執行您的操作。
uj5u.com熱心網友回復:
您可以使用下面給出的以下代碼滾動到元素。
public static void scrollElement(WebElement element) {
Coordinates cor = ((Locatable) element).getCoordinates();
cor.inViewPort();
pause(2);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414622.html
標籤:
