WebDriver API 之操作多選的選擇列表

2021-08-20 13:00:45 字數 1704 閱讀 3391

取消所有已選項的方法:

select_element.deselect_all()
取消已選項的三種方法

select_element.deselect_by_visible_text(text)

select_element.deselect_by_index(index)

select_element.deselect_by_value(value)

html**:

桃子西瓜

橘子獼猴桃

山楂荔枝

多選項測試**:

from selenium import webdriver

# 匯入sele包

from selenium.webdriver.support.ui import select

from time import sleep

import unittest

class droplist(unittest.testcase):

def setup(self):

self.driver=webdriver.firefox()

def test_multiple_select(self):

url="file:///f:/workspace/try.html"

self.driver.get(url)

select_element=select(self.driver.find_element_by_tag_name("select"))

select_element.select_by_value("xigua")

select_element.select_by_index(0)

# select_element.first_selected_option

select_element.select_by_visible_text("荔枝")

for option in select_element.all_selected_options:

print('first-selected-->',option.text)

# 取消所有的已選中項

select_element.deselect_all()

# 重新選擇選項

select_element.select_by_value("xigua")

select_element.select_by_index(2)

select_element.select_by_visible_text("山楂")

for option in select_element.all_selected_options:

print('second-selected-->',option.text)

select_element.deselect_by_visible_text('山楂')

for option in select_element.all_selected_options:

print('third-selected-->',option.text)

def teardown(self):

self.driver.quit()

if __name__ == '__main__':

unittest.main()

WebDriver API 之操作單選下拉列表

選擇下拉列表首先需要匯入依賴包 select是個類 兩種方法 from selenium.webdriver.support.ui import select from selenium.webdriver.support.select import select第一步 例項化select類物件 s...

Webdriver API之元素定位

webdriver提供了8種元素定位方法 id name class name tag name link text partial link text xpath css selector 一 以上8種元素定位,webdriver提供兩套寫法 1.用by定位元素 通過by來宣告定位的方法,需引入b...

常用WebDriver API 的用法 6

1 操作單選框 doctype html html lang en head title 操作單選框 title head body form input type radio name fruit value berry 士多啤梨 input br input type radio name fr...