下拉選擇
from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.get("https://www.xxxxx.com/") sleep(2) driver.find_elements_by_tag_name('option')[2].click() # 通過標簽名定位到 option 標簽,選擇第三個,第一個下標為 0 driver.find_element_by_css_selector("[value='3']").click() # 通過 css 定位屬性定位
通過Select類定位
# 通過Select類定位 from selenium import webdriver from selenium.webdriver.support.ui import Select from time import sleep driver = webdriver.Chrome() driver.get("http:\\www.xxxx.com") select = Select(driver.find_element_by_css_selector("[name='CookieDate']")) # 定位到所有的選項列表 select.select_by_index('1') # 根據索引定位,從 0 開始 sleep(2) select.select_by_visible_text("一年") # 根據看的見的文本定位 select.select_by_value('3') # 根據 value 值定位 sleep(2) driver.quit()
栗子;
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>下拉框</title> </head> <body> <select name="fruit" size="1"> <option id="peach" value="taozi">桃子</option> <option id="watermelon" value="xigua">西瓜</option> <option id="orange" value="juzi">橘子</option> <option id="kiwifruit" value="mihoutao">獼猴桃</option> <option id="matbush" value="shanzha">山楂</option> <option id="litchi" value="lizhi">荔枝</option> </select> </body> </html>
遍歷所有選項並打印選項顯示的文本和選項值
from selenium import webdriver import unittest import time class Test_SelectText(unittest.TestCase): def test_getSelectText(self): url = '01.html' self.driver = webdriver.Chrome() self.driver.get(url) # 找到下拉框 select = self.driver.find_element_by_name('fruit') # 找到所有的option 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(2) test1 = Test_SelectText() test1.test_getSelectText()
結果:
選項顯示的文本: 桃子
選項值為: taozi
選項顯示的文本: 西瓜
選項值為: xigua
選項顯示的文本: 橘子
選項值為: juzi
選項顯示的文本: 獼猴桃
選項值為: mihoutao
選項顯示的文本: 山楂
選項值為: shanzha
選項顯示的文本: 荔枝
選項值為: lizhi
通過索引定位
from selenium import webdriver import unittest from selenium.webdriver.support.ui import Select class Test_SelectText(unittest.TestCase): def test_getSelectText(self): url = '01.html' self.driver = webdriver.Chrome() self.driver.get(url) # 使用xpath定位方式獲取select頁面元素對象 select_element = Select(self.driver.find_element_by_xpath('//select')) # 打印默認選中項的文本 print(select_element.first_selected_option.text) # 獲取所有選擇項的頁面元素對象 all_options = select_element.options # 打印選項總個數 print(len(all_options)) if all_options[1].is_enabled() and not all_options[1].is_selected(): # 通過序號選中第二個元素,序號從0開始 select_element.select_by_index(1) # 打印已選中的文本 txt = select_element.all_selected_options[0].text print(txt) # 斷言當前選中的文本是否是西瓜 self.assertEqual(txt, '西瓜') test1 = Test_SelectText() test1.test_getSelectText()
結果:
桃子 6 西瓜
根據文本獲取
from selenium import webdriver import unittest from selenium.webdriver.support.ui import Select class Test_SelectText(unittest.TestCase): def test_getSelectText(self): url = '01.html' self.driver = webdriver.Chrome() self.driver.get(url) # 使用xpath定位方式獲取select頁面元素對象 select_element = Select(self.driver.find_element_by_xpath('//select')) # 打印默認選中項的文本 print(select_element.first_selected_option.text) # 獲取所有選擇項的頁面元素對象 all_options = select_element.options # 打印選項總個數 print(len(all_options)) select_element.select_by_visible_text('獼猴桃') txt = select_element.all_selected_options[0].text print(txt) # 斷言當前選中的文本是否是獼猴桃 self.assertEqual(txt, '獼猴桃') test1 = Test_SelectText() test1.test_getSelectText()
結果:
桃子 6 獼猴桃
根據value
from selenium import webdriver import unittest import time from selenium.webdriver.support.ui import Select class Test_SelectText(unittest.TestCase): def test_getSelectText(self): url = '01.html' self.driver = webdriver.Chrome() self.driver.get(url) # 使用xpath定位方式獲取select頁面元素對象 select_element = Select(self.driver.find_element_by_xpath('//select')) # 打印默認選中項的文本 print(select_element.first_selected_option.text) # 獲取所有選擇項的頁面元素對象 all_options = select_element.options # 打印選項總個數 print(len(all_options)) select_element.select_by_value('shanzha') txt = select_element.all_selected_options[0].text print(txt) # 斷言當前選中的文本是否是山楂 self.assertEqual(txt, '山楂') test1 = Test_SelectText() test1.test_getSelectText()
select_element.all_selected_options屬性獲取的是所有被選中項的對象組成的列表對象