首页 > 解决方案 > Selenium 模拟鼠标滚轮下拉和 webdriverwait 替代 time.sleep

问题描述

我正在使用俏皮话办公系统

想用selenium导出所有文档,遇到很多问题。

1)等待时间,我只用time.sleep,经常有问题

2)加载一个文档,我遇到了很多需要向下滚动的文档

俏皮话测试文件

像这个文件夹包含很多文档,需要向下滚动才能获取href

3)由于是人为创建的文件夹,可能文件夹下有一个新文件夹,新文件下还有另一个文件夹。

我是新手,请尽可能告诉我。

代码中提供了测试帐户和密码。

# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
import time

# Configuration information
email = "187069474@qq.com"
password = "Huangbo1019@"


def work_on():

    driver = webdriver.Chrome('drivers/chromedriver72.exe')
    index_url = "https://quip.com/"
    driver.get(url=index_url)

    def get_docs(docs):
        for doc in docs:
            driver.get(doc)
            time.sleep(2)
            driver.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div[1]/div[1]/div[1]/div[2]/button[1]').click()  # select document
            time.sleep(2)
            ele = driver.find_element_by_xpath('//div[@class="parts-menu-label" and text()="Export"]')  # Determine the position of the element
            actions = ActionChains(driver)
            actions.move_to_element(ele).perform()
            time.sleep(2)
            html = driver.find_element_by_xpath('//div[@class="parts-menu-label" and text()="HTML"]')
            actions.move_to_element(html).click(html).perform()
            time.sleep(5)

    time.sleep(1)
    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)
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div/div[1]/div/div/div[3]/div[1]/a[2]/div/div').click()  # click file
    time.sleep(5)
    driver.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div[2]/div/div/div/div[1]/div[2]/div[1]/a').click()  # select test
    time.sleep(2)
    docs = driver.find_elements_by_class_name('folder-document-thumbnail')
    docs = [x.get_attribute('href') for x in docs]
    folders = driver.find_elements_by_class_name('folder-thumbnail')
    folders = [x.get_attribute('href') for x in folders]
    get_docs(docs)
    for folder in folders:
        driver.get(folder)
        time.sleep(2)
        docs = driver.find_elements_by_class_name('folder-document-thumbnail')
        docs = [x.get_attribute('href') for x in docs]
        get_docs(docs)

    time.sleep(5)
    driver.close()


if __name__ == '__main__':
    work_on()


当前代码只能获取二级目录文件夹。

由于无法向下滑动鼠标,无法捕获所有文档链接

等待的时间很痛苦,有时候网络不好会报错

提供的俏皮话只是测试,但在制作过程中会有成千上万的文档。

我希望谁能改进这个代码。这对我很有帮助。我真的很感激。


标签: pythonselenium

解决方案


您可以使用selenium的滚动功能。以下导致滚动到页面底部:

 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

这篇文章的更多信息:如何在 python 中使用 selenium webdriver 滚动网页?


推荐阅读