在 Chrome 中使用 Selenium 在 Python 中滚动不同的图层
Scrolling in a different layer in Chrome using Selenium in Python
我正在使用模块 selenium 在 python 中编写代码,我想在同一 window 的不同层上的列表上滚动。想象一下,你去 Instagram,点击关注者,然后希望向下滚动到底部,这样 selenium 就可以列出所有关注该页面的用户。
我的问题是我的代码在下面一层滚动,也就是用户的墙。
def readingFollowers(self):
self.driver.find_element_by_xpath("//a[contains(@href, '/followers')]")\
.click()
sleep(2.5)
scroll_box = self.driver.find_element_by_xpath('/html/body/div[4]/div/div[2]')
# Get scroll height
last_height = self.driver.execute_script("return arguments[0].scrollHeight", scroll_box)
while True:
# Scroll down to bottom
self.driver.execute_script("window.scrollTo(0, arguments[0].scrollHeight);", scroll_box)
# Wait to load page
sleep(1)
# Calculate new scroll height and compare with last scroll height
new_height = self.driver.execute_script("return arguments[0].scrollHeight", scroll_box)
if new_height == last_height:
break
last_height = new_height
我已经使用了 Google Chrome,检查元素在所有系统上都是相同的(很可能)。
完整代码可以评论给我,看不懂的地方。我可以给你重现情况所需的代码,以便更好地理解。
假设你已经登录了IG账号
def readingFollowers(self):
#click followers
self.driver.find_element_by_xpath('//a[@class="-nal3 "]').click()
time.sleep(5)
pop_up = driver.find_element_by_xpath('//div[@class="isgrP"]')
height = driver.execute_script("return arguments[0].scrollHeight", pop_up)
initial_height = height
#default follower count is 12
followers_count = 12
while True:
driver.execute_script("arguments[0].scrollBy(0,arguments[1])", pop_up, initial_height)
time.sleep(5)
#count loaded followers
count = len(driver.find_elements_by_xpath('//div[@class="PZuss"]/li'))
if count == followers_count:
break
followers_count = count
#add height because the list is expanding
initial_height+=initial_height
我花了一些时间,但它有效。
我正在使用模块 selenium 在 python 中编写代码,我想在同一 window 的不同层上的列表上滚动。想象一下,你去 Instagram,点击关注者,然后希望向下滚动到底部,这样 selenium 就可以列出所有关注该页面的用户。
我的问题是我的代码在下面一层滚动,也就是用户的墙。
def readingFollowers(self):
self.driver.find_element_by_xpath("//a[contains(@href, '/followers')]")\
.click()
sleep(2.5)
scroll_box = self.driver.find_element_by_xpath('/html/body/div[4]/div/div[2]')
# Get scroll height
last_height = self.driver.execute_script("return arguments[0].scrollHeight", scroll_box)
while True:
# Scroll down to bottom
self.driver.execute_script("window.scrollTo(0, arguments[0].scrollHeight);", scroll_box)
# Wait to load page
sleep(1)
# Calculate new scroll height and compare with last scroll height
new_height = self.driver.execute_script("return arguments[0].scrollHeight", scroll_box)
if new_height == last_height:
break
last_height = new_height
我已经使用了 Google Chrome,检查元素在所有系统上都是相同的(很可能)。
完整代码可以评论给我,看不懂的地方。我可以给你重现情况所需的代码,以便更好地理解。
假设你已经登录了IG账号
def readingFollowers(self):
#click followers
self.driver.find_element_by_xpath('//a[@class="-nal3 "]').click()
time.sleep(5)
pop_up = driver.find_element_by_xpath('//div[@class="isgrP"]')
height = driver.execute_script("return arguments[0].scrollHeight", pop_up)
initial_height = height
#default follower count is 12
followers_count = 12
while True:
driver.execute_script("arguments[0].scrollBy(0,arguments[1])", pop_up, initial_height)
time.sleep(5)
#count loaded followers
count = len(driver.find_elements_by_xpath('//div[@class="PZuss"]/li'))
if count == followers_count:
break
followers_count = count
#add height because the list is expanding
initial_height+=initial_height
我花了一些时间,但它有效。