【问题标题】:Scrolling the following and followers list with selenium(Python)使用 selenium (Python) 滚动以下和关注者列表
【发布时间】:2020-06-23 07:13:15
【问题描述】:

嘿,我正在尝试使用 selenium 制作一个简单的 Insta 机器人。我通过自动化达到了我的以下列表,但现在我不知道如何向下滚动我的关注者/关注者列表。我想从我的关注者和关注者列表中获取该帐户并进行比较,并列出没有关注我的帐户。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from secrets import password,username


class Instabot:
    def __init__(self,username,password):
    self.driver = webdriver.Chrome(executable_path="C:\\Users\\user\\PycharmProjects\\chromedriver_win32\\chromedriver.exe")
    self.driver.get("https://www.instagram.com")

    sleep(5)

    self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input').send_keys(username) #searching the username box and giving it username

    self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input').send_keys(password)#searching the password box and giving it password

    self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[4]/button').click() #clicking login button

    wait=WebDriverWait(self.driver,10)

    notNowButton = wait.until(
        lambda d: d.find_element_by_xpath('//button[text()="Not Now"]'))#it will click on first notnow button


    notNowButton.click()

    next_not_now=wait.until(lambda notnow:notnow.find_element_by_xpath('//button[text()="Not Now"]'))
    next_not_now.click() #it will click on second not now button

def get_unfollowers(self):
    wait = WebDriverWait(self.driver, 10)

    clickprofile= wait.until(lambda a: a.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/a'))
    clickprofile.click()

    following_list = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href,'/following')]")))
    following_list.click()#IT clicks the following and gives window of following listy
    print("CLicked following list")
   


my_bot=Instabot(username,password)
my_bot.get_unfollowers()

我看到了关于 execute_script() 但我不知道在那些括号下放什么。

好吧,我想通了。这段代码现在对我有用!

fBody = self.driver.find_element_by_css_selector("div[class='isgrP']")
    scrolling_times=(numoffollowers/4)
    scroll=0
    scroll_count = scrolling_times+5  #  You can use your own logic to scroll down till the bottom
    while scroll < scroll_count:
        self.driver.execute_script(
            'arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;',
            fBody)
        sleep(2)
        scroll += 1

【问题讨论】:

    标签: python-3.x selenium webdriver


    【解决方案1】:

    你可以使用类似的东西:

    self.driver.execute_script("window.scrollTo(0, Y)")
    

    with Y : 你想滚动多少像素。

    由你决定你必须往下走多远。

    【讨论】:

    • 我收到“selenium.common.exceptions.JavascriptException:消息:javascript 错误:Y 未定义”错误在 Y 中定义什么,我的意思是我应该在 Y 中定义什么?我必须去到最后一个帐户,所以我该怎么办?我不知道它的逻辑!
    • 你需要输入一个数字而不是 Y。比如 « window.scrollTo(0,10) »
    • 它没有用!!!还有其他想法吗?还是不可能?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多