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

被折叠的 条评论
为什么被折叠?



