我有一个使用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密钥发送给哪个元素,或者是否有另一种加载帐户的方法吗?
非常感谢您的帮助!
答案 0 :(得分:0)
您要查找的元素为//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()
答案 1 :(得分:0)
如果您在范围内添加迭代(用于),则上述代码可以正常工作 对于范围(1,4)中的i: 尝试:
#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循环也可以使其滚动