How can I scroll a web page using selenium webdriver in python, for twitch?
How can I scroll a web page using selenium webdriver in python, for twitch?
I want to scroll vertical using Selenium.
I have read all the existing answers but non of them is working for the link
https://www.twitch.tv/directory/all
Kindly guide what is it about this page that makes the code
driver.execute_script("window.scrollTo(0, 1080);")
driver.execute_script("window.scrollTo(0, 1080);")
to have no effect at all.
Here is the complete code:
from time import sleep
from selenium import webdriver
driver = webdriver.Chrome('/home/sohaib/Desktop/chromedriver')
driver.get('https://www.twitch.tv/directory/all')
sleep(10)
driver.execute_script("window.scrollTo(0, 1080);")
driver.quit()`
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.
– DebanjanB
Sep 5 '18 at 5:54
@IshitaShah, I want to scroll vertical.
– Sohaib Aslam Sameja
Sep 5 '18 at 6:20
@DebanjanB, I am looking for the approach that can be used to scroll a page using Selenium. Many have suggested to use driver.execute_script("window.scrollTo(0, 1080);") which is not working for link I have provided in the question, I am not sure if that has something to do with the structure of the page.
– Sohaib Aslam Sameja
Sep 5 '18 at 6:21
@SohaibAslam What happens when you use that?
– DebanjanB
Sep 5 '18 at 6:22
2 Answers
2
Try this for vertical scroll,
from selenium.webdriver.common.keys import Keys
# Selects the first preview card
card = driver.find_element_by_xpath('//a[@data-a-target="preview-card-title-link"]')
card.send_keys(Keys.END) # Add a while loop to do infinite scroll
This is not working, and I am not sure why is would work?
– Sohaib Aslam Sameja
Sep 5 '18 at 6:31
I just tested it, it works fine, its doing vertical scroll, are you getting any error?
– Stack
Sep 5 '18 at 6:31
Yes it worked, I must have tested it wrong earlier. Thanks a lot.
– Sohaib Aslam Sameja
Sep 5 '18 at 6:37
upvote pls.. :)
– Stack
Sep 5 '18 at 6:38
How would I stop the iteration in while loop. How can I detect the end of page, any ideas on that?
– Sohaib Aslam Sameja
Sep 5 '18 at 6:40
If you wants to use vertical scroll, You can use window.scrollBy(0,200) too,
driver.execute_script("window.scrollBy(0, 1080)")
You may try with JavaScriptExecutor as well,
js.execute_Script("window.scrollBy(0,1080)")
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You wants to scroll Horizontal or Vertical ?
– Ishita Shah
Sep 5 '18 at 5:51