简体   繁体   中英

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);")

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()`

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

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)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM