python-selenum3第六天——WebDriver常用API(二)

1.循環(huán)遍歷所有的下拉列表值
2.單選下拉列表
3.多選擇列表的選中與取消
4.操作單選框、多選框以及斷言及全部選中
5.斷言頁面源碼中的關(guān)鍵字
6.截屏
7.拖拽頁面元素

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出中衛(wèi)免費(fèi)做網(wǎng)站回饋大家。


1. 循環(huán)遍歷所有的下拉列表值

   <!--練習(xí)的html-->
     學(xué)歷:<select id="zz" name="ss" size="1">
                   <option value ="you">幼兒園</option>
                   <option value ="xiao">小學(xué)</option>
                   <option value ="chu">初中</option>
                   <option value ="gao">高中</option>
                   <option value ="da">大學(xué)</option>
              </select>
from selenium import webdriver
#導(dǎo)入select模塊
from selenium.webdriver.support.select import Select
driver  = webdriver.Firefox(executable_path="d:\\geckodriver")
url = "file:///d:/day8.html"
driver.get(url)
#定位下拉菜單
select_element = driver.find_element_by_id("zz")
#將所有的內(nèi)容保存并賦值給options   注意這里是所有元素 所以用elements
options = select_element.find_elements_by_tag_name("option")
#循環(huán)顯示加打印
for option in options:
    print("選項(xiàng)顯示的文本:",option.text)
    print("選項(xiàng)值為:",option.get_attribute("value"))
    option.click()
    import time
    time.sleep(1)

2.單選下拉列表

from selenium import webdriver
from selenium.webdriver.support.select import Select
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("file:///d:/day8.html")
#定位下拉菜單
xiala = driver.find_element_by_id("zz")
#通過序號(hào)選擇,序號(hào)從0開始,2為初中
Select(xiala).select_by_index(2)
#通過value屬性值選擇,選擇高中
Select(xiala).select_by_value("gao")
#通過文本值選擇,直接選擇大學(xué)
Select(xiala).select_by_visible_text(u"大學(xué)")

3.多選擇列表的選中與取消

   <!--練習(xí)的html-->
             學(xué)歷:<select id="zz" name="ss" size="6" multiple="true">
                   <option value ="you">幼兒園</option>
                   <option value ="xiao">小學(xué)</option>
                   <option value ="chu">初中</option>
                   <option value ="gao">高中</option>
                   <option value ="da">大學(xué)</option>
              </select>
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("file:///d:/day8.html")
#定位下拉菜單
xiala = driver.find_element_by_id("zz")
#多選后為選擇初中、高中、大學(xué)
Select(xiala).select_by_index(2)
Select(xiala).select_by_value("gao")
Select(xiala).select_by_visible_text(u"大學(xué)")
#取消已經(jīng)選擇的內(nèi)容(下面簡寫了,比選擇多加了個(gè)de而已,最后一個(gè)是取消所有已經(jīng)選中)
Select(xiala).deselect_by_index(2)
Select(xiala).deselect_by_value("gao")
Select(xiala).deselect_by_visible_text(u"大學(xué)")
Select(xiala).deselect_all()

4.操作單選框、多選框以及斷言及全部選中

   <!--練習(xí)的html-->

      <!--單選--> <p>
             性別:男<input type="radio" name="r1" checked/>
                   女<input type="radio" name="r1" />
                   不明<input type="radio" name="r1" />
           </p>
    <!--多選-->  <p>
             愛好:游戲<input type="checkbox" name="c1" checked/>
                   文藝<input type="checkbox" name="c2" />
                   睡覺<input type="checkbox" name="c3" />
           </p>
from selenium import webdriver
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("file:///d:/day8.html")
#最簡單的單選和多選,直接點(diǎn)擊選擇框即可 下面為單選女的選擇點(diǎn)擊
xuanzhong = driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")
xuanzhong.click()
#斷言是否被選中(選擇需要配合框架使用)
assertTrue(xuanzhong.is_selected(),u"女沒有被選中")
#一次性將所有的多選選項(xiàng)全部選擇(一定要注意因?yàn)橐淮涡远噙x所以是elements)
#注意:因?yàn)橛螒蚴悄J(rèn),所以在次點(diǎn)擊等于取消了選擇,下面結(jié)果為選中文藝和睡覺
duoxuan = driver.find_elements_by_xpath(".//*[@type='checkbox']")
for i in duoxuan:
    i.click()

5.斷言頁面源碼中的關(guān)鍵字

from selenium import webdriver
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("WIKTK")
driver.find_element_by_id("su").click()
import time
time.sleep(4)
#斷言頁面源碼中的關(guān)鍵字
assert "WIKTK" in driver.page_source, u"頁面中源碼中不存在該關(guān)鍵字"

6.截屏

from selenium import webdriver
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("https://www.baidu.com")
driver.save_screenshot(r"d:\截圖.png")

7.拖拽頁面元素

http://jqueryui.com/resources/demos/draggable/scroll.html
from selenium import webdriver
driver = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("http://jqueryui.com/resources/demos/draggable/scroll.html")
#定位第一、第二、第三拖動(dòng)框體
yi = driver.find_element_by_id("draggable")
er = driver.find_element_by_id("draggable2")
san = driver.find_element_by_id("draggable3")

#導(dǎo)入拖拽元素方法模塊
from selenium.webdriver import ActionChains

a_chains = ActionChains(driver)
#第一個(gè)框拖動(dòng)到第二個(gè)框
a_chains.drag_and_drop(yi, er).perform()
#將第三個(gè)框平移500
a_chains.drag_and_drop_by_offset(san,500, 0).perform()

當(dāng)前題目:python-selenum3第六天——WebDriver常用API(二)
本文網(wǎng)址:http://www.muchs.cn/article24/ghgsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、用戶體驗(yàn)、定制網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司