點擊下拉框選項
option.click()
import unittest
import time
from selenium import webdriver
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
# 啟動IE瀏覽器
self.driver = webdriver.Ie(executable_path="g:\IEDriverServer")
def test_printSelectText(self):
url = "http://127.0.0.1/test_select.html"
# 訪問自定義的html網頁
self.driver.get(url)
# 使用name屬性找到頁面上name屬性為“fruit”的下拉列表元素
select = self.driver.find_element_by_name("fruit")
all_options = select.find_elements_by_tag_name("option")
for option in all_options:
print("選項顯示的文本:", option.text)
print("選項值為:", option.get_attribute("value"))
option.click()
time.sleep(1)
def tearDown(self):
# 退出IE瀏覽器
self.driver.quit()
if name == 'main':
unittest.main()
只選獼猴桃,遍歷所有選項判斷
import unittest
import time
from selenium import webdriver
class VisitSogouByIE(unittest.TestCase):
def setUp(self):
# 啟動IE瀏覽器
self.driver = webdriver.Ie(executable_path="g:\IEDriverServer")
def test_printSelectText(self):
url = "http://127.0.0.1/test_select.html"
# 訪問自定義的html網頁
self.driver.get(url)
# 使用name屬性找到頁面上name屬性為“fruit”的下拉列表元素
select = self.driver.find_element_by_name("fruit")
all_options = select.find_elements_by_tag_name("option")
for option in all_options:
print("選項顯示的文本:", option.text)
print("選項值為:", option.get_attribute("value"))
if option.get_attribute("value") == 'mihoutao':
option.click()
time.sleep(1)
def tearDown(self):
# 退出IE瀏覽器
self.driver.quit()
if name == 'main':
unittest.main()