我也一直在尝试找到一种方法来滚动关注者弹出窗口或对话框,但还没有找到一种方法来关注关注者框以使用我通常在 WebDriver 中使用的滚动功能。我通过在单击关注者链接后简单地发送 ARROW_DOWN 键直到它一直向下滚动来解决这个问题,计数列表和完整列表在最后一个 ARROW_DOWN 键之后保持不变。我相信关于对话框的部分是无关紧要的,但是嗯。这是我的代码:
def listfollowers (instaURL):
actions = ActionChains(driver)
assert isinstance(instaURL, object)
driver.get(instaURL)
time.sleep(3) # Let the user actually see something!
followersbutton = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='followers']")))
followersbutton.click()
time.sleep(2)
dialoguebox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > div:nth-child(14) > div > div.zZYga > div > div.j6cq2 > ul > div")))
actions.move_to_element(dialoguebox)
actions.click()
actions.perform()
actions.reset_actions()
###Below 是您列出关注者并滚动关注者对话框所需的内容
followerlist = []
scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
while len(followerlist) < len(scrollfollowercount):
profiles = driver.find_elements_by_class_name("UYK0S") #the followers
for profile in profiles:
profileurl = profile.get_attribute('href')
followerlist.append(profileurl)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN) #included a few times for good measure
actions.perform()
actions.reset_actions()
scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
if len(scrollfollowercount) == len(followerlist):
break
print(followerlist)
所以对于我的 MAIN 部分,我有登录位,然后调用我的函数 listfollowers()
actions = ActionChains(driver)
driver.get("https://www.instagram.com/kapow.fitness/followers")
time.sleep(2)
username = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(1) > div")))
username.click()
actions.send_keys("your-username")
actions.perform()
password = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(2) > div > div.f0n8F")))
password.click()
actions.reset_actions()
actions.send_keys("your-password")
actions.send_keys(Keys.RETURN)
actions.perform()
time.sleep(2)
listfollowers("https://www.instagram.com/kapow.fitness/")