【问题标题】:Selenium simulate mouse wheel pull down and webdriverwait Alternative time.sleepSelenium 模拟鼠标滚轮下拉和 webdriverwait 替代 time.sleep
【发布时间】:2019-09-21 12:56:56
【问题描述】:

我正在使用俏皮办公系统

我想用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()


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

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

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

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

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


【问题讨论】:

    标签: python selenium


    【解决方案1】:

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

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

    更多信息请看这篇文章:How can I scroll a web page using selenium webdriver in python?

    【讨论】:

    • 你好,虽然没有报错,但是好像不行。
    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多