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

根据 http://blog.csdn.net/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还要在多用些, 继续学习记录吧

你可能感兴趣的:(selenium+python)