【问题标题】:How to scroll down in an instagram pop-up frame with Selenium如何使用 Selenium 在 Instagram 弹出框中向下滚动
【发布时间】:2019-01-13 21:59:31
【问题描述】:

我有一个使用 selenium 的 python 脚本来访问给定的 Instagram 个人资料并遍历用户的关注者。在 instagram 网站上,单击查看关注者列表时,会打开一个弹出窗口,其中列出了列出的帐户(这里是 a screenshot of the site

但是,无论是在视觉上还是在 html 中,都只显示了 12 个帐户。为了查看更多内容,必须向下滚动,所以我尝试使用 Keys.PAGE_DOWN 输入来执行此操作。

from selenium import webdriver
from selenium.common.exceptions         import TimeoutException
from selenium.webdriver.support.ui      import WebDriverWait 
from selenium.webdriver.support         import expected_conditions as EC
from selenium.webdriver.chrome.options  import Options
from selenium.webdriver.common.keys     import Keys
import time 

...

username = 'Username'
password = 'Password'
message  = 'blahblah'
tryTime  = 2

#create driver and log in
driver = webdriver.Chrome()
logIn(driver, username, password, tryTime)

#gets rid of preference pop-up
a = driver.find_elements_by_class_name("HoLwm")
a[0].click()

#go to profile
driver.get("https://www.instagram.com/{}/".format(username))

#go to followers list
followers = driver.find_element_by_xpath("//a[@href='/{}/followers/']".format(username))
followers.click()
time.sleep(tryTime) 

#find all li elements in list
fBody  = driver.find_element_by_xpath("//div[@role='dialog']")
fBody.send_keys(Keys.PAGE_DOWN) 

fList  = fBody.find_elements_by_tag("li")
print("fList len is {}".format(len(fList)))

time.sleep(tryTime)

print("ended")
driver.quit()

当我尝试运行它时,我收到以下错误:

Message: unknown error: cannot focus element

我知道这可能是因为我为 fBody 使用了错误的元素,但我不知道哪个是正确的。有谁知道我应该将 PAGE_DOWN 键发送到哪个元素,或者是否有另一种加载帐户的方法?

非常感谢任何帮助!

【问题讨论】:

    标签: python selenium instagram webautomation


    【解决方案1】:

    您正在寻找的元素是 //div[@class='isgrP']Keys.PAGE_DOWN 不适用于可滚动的 div。

    您的变量fList 保持旧值,您需要在滚动后再次查找元素。

    #find all li elements in list
    fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")
    scroll = 0
    while scroll < 5: # scroll 5 times
        driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
        time.sleep(tryTime)
        scroll += 1
    
    fList  = driver.find_elements_by_xpath("//div[@class='isgrP']//li")
    print("fList len is {}".format(len(fList)))
    
    print("ended")
    #driver.quit()
    

    【讨论】:

    • 非常感谢,这正是我所需要的!
    【解决方案2】:

    如果您添加带有范围的迭代(for),上面的代码可以正常工作 对于范围内的 i (1, 4): 试试:

                #find all li elements in list
                fBody  = self.driver.find_element_by_xpath("//div[@class='isgrP']")
                scroll = 0
                while scroll < 5: # scroll 5 times
                    self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
                    time.sleep(2)
                    scroll += 1
    
                fList  = self.driver.find_elements_by_xpath("//div[@class='isgrP']//li")
                print("fList len is {}".format(len(fList)))
    
            except Exception as e:
                print(e, "canot scrol")
    
            try:
                #get tags with a
                hrefs_in_view = self.driver.find_elements_by_tag_name('a')
                # finding relevant hrefs
                hrefs_in_view = [elem.get_attribute('title') for elem in hrefs_in_view]
    
                [pic_hrefs.append(title) for title in hrefs_in_view if title not in pic_hrefs]
                print("Check: pic href length " + str(len(pic_hrefs)))
    
            except Exception as tag:
                print(tag, "can not find tag")
    

    因此,即使 while 循环未命中,for 循环也可以进行滚动

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2019-03-29
      相关资源
      最近更新 更多