python-selenum3 day 6 - WebDriver common API s

Keywords: Python Selenium Firefox Attribute

1. Loop through all drop-down list values
2. Radio drop-down list
3. Selection and cancellation of multi selection list
4. Operate the radio box, multi box, assert and select all
5. Assert keywords in page source code
6. screenshots
7. Drag page elements

1. Loop through all drop-down list values

   <!--Practicing html-->
     //Education background: < select id = "ZZ" name = "SS" size = "1" >
                   <option value ="you">Kindergarten</option>
                   <option value ="xiao">Primary school</option>
                   <option value ="chu">Junior middle school</option>
                   <option value ="gao">high school</option>
                   <option value ="da">University</option>
              </select>
from selenium import webdriver
#Import select module
from selenium.webdriver.support.select import Select
driver  = webdriver.Firefox(executable_path="d:\\geckodriver")
url = "file:///d:/day8.html"
driver.get(url)
#Locate drop-down menu
select_element = driver.find_element_by_id("zz")
#Save all the content and assign it to options. Note that here are all the elements, so use elements
options = select_element.find_elements_by_tag_name("option")
#Cycle display plus printing
for option in options:
    print("Option display text:",option.text)
    print("The option values are:",option.get_attribute("value"))
    option.click()
    import time
    time.sleep(1)

2. Radio drop-down list

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")
#Locate drop-down menu
xiala = driver.find_element_by_id("zz")
#Select by serial number. The serial number starts from 0 and 2 is junior high school
Select(xiala).select_by_index(2)
#Select high school through value attribute value selection
Select(xiala).select_by_value("gao")
#Select university directly through text value selection
Select(xiala).select_by_visible_text(u"University")

3. Selection and cancellation of multi selection list

   <!--Practicing html-->
             //Education background: < select id = "ZZ" name = "SS" size = "6" multiple = "true" >
                   <option value ="you">Kindergarten</option>
                   <option value ="xiao">Primary school</option>
                   <option value ="chu">Junior middle school</option>
                   <option value ="gao">high school</option>
                   <option value ="da">University</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")
#Locate drop-down menu
xiala = driver.find_element_by_id("zz")
#Choose junior high school, senior high school and university after multiple choices
Select(xiala).select_by_index(2)
Select(xiala).select_by_value("gao")
Select(xiala).select_by_visible_text(u"University")
#Cancel the selected content (abbreviated below, just add more de than the selection, and the last one is to cancel all the selected content)
Select(xiala).deselect_by_index(2)
Select(xiala).deselect_by_value("gao")
Select(xiala).deselect_by_visible_text(u"University")
Select(xiala).deselect_all()

4. Operate the radio box, multi box, assert and select all

   <! -- HTML for practice -- >

      <! -- single choice -- > < p >
             Gender: male < input type = "radio" name = "R1" checked / >
                   Female < input type = "radio" name = "R1" / >
                   Unknown < input type = "radio" name = "R1" / >
           </p>
    <! -- mu lt iple choice
             Hobbies: Games < input type = "checkbox" name = "C1" checked / >
                   Literature and art < input type = "checkbox" name = "C2" / >
                   Sleep < input type = "checkbox" name = "C3" / >
           </p>
from selenium import webdriver
driver  = webdriver.Firefox(executable_path="E:\\geckodriver.exe")
driver.get("file:///d:/day8.html")
#For the simplest single choice and multiple choice, click the selection box directly, and then click the single choice female below.
xuanzhong = driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")
xuanzhong.click()
#Whether the assertion is selected (the choice needs to be used with the framework)
assertTrue(xuanzhong.is_selected(),u"Women are not selected")
#Select all the multi selection options at one time (be sure to note that they are elements because of multi selection at one time)
#Note: because the game is the default, the next click is to cancel the selection. The following result is to select literature and art and sleep
duoxuan = driver.find_elements_by_xpath(".//*[@type='checkbox']")
for i in duoxuan:
    i.click()

5. Assert keywords in page source code

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)
#Assert keywords in page source code
assert "WIKTK" in driver.page_source, u"The keyword does not exist in the source code of the page"

6. screenshots

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

7. Drag page elements

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")
#Positioning the first, second and third drag boxes
yi = driver.find_element_by_id("draggable")
er = driver.find_element_by_id("draggable2")
san = driver.find_element_by_id("draggable3")

#Import drag element method module
from selenium.webdriver import ActionChains

a_chains = ActionChains(driver)
#First box drag to second box
a_chains.drag_and_drop(yi, er).perform()
#Pan the third box 500
a_chains.drag_and_drop_by_offset(san,500, 0).perform()

Posted by pillot1005 on Wed, 20 Nov 2019 12:24:42 -0800