在Selenium中如何处理单选框

最近遇到了页面单选框,如何在自动化脚本中实现自动选中的问题,通过下面这个文章,掌握了这个技巧,分享给大家,请注意“get_attribute”这个的用法,对今后类似的问题,提供了一个很好的思路,看来还是要把xpaht仔细的好好学学!顺便说一下,driver.find_elements_by_tag_name,这里的tag_name,指的是html里的,<a></a>


选择框选择的方式:get_attribute
(1)根椐看到的文字来选择:
Select(driver.find_element_by_name("wl0_net_mode")).select_by_visible_text("Disabled")
(2)根据html网页的元素name来选择:
Select(driver.find_element_by_name("_wl1_channel")).select_by_value(channel5g)
这里前者直观,但是不如后者快,后者快但是需要解析html代码。默认selenium使用文字来定位元素。

另外,selenium搜索元素的时候,可以通过xpath方式来搜索,这样搜索的方式应该能够唯一的定位元素,但是如果使用inputtype来搜索的时候,可能会出现同样一个页面相同type元素的时候定位错误的问题。selenium搜索的时候不区分字符大小写,所以匹配的时候匹配到第一个。


http://seleniumhq.org/docs/
http://selenium.googlecode.com/svn/trunk/docs/api/py/selenium/selenium.selenium.html?highlight=is_checked#selenium.selenium.selenium.is_checked

whether a checkbox is checked:
        print driver.find_element_by_id("dhcpsvr").is_selected()

get value
        print driver.find_element_by_id("lan_netmask").get_attribute("value")

        print driver.find_element_by_id("dhcpsvr").get_attribute("value")


#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
#from selenium import selenium
import unittest, time, re

class Test3(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://admin:admin@10.1.1.15"
        self.verificationErrors = []
    
    def test_3(self):
        driver = self.driver
        driver.get(self.base_url + "/Wireless_Advanced.asp")

######select on wireless_advanced.asp:
        n_element = driver.find_element_by_name("wl1_nmcsidx")#find by element id

        n_select = Select(n_element)#treate the element as select.

        print "nrate:",n_element.get_attribute("value")#get element value
        print "nrate:",n_element.get_attribute("name")#get element name
        print "nrate:",n_element.tag_name#get element tag in html
        print "nrate:",n_element.text#get all the html element text of the tag.
        print n_select.first_selected_option.text #get first selected option text we can see.
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值