我是靠谱客的博主 单薄方盒,这篇文章主要介绍在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")


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/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()


最后

以上就是单薄方盒最近收集整理的关于在Selenium中如何处理单选框的全部内容,更多相关在Selenium中如何处理单选框内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部