Selenium API(C#)
2013-11-23 12:20
155 查看
1FetchingaPage
driver.Url="http://www.google.com";
2LocatingUIElements(WebElements)
ByID
Thisisthemostefficientandpreferredwaytolocateanelement.CommonpitfallsthatUIdevelopersmakeishavingnon-uniqueid’sonapageorauto-generatingtheid,bothshouldbeavoided.Aclassonanhtmlelementismoreappropriatethananauto-generatedid.
Exampleofhowtofindanelementthatlookslikethis:
IWebElementelement=driver.FindElement(By.Id("coolestWidgetEvah"));
ByClassName
“Class”inthiscasereferstotheattributeontheDOMelement.OfteninpracticalusetherearemanyDOMelementswiththesameclassname,thusfindingmultipleelementsbecomesthemorepracticaloptionoverfindingthefirstelement.
Exampleofhowtofindanelementthatlookslikethis:
<divclass="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
IList<IWebElement>cheeses=driver.FindElements(By.ClassName("cheese"));
ByTagName
TheDOMTagNameoftheelement.
Exampleofhowtofindanelementthatlookslikethis:
ByLinkText
Findthelinkelementwithmatchingvisibletext.
Exampleofhowtofindanelementthatlookslikethis:
ByCSS
Likethenameimpliesitisalocatorstrategybycss.Nativebrowsersupportisusedbydefault,sopleaserefertow3ccssselectors<http://www.w3.org/TR/CSS/#selectors>foralistofgenerallyavailablecssselectors.Ifabrowserdoesnothavenativesupportforcssqueries,thenSizzleisused.IE6,7andFF3.0currentlyuseSizzleasthecssqueryengine.
Bewarethatnotallbrowserswerecreatedequal,somecssthatmightworkinoneversionmaynotworkinanother.
Exampleoftofindthecheesebelow:
ByXPATH
Atahighlevel,WebDriverusesabrowser’snativeXPathcapabilitieswhereverpossible.Onthosebrowsersthatdon’thavenativeXPathsupport,wehaveprovidedourownimplementation.Thiscanleadtosomeunexpectedbehaviourunlessyouareawareofthedifferencesinthevariousxpathengines.
Thisisalittleabstract,soforthefollowingpieceofHTML:
UsingJavaScript
YoucanexecutearbitraryjavascripttofindanelementandaslongasyoureturnaDOMElement,itwillbeautomaticallyconvertedtoaWebElementobject.
SimpleexampleonapagethathasjQueryloaded:
driver.Url="http://www.google.com";
2LocatingUIElements(WebElements)
ByID
Thisisthemostefficientandpreferredwaytolocateanelement.CommonpitfallsthatUIdevelopersmakeishavingnon-uniqueid’sonapageorauto-generatingtheid,bothshouldbeavoided.Aclassonanhtmlelementismoreappropriatethananauto-generatedid.Exampleofhowtofindanelementthatlookslikethis:
<divid="coolestWidgetEvah">...</div>
IWebElementelement=driver.FindElement(By.Id("coolestWidgetEvah"));
ByClassName
“Class”inthiscasereferstotheattributeontheDOMelement.OfteninpracticalusetherearemanyDOMelementswiththesameclassname,thusfindingmultipleelementsbecomesthemorepracticaloptionoverfindingthefirstelement.Exampleofhowtofindanelementthatlookslikethis:
<divclass="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
IList<IWebElement>cheeses=driver.FindElements(By.ClassName("cheese"));
ByTagName
TheDOMTagNameoftheelement.Exampleofhowtofindanelementthatlookslikethis:
<iframesrc="..."></iframe> IWebElementframe=driver.FindElement(By.TagName("iframe"));
ByLinkText
Findthelinkelementwithmatchingvisibletext.Exampleofhowtofindanelementthatlookslikethis:
<ahref="http://www.google.com/search?q=cheese">cheese</a> IWebElementcheese=driver.FindElement(By.LinkText("cheese"));
ByCSS
Likethenameimpliesitisalocatorstrategybycss.Nativebrowsersupportisusedbydefault,sopleaserefertow3ccssselectors<http://www.w3.org/TR/CSS/#selectors>foralistofgenerallyavailablecssselectors.Ifabrowserdoesnothavenativesupportforcssqueries,thenBewarethatnotallbrowserswerecreatedequal,somecssthatmightworkinoneversionmaynotworkinanother.
Exampleoftofindthecheesebelow:
<divid="food"><spanclass="dairy">milk</span><spanclass="dairyaged">cheese</span IWebElementcheese=driver.FindElement(By.CssSelector("#foodspan.dairy.aged"));
ByXPATH
Atahighlevel,WebDriverusesabrowser’snativeXPathcapabilitieswhereverpossible.Onthosebrowsersthatdon’thavenativeXPathsupport,wehaveprovidedourownimplementation.Thiscanleadtosomeunexpectedbehaviourunlessyouareawareofthedifferencesinthevariousxpathengines.Driver | TagandAttributeName | AttributeValues | NativeXPathSupport |
---|---|---|---|
Lower-cased | AstheyappearintheHTML | Yes | |
Lower-cased | AstheyappearintheHTML | No | |
Caseinsensitive | AstheyappearintheHTML | Yes |
<inputtype="text"name="example"/> <INPUTtype="text"name="other"/>
IList<IWebElement>inputs=driver.FindElements(By.XPath("//input"));
UsingJavaScript
YoucanexecutearbitraryjavascripttofindanelementandaslongasyoureturnaDOMElement,itwillbeautomaticallyconvertedtoaWebElementobject.SimpleexampleonapagethathasjQueryloaded:
IWebElementelement=(IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return$('.cheese')[0]");
相关文章推荐
- C#和JAVA5.0可以在String对象中使用类似天C的printf()格式化函数
- C#调用输入法
- C#中使用Win32和其他库
- [翻译]使用c#读取word文档
- [C#][正则表达式]寻找匹配的Groups的几种方法
- 使用C#在应用程序间发送消息
- C#学习小记1 没有main() 及我突发的想起来的,使用来验证在类内可以实例化本类的对象
- C#中的常用正则表达式总结
- C# 中关于 List 的 Xml序列化
- c# 类实例化过程
- 也说C#实现对Word文件读写
- C#用WebBrowser控件获取Post数据
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C#截取字符串方法
- C#与.NET程序员面试宝典 2.3.1 面试题16:如何进行源代码的保护和版本间的维护(图)
- c#获取中文首字母的方法
- C# Image和字节流的互化
- 跟小静读CLR via C#(10)-参数
- C#在Form之间传值问题
- C#反射的GetCustomAttributes方法