python - 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()
当前代码只能获取二级目录文件夹。
由于无法向下滑动鼠标,无法捕获所有文档链接
等待的时间很痛苦,有时候网络不好会报错
提供的俏皮话只是测试,但在制作过程中会有成千上万的文档。
我希望谁能改进这个代码。这对我很有帮助。我真的很感激。
解决方案
您可以使用selenium
的滚动功能。以下导致滚动到页面底部:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
这篇文章的更多信息:如何在 python 中使用 selenium webdriver 滚动网页?
推荐阅读
- angular - 如何从字符串中获取 LatLng 对象?
- javascript - 在 Rails 中,访问控制器变量/视图/javascript 中的参数
- c++ - 动态分配的数组值重置问题
- unity3d - 在 Unity 中设置动画循环结束点
- java - 如何为多个用户隔离 Jetty HttpClient?
- javascript - 如何将 Babel Standalone 与 Flow 一起使用
- csv - 如何为 CSV 文件中的逗号分隔列表格式化 CSV 文件
- c - 如何获得准确的输出“数学错误”?
- css - 使用 CSS3 渐变时出现“未知属性值”错误
- flutter - 使两个文本字段始终对齐