1、首先自己写一个包含下拉项的简单页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下拉列表使用</title>
</head>
<body>
<form action="javascript:alert('test')">
provice:
<select name="provice" id="provice"multiple>
<option value="bj">beijing</option>
<option value="sh">shanghai</option>
<option value="tj">tianjin</option>
<option value="gs">gansu</option>
</select>
</form>
</body>
</html>
2、把实现下拉框被选中的方法封装在一个类中:代码如下
from selenium import webdriver
import os
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
class TestCase1:
def __init__(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
path = os.path.dirname(os.path.abspath(__file__))
file_path = "file:///" + path + '/forms3.html'
self.driver.get(file_path)
def test_select(self):
se = self.driver.find_element(By.ID,value="provice")
select = Select(se)
select.select_by_value('bj')
sleep(2)
select.select_by_index(2)
sleep(2)
select.select_by_visible_text('shanghai')
sleep(3)
select.deselect_all()
for i in range(4):
select.select_by_index(i)
sleep(2)
for option in select.options:
print(option.text)
sleep(3)
self.driver.quit()
if __name__ == '__main__':
case = TestCase1()
case.test_select()
|