我有一个 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 键发送到哪个元素,或者是否有另一种加载帐户的方法?
任何帮助深表感谢!
Smart猫小萌
相关分类