如何使用 Selenium 在 Instagram 弹出框架中向下滚动
How to scroll down in an instagram pop-up frame with Selenium
我有一个 python 脚本使用 selenium 转到给定的 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 键发送到哪个元素,或者是否有另一种方法来加载帐户?
非常感谢任何帮助!
您正在查找的元素是 //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()
如果您添加带范围的迭代 (for),以上代码可以正常工作
对于我在范围 (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 循环也可以使滚动成为可能
我有一个 python 脚本使用 selenium 转到给定的 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 键发送到哪个元素,或者是否有另一种方法来加载帐户?
非常感谢任何帮助!
您正在查找的元素是 //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()
如果您添加带范围的迭代 (for),以上代码可以正常工作 对于我在范围 (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 循环也可以使滚动成为可能