【问题标题】:How to choose a secondary hidden menu如何选择二级隐藏菜单
【发布时间】:2020-01-20 00:59:32
【问题描述】:

我想用selenium自动导出文档

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import time

# Configuration information
email = "my_email"
password = "my_password"



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(2)
    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('//*[@id="app"]/div/div/div/div[3]/div[2]/div/div/div/div/div[2]/div/a').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_id('id-7-export')  # Determine the position of the element
    actions = ActionChains(driver)
    actions.move_to_element(ele)
    actions.click(ele) # Export to html
    actions.perform()

    time.sleep(5)
    driver.close()


if __name__ == '__main__':
    work_on()

错误信息

ele = driver.find_element_by_id('id-7-export')  # Determine the position of the element

找不到标签无法导出

此代码包含账号和密码,请测试使用

自动登录可能不正确,请重试

【问题讨论】:

    标签: python selenium quip


    【解决方案1】:

    在网页中没有找到这个定位器:.find_element_by_id('id-7-export'),你可以使用xpath

    //div[@class="parts-menu-label" and text()="Export"] -> to export
    //div[@class="parts-menu-label" and text()="HTML"] -> to HTML
    

    试试下面的:

    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()
    

    【讨论】:

    • elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="parts-menu-label" and text()="Export"]"}
    • 不确定您是否已登陆test11,我已使用此定位器进行了更改:By xpath > //h1[text()="test11"]
    • hi ``` 文件 "C:/Users/bhhuan/Desktop/20190919/test.py",第 21 行,在 work_on actions = ActionChains(driver) NameError: name 'ActionChains' is not defined ```
    • 以下导入:from selenium.webdriver import ActionChains
    • 欢迎,很高兴它成功了,顺便说一句,我建议你可以使用WebDriverWait 而不是time.sleep(...),它会让你的项目更有效率。
    猜你喜欢
    • 2015-11-17
    • 2013-06-29
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多