Python+selenium之獲取文本值和下拉框選擇數據


Python+selenium之獲取文本值和下拉框選擇數據

一、結合實例進行描述

1. 實例如下所示:

 #新增標簽操作
    def func_labels(self):
        self.driver.find_element_by_xpath("//*[@class='menu-text'][text()='業務管理']").click()
        time.sleep(1)
        self.driver.find_element_by_xpath("//*[@class='menu-text'][text()='打標簽']").click()
        time.sleep(1)
        self.driver.find_element_by_id("btn-addTag").click()#點擊【新增標簽】按鈕
        time.sleep(1)
        self.driver.execute_script("$('#tag_receivingId_flexselect').blur();$('#tag_receivingId option:eq(2)').attr('selected',true).change();ecui.form.flexSelect('#tag_receivingId');")#運用js來下拉選擇數據(隱藏屬性)
        #time.sleep(1)
        self.driver.execute_script("$('#tag_senderId option:eq(1)').attr('selected',true).change()")#下拉框選中發貨方
        #time.sleep(1)
        self.driver.find_element_by_id('tag_totalCnt').send_keys(3)#填寫貨品件數
        #time.sleep(1)
        self.driver.execute_script("$('#tagDetail_prod option:eq(1)').attr('selected',true).change()")#下拉選擇“貨品信息”
        #time.sleep(1)
        self.driver.find_element_by_id('tagDetail_prodCount').send_keys(5)#填寫數量值
        #time.sleep(1)
        self.driver.find_element_by_xpath("//button[@data-bb-handler='success']").click()#點擊【保存】按鈕
        #self.driver.find_element_by_xpath("//button[@class='btn btn-purple ehkey-saveAndPrint']").click()#點擊【保存並打印】按鈕
        # < div
        # id = "hid_new_tagno"
        # style = "display: none" >
        #
        # < / div >
        ls=self.driver.execute_script('return $("#hid_new_tagno").text();')#運用JS來獲取內容值
        time.sleep(2)
        # 參考http://blog.csdn.net/eastmount/article/details/48108259
        time.sleep(2)
        self.driver.find_element_by_xpath("//span[@class='menu-text'][text()='托運單']").click()
        time.sleep(1)
        self.driver.find_element_by_xpath("//button[@id='btn-addTag']").click()
        bs=self.driver.find_element_by_id("tag_tagNo")
        bs.send_keys(ls)
        bs.send_keys(Keys.RETURN)#回車
        time.sleep(2)
        self.driver.find_element_by_xpath("//button[@class='btn btn-success ehkey-save']").click()
        self.driver.quit()  

2.上述代碼中:

①self.driver.execute_script("$('#tag_receivingId_flexselect').blur();$('#tag_receivingId option:eq(2)').attr('selected',true).change();ecui.form.flexSelect('#tag_receivingId');")#運用js來下拉選擇數據(隱藏屬性)

②在層級菜單中,一定需要加上一個延時,這里以time.sleep(2)為例,否則,會報錯

③ls=self.driver.execute_script('return $("#hid_new_tagno").text();')#使用return返回值,運用JS來獲取內容值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM