C# 使用SeleniumWebDriver,C查找dl,dt下的元素#
我正在C#中使用Selenium Webdriver 2.30。如果有人能告诉我如何在以下结构中点击链接(id=“e”),我将不胜感激C# 使用SeleniumWebDriver,C查找dl,dt下的元素#,c#,selenium,webdriver,C#,Selenium,Webdriver,我正在C#中使用Selenium Webdriver 2.30。如果有人能告诉我如何在以下结构中点击链接(id=“e”),我将不胜感激 <frame name = "a"> #document <html> <head></head> <body> <div id = "b"> &l
<frame name = "a">
#document
<html>
<head></head>
<body>
<div id = "b">
<dl id= "c">
<dt class = "d">
<a href = "http://somewhere.com" id = "e" class> Go to somewhere</a>
</dt>
<dt>
<a href = "http://something.com" id = "f"> Find something </a>
</dt>
</dl>
</div>
</body>
</html>
</frame>
并尝试
selenium.SwitchTo().Frame("a");
selenium.FindElement(By.XPath("//div[@id='b']/dl[1]/dt[1]/a")).Click();
我也试过了
selenium.SwitchTo().Frame("a");
selenium.FindElement(By.LinkText("Go to somewhere")).Click();
不幸的是,它们都不起作用。这个问题可能是因为定义列表下的元素有一些不同,但我还没有弄明白 抛出的异常消息可能在此处有用,如果可以,请发布 首先,你需要确保它在正确的框架内。 尝试调试以下内容,并查看计数是否正确
selenium.SwitchTo().Frame(“a”);
IList dt=selenium.FindElements(By.XPath(“//div[@id='b']//dt”);
//替代方法
selenium.SwitchTo().Frame(selenium.FindElement(By.CssSelector(“Frame[name='a'])));
IList dt=selenium.FindElements(由.CssSelector(“#b dt”);
如果它在正确的框架中,请使用Firebug验证xpath或cssSelector。我也会尝试以下方法。记住要确保元素已实际加载。可能有用
selenium.FindElement(由.CssSelector(“#e”))。单击();
selenium.FindElement(通过.CssSelector(“dt.d>a”))。单击();
selenium.FindElement(By.XPath(“//a[text()='gotosomethine']”);
selenium.FindElement(By.XPath(“(//dl[@id='c']/dt/a)[1]”);
谢谢你,伙计。我的团队领导帮助解决了这个问题。因为在这个之前我还有另一个Swithto框架。所以它实际上在swithto帧(“a”)上失败了。解决方案非常简单,只需添加selenium.Navigate().Refresh();在切换到帧之前。根据我的理解,在切换到帧之后,webdriver将忽略其他帧,除非您进行页面刷新。否,selenium.Navigate().refresh()代码>可能有效,但它不是处理此问题的理想方法。如果要跳出框架,应使用selenium.SwitchTo().DefaultContent()
。这将让webdriver切换回来,然后使用另一个selenium.SwitchTo().Frame()代码>。顺便说一句,如果您觉得我的答案有帮助并解决了您的问题,请随意投票并选择我的答案作为接受答案。
selenium.SwitchTo().Frame("a");
selenium.FindElement(By.LinkText("Go to somewhere")).Click();