Selenium自動化之點擊下拉框選項操作


點擊下拉框選項

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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM