控件定位学习:radio、checkbox、ComboBox

本文介绍了如何使用selenium进行网页控件定位,包括radio、checkbox和ComboBox的选择与操作。通过实例分享了针对select选择框的六种定位方法,并提到在遇到frame时的处理策略。学习过程中,作者强调了is_selected()方法和try-except-finally语句的重要性。

根据 http://blog.youkuaiyun.com/liujingqiu/article/details/50489455 博主的脚步学习了一下网页中一些控件的使用
哈哈哈,昨晚竟然有人问我这个小菜鸟的问题,然后他用的是unittest框架,昨晚百度了一点皮毛,准备用用虽然可以执行,但是好像他的意义与用法不是我想的那样子,还是好好先来简单的吧
在定位select选择框时出错,百度了一下需要引入select模块
其中 http://www.cnblogs.com/yoyoketang/p/6128636.html,这篇文章对select框的定位讲解了六种方法
下面是定位控件的脚本(恰好遇到frame,顺便定位试了试)

from selenium import webdriver
from selenium.webdriver.support.select import Select

class ControlsClass(object):
    def test_open(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get("https://register.rediff.com/register/register.php")
    def test_radio(self):   #raido定位并选择女
        try:
            Radio = self.driver.find_element_by_xpath('//*[@id="tblcrtac"]/tbody/tr[24]/td[3]/input[1]')
            #//*[@id="tblcrtac"]/tbody/tr[24]/td[3]/input[1]
            if Radio.is_selected() == False:
                Radio.click()
                print('radio is seleted')
            else:
                print('radio is seleted')
        except Exception as e:
            print("Exception is", format(e))
        #finally:
         #   print("OK")
    def test_checkBox(self): #选择checkBox控件
        try:
            checkBox = self.driver.find_element_by_xpath('//input[@class="nomargin"]')
            if checkBox.is_selected() == False:
                checkBox.click()
                print('checkBox is selected')
            else:
                print('checkBox is selected')
        except Exception as e:
            print("Exception is",format(e))
        #finally:
        #    print("OK")
    def test_ComboBox(self): #选择Country下拉框的Chian选项
        try:
            ComboBox = Select(self.driver.find_element_by_id("country"))  #需要导入select 模块
            #选择Chian
            ComboBox.select_by_visible_text("China")
            return print("ComBox selected")
        except Exception as e:
            print("Exception is",format(e))
        #finally:
         #   print("OK")
    def test_Frame(self):   #尝试定义Frame
        try:
            self.driver.switch_to.frame("metric_iframe")
            print("frame is selected")
        except Exception as e :
            print("Exception is ",format(e))
if __name__ =='__main__':
    test = ControlsClass()
    test.test_open()
    test.test_radio()
    test.test_checkBox()
    test.test_ComboBox()
    test.test_Frame()
    test.driver.quit()

我觉得稍微复杂一点的就是select选择框的定位,然后radio、checkbox 记住is_select()方法,其中跟着博主用的try-except-finally还要在多用些, 继续学习记录吧

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值