Selenium Scroll inside of popup div

大城市里の小女人 提交于 2020-06-11 04:09:25

问题


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)


来源:https://stackoverflow.com/questions/38041974/selenium-scroll-inside-of-popup-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!