获取页面及页面元素的内容—Selenium自动化测试指南(5)

发表于:2013-8-30 11:50

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:赵卓    来源:51Testing软件测试网

分享:

  通过Firebug查看其源码,如图5-53所示。

  图5-53  HTML源码

  编写如程序清单5-57或程序清单5-58所示的的代码,将勾选状态存放到isSelected变量中,并将其打印出来。

  程序清单5-57  C#代码

IWebDriver driver = new FirefoxDriver();
INavigation navigation = driver.Navigate();
navigation.GoToUrl(http://tieba.baidu.com);
IWebElement checkBox = driver.FindElement(By.Id("pass_loginLite_input_isMem0"));
bool isSelected = checkBox.Selected;
Console.WriteLine("是否勾选:" + isSelected);
Console.ReadKey();

  程序清单5-48  Java代码

WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to(http://tieba.baidu.com);
WebElement checkBox = driver.findElement(By.id("pass_loginLite_input_isMem0"));
boolean isSelected = checkBox.isSelected();
System.out.println("是否勾选:" + isSelected);

  执行结果如图5-54所示。

  图5-54  执行结果

  5.7.5  TagName/getTagName()

  TagName/getTagName()用于获取元素的标记名称。

  例如,要获取百度首页的“搜索设置”超级链接的标记名称,如图5-55所示。

  

图5-55  百度首页

  可以编写如程序清单5-49或程序清单5-50所示的代码,先打开百度页面,然后将“搜索设置”的文本值存放到tagName变量中,最后在将其打印出来。

  程序清单5-49  C#代码
  

IWebDriver driver = new FirefoxDriver();
INavigation navigation = driver.Navigate();
navigation.GoToUrl(http://www.baidu.com);
IWebElement link = driver.FindElement(By.Name("tj_setting"));
string tagName = link.TagName;
Console.WriteLine(tagName);
Console.ReadKey();

54/5<12345>
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号