如何使用 Selenium 在 Instagram 弹出框架中向下滚动

我有一个 python 脚本,使用 selenium 转到给定的 Instagram 个人资料并遍历用户的关注者。在 instagram 网站上,当单击查看关注者列表时,会打开一个弹出窗口,其中列出了列出的帐户(这是该网站的屏幕截图)


然而,无论是在视觉上还是在 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 键发送到哪个元素,或者是否有另一种加载帐户的方法?


任何帮助深表感谢!


跃然一笑
浏览 206回答 2
2回答

Smart猫小萌

如果在 range(1, 4) 中为 i 的范围添加迭代 (for),则上述代码工作正常:尝试:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #find all li elements in list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fBody&nbsp; = self.driver.find_element_by_xpath("//div[@class='isgrP']")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scroll = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while scroll < 5: # scroll 5 times&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scroll += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fList&nbsp; = self.driver.find_elements_by_xpath("//div[@class='isgrP']//li")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("fList len is {}".format(len(fList)))&nbsp; &nbsp; &nbsp; &nbsp; except Exception as e:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(e, "canot scrol")&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #get tags with a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hrefs_in_view = self.driver.find_elements_by_tag_name('a')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # finding relevant hrefs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hrefs_in_view = [elem.get_attribute('title') for elem in hrefs_in_view]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [pic_hrefs.append(title) for title in hrefs_in_view if title not in pic_hrefs]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Check: pic href length " + str(len(pic_hrefs)))&nbsp; &nbsp; &nbsp; &nbsp; except Exception as tag:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(tag, "can not find tag")因此,即使 while 循环未命中,for 循环也可以滚动
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python