Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

python - How to iterate button links in selenium

background:I want to export all document in quip.

I provided the account and password in the code for testing.

quip

But now I can only access one file to be exported.

I want to automate these exports.

# -*- 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 = "[email protected]"
password = "Huangbo1019@"



def work_on():
    driver = webdriver.Chrome()
    index_url = "https://quip.com/"
    driver.get(url=index_url)
    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)
    driver.find_element_by_xpath('//h1[text()="test11"]').click()  # select test
    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(1)
    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)
    driver.close()


if __name__ == '__main__':
    work_on()

Automatic login may be incorrect, please try again.

This code can only access one document and export.

I don't know how to iterate over all document and export.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATED

Now works for subfolders too:

# -*- 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 = "[email protected]"
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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...