Python Selenium如何向下滚动以获取所有链接
我想导出所有的文件,所以我需要所有的链接 如果鼠标未向下滚动,则不会加载所有链接 需要向下移动一点以逐渐加载Python Selenium如何向下滚动以获取所有链接,python,selenium,Python,Selenium,我想导出所有的文件,所以我需要所有的链接 如果鼠标未向下滚动,则不会加载所有链接 需要向下移动一点以逐渐加载 from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # Configuration information email = "187069474@qq.com" password = "Huangbo1019@" driver = webdriver.C
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# Configuration information
email = "187069474@qq.com"
password = "Huangbo1019@"
driver = webdriver.Chrome()
index_url = "https://testselenium.quip.com/BCWAOAUkg1v"
driver.get(url=index_url)
driver.find_element_by_xpath('//*[@id="header-nav-collapse"]/ul/li[9]/a').click() # click login
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[1]/form/div/input').send_keys(email) # input email
driver.find_element_by_xpath('//*[@id="email-submit"]').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div/div/form/div/input[2]').send_keys(password) # input password
driver.find_element_by_xpath('/html/body/div/div/form/button').click()
time.sleep(2)
可能需要新策略您可以直接使用请求库进行尝试
links = browser.find_elements_by_tag_name('a')
for link in links:
try:
requests.get(link.get_attribute('href'))
您可以使用Javascript按坐标滚动。如果可以获得页面的像素高度,可以调用
Scroll(int xCoordinate,int ycocordinate)
方法&在每次连续滚动时增加滚动坐标
您希望首先滚动到页面上的初始x,y坐标。向下滚动时,要滚动到的y坐标将发生变化。如果在向下滚动时不增加y坐标,则会反复滚动到同一位置。因此,您应该根据所使用页面的像素高度来确定增量。根据一次要滚动的行数,您的增量
变量应该大约等于您滚动经过的每一行的像素高度
numberOfScrolls
应等于要滚动的行数
方法的外观如下所示:
public void ScrollDown(int startX, int startY, int increment, int numberOfScrolls)
{
// cast webdriver to javascript executor
var executor = ((IJavaScriptExecutor) driver);
// keep track of current y coordinate
// since we are scrolling down, y coordinate will change with each scroll
var currentYCoordinate = startY;
// scroll down in a loop
for (int i = 0; i < numberOfScrolls; i++)
{
// scroll down
executor.ExecuteScript("window.scrollTo(startX, currentYCoordinate);");
// todo: implement any FindElement methods to get the new elements which have been scrolled into view.
// update y coordinate since we just scrolled down
currentYCoordinate = currentYCoordinate + increment;
}
}
public void向下滚动(int startX、int startY、int increment、int numberOfScrolls)
{
//将webdriver强制转换为javascript执行器
var executor=((IJavaScriptExecutor)驱动程序);
//跟踪当前的y坐标
//由于我们正在向下滚动,y坐标将随着每次滚动而改变
var currentYCoordinate=起始值;
//在循环中向下滚动
对于(int i=0;i
查看这篇文章,了解如何操作:您好,我测试了它,但它不起作用。我获得了多达29个链接。不是所有链接:(