Web automated testing six ----- selector selection

1, in general, are the first choice in positioning elements

from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = Chrome()

driver.get('http://www.baidu.com')

setting_ele_xpath = '//a[@name="tj_settingicon" and @href="http://www.baidu.com/gaoji/preferences.html"]'

wait = WebDriverWait(driver, 20)
e = wait.until(EC.visibility_of_element_located((By.XPATH, setting_ele_xpath)))
driver.find_element_by_xpath(setting_ele_xpath).click()

locator = (By.XPATH, "//a[contains(text(), '高级搜索')]")
WebDriverWait(driver, 30).until(EC.element_to_be_clickable(locator)).the Click () 

# usually go to locate selector, go positioning option
WebDriverWait the wait = # (Driver, 20 is) 
# = my_selector wait.until (EC.element_to_be_clickable ((By.NAME, '. Ft'))) 
# my_selector.click () 

# source code has been loaded out, can go directly to click 
the wait = WebDriverWait (Driver, 20) 
my_option = wait.until (EC.element_to_be_clickable ((By.XPATH, 'the Option // [@ value = "PPT"]'))) 
my_option.click ()

2, using the selector selector

from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = Chrome()

driver.get('http://www.baidu.com')

def wait_clicl_element(driver, locator):
    wait = WebDriverWait(driver, 20)
    return wait.until(EC.element_to_be_clickable(locator))

setting_ele_xpath = '//a[@name="tj_settingicon" and @href="http://www.baidu.com/gaoji/preferences.html"]'

wait = WebDriverWait(driver, 20)
e = wait.until(EC.visibility_of_element_located((By.XPATH, setting_ele_xpath)))
driver.find_element_by_xpath (setting_ele_xpath) .click () 

Locator = (By.XPATH, "A // [the contains (text (), 'Advanced Search')]") 
WebDriverWait (Driver, 30) .until (EC.element_to_be_clickable (Locator .)) the Click () 

the wait = WebDriverWait (Driver, 20 is) 
my_selector = wait.until (EC.element_to_be_clickable ((By.NAME, '. ft'))) 
my_selector.click () 

# sELECT select three ways: index value , value value, text, point into the source code to see 
selector_obj = the Select (my_selector) 
selector_obj.select_by_value ( 'PDF') 
# cancel selector_obj.deselect_by_value () 

Print (selector_obj.options) 
Print (selector_obj.first_selected_option)

  

Guess you like

Origin www.cnblogs.com/qyh0902/p/11223216.html