简体   繁体   English

使用Selenium Webdriver,C#在dl,dt下查找元素

[英]Find Element under dl, dt using Selenium Webdriver, C#

I'm using Selenium Webdriver 2.30 in C#. 我在C#中使用Selenium Webdriver 2.30。 I appreciate if someone can tell me how to get the link clicking (id="e") in the following structure. 如果有人可以告诉我如何在以下结构中单击(id =“ e”)链接,我将不胜感激。

<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>

I tried 我试过了

selenium.SwitchTo().Frame("a");
selenium.FindElement(By.XPath("//a[@id=\"e\"]")).Click();

And tried 并尝试

selenium.SwitchTo().Frame("a");
selenium.FindElement(By.XPath("//div[@id='b']/dl[1]/dt[1]/a")).Click();

I also tried 我也试过

selenium.SwitchTo().Frame("a");
selenium.FindElement(By.LinkText("Go to somewhere")).Click();

Unfortunately, none of them works. 不幸的是,它们都不起作用。 The problem might because there is something different for element under a definition list, but I've not figured out. 问题可能是因为定义列表下的元素有些不同,但是我还没有弄清楚。

The exception messages thrown might useful here, post if you can. 抛出的异常消息在这里可能有用,请尽可能发布。

First you need to make sure it's in the correct frame. 首先,您需要确保它在正确的框架中。 Try debug the following and see if the counts are correct. 尝试调试以下内容,查看计数是否正确。

selenium.SwitchTo().Frame("a");
IList<IWebElement> dt = selenium.FindElements(By.XPath("//div[@id='b']//dt"));

// alternative way
selenium.SwitchTo().Frame(selenum.FindElement(By.CssSelector("frame[name='a']")));
IList<IWebElement> dt = selenium.FindElements(By.CssSelector("#b dt"));

If the it's in the correct frame, use Firebug to validate the xpath or cssSelector. 如果它在正确的框架中,请使用Firebug验证xpath或cssSelector。 I would try the followings as well. 我也会尝试以下方法。 Remember to make sure elements are actually loaded. 记住要确保元素被实际加载。 WebdriverWait might be useful. WebdriverWait可能有用。

selenium.FindElement(By.CssSelector("#e")).Click();
selenium.FindElement(By.CssSelector("dt.d>a")).Click();
selenium.FindElement(By.XPath("//a[text()='Go to somewhere']")).Click();
selenium.FindElement(By.XPath("(//dl[@id='c']/dt/a)[1]")).Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM