使用 selenium 滚动网站的特定部分. [英] Scroll a specific part of a website with selenium.
问题描述
我正在尝试创建一个用于滚动此站点的脚本:http://m.1688.com/offer/39202959720.html?spm=a26g8.7664812.0.0.dfxn8I
I'm trying to create a script that will be used to scroll this site: http://m.1688.com/offer/39202959720.html?spm=a26g8.7664812.0.0.dfxn8I
正如您在网站上看到的,如果光标放在主空间内,它将能够滚动,如果它在主空间外则不会滚动.我创建了一个我想用来滚动网站的脚本:
As you can see on the site if the cursor is put inside the main space, it will be able to scroll and it wont if it's outside the main space. I created a script that i want to use to scroll the site:
driver = webdriver.Chrome('C:\Users\V\Desktop\PY\web_scrape\chromedriver.exe')
driver.get('http://m.1688.com/offer/39202959720.html?spm=a26g8.7664812.0.0.dfxn8I')
displayed = False
for i in range(5):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
假设所有相关的包都被导入,当脚本运行时,selenium 似乎没有滚动站点,我想知道我能做些什么来解决这个问题.也许有一些方法可以告诉 selenium 将光标放在特定的部分.
Assuming all the related packages are imported, when the script runs, it appears that selenium isnt scrolling the site and i would like to know what can i do to fix that. Maybe there are some ways that i can tell selenium to put the cursor in a specific part.
推荐答案
尝试使用以下代码向下滚动内容:
Try to use below code to scroll content down:
from selenium.webdriver.common.keys import Keys
driver.get('http://m.1688.com/offer/39202959720.html?spm=a26g8.7664812.0.0.dfxn8I')
# Click on element inside the main content to switch focus
driver.find_element_by_xpath('//h1[@class="d-title"]').click()
for i in range(5):
# Scroll page down
driver.find_element_by_tag_name('body').send_keys(Keys.END)
time.sleep(2)
这篇关于使用 selenium 滚动网站的特定部分.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!