在UI自动化测试中,有很过的类可以调用,比如下拉框的操作可选择select;

在实际调用中,下拉框选择有三种方式:1、按照索引2、按照value3、按照文本

HTML的源码信息如下:

复制代码
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8     <center>
 9         <p>
10             账户:<input id="login" type="text" placeholder="请输入账户">
11         </p>
12         <p>
13             密码:<input id="login" type="password" placeholder="请输入密码">
14         </p>
15         <p>
16             登录:<input id="login" type="button">
17         </p>
18 
19         <a href="http://www.baidu.com">百度</a>
20     </center>
21 </body>
22 </html>
复制代码

下拉框的交互界面信息如下所示:

 

通过运行一下代码,用三种不同的方式进行下拉框操作,选择“python”。

复制代码
 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 from selenium.webdriver.support.select import Select
 4 import time as t
 5 driver=webdriver.Chrome()
 6 driver.maximize_window()
 7 driver.get("file:///D:/test/code/testDev/UI%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/select.html")
 8 selectID=driver.find_element(By.ID,"name")
 9 #首先对类实例化
10 select=Select(selectID)
11 t.sleep(3)
12 #按照索引方式
13 select.select_by_index(1)
14 #按照value方式
15 # select.select_by_value("python")
16 #按照文本方式
17 # select.select_by_visible_text("python语言")
18 t.sleep(3)
19 driver.quit()
复制代码

下拉框的实战:

复制代码
 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 from selenium.webdriver.support.select import Select
 4 import time as t
 5 driver=webdriver.Chrome()
 6 driver.maximize_window()
 7 driver.get("https://sh.zu.anjuke.com/")
 8 t.sleep(3)
 9 driver.find_element(By.LINK_TEXT,"租房").click()
10 t.sleep(3)
11 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[1]/span[2]/div/a[2]").click()
12 t.sleep(2)
13 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[1]/span[2]/a[2]").click()
14 t.sleep(2)
15 driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/span[2]/div/a[7]').click()
16 t.sleep(2)
17 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[1]/span[2]/div/a[7]").click()
18 t.sleep(2)
19 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[3]/span[2]/a[2]").click()
20 t.sleep(2)
21 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[2]/span[2]/a[3]").click()
22 t.sleep(2)
23 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[4]/span[2]/a[2]").click()
24 t.sleep(2)
25 driver.find_element(By.XPATH,"/html/body/div[5]/div[2]/div[4]/span[2]/a[2]").click()
26 t.sleep(2)
27 driver.find_element(By.ID,"condhouseage_txt_id").click()
28 t.sleep(2)
29 driver.find_element(By.XPATH,'//*[@id="condmenu"]/ul/li[2]/ul/li[3]/a').click()
30 t.sleep(2)
31 driver.find_element(By.CLASS_NAME,'filter_check').click()
32 t.sleep(2)
33 driver.find_element(By.ID,'condhouse_orient_txt_id').click()
34 t.sleep(2)
35 driver.find_element(By.XPATH,'//*[@id="condmenu"]/ul/li[3]/ul/li[3]/a').click()
36 driver.quit()
37 t.sleep(5)
38 driver.quit()
复制代码