Selenium Scroll inside of popup div

2020-07-11 08:14发布

问题:

I am using selenium and trying to scroll inside the popup div on instagram.

I get to a page like 'https://www.instagram.com/kimkardashian/', click followers, and then I can't get the followers list to scroll down.

I tried using hover, click_and_hold, and a few other tricks to select the div but none of them worked.

What would the best way be to get this selected?

This is what I tried so far:

driver.find_elements_by_xpath("//*[contains(text(), 'followers')]")[0].click()
element_to_hover_over = driver.find_elements_by_xpath("//*[contains(text(), 'Follow')]")[12]
hover = ActionChains(webdriver).move_to_element(element_to_hover_over)
hover.click_and_hold()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

回答1:

The exact code is as follow. You first have to find the new iframe which contains the name of followers:

scr1 = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div[2]')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scr1)

This will automatically scroll down the page but you have make a for loop for it until it reaches to the end of page. You can see my Instagram crawler here.



回答2:

You would need to use jQuery to execute a function on the div. Here's the way I figured to do it. It was easier to solve it with jQuery and execute a script than handle it with the api.

height = 2000
query = 'jQuery("div").filter((i, div) => jQuery(div).css("overflow-y") == "scroll")[0].scrollTop = %s' %height
driver.execute_script(query)