开发者

Using htmlagility xpath's to find webdriver elements

开发者 https://www.devze.com 2023-01-16 23:52 出处:网络
I\'m using htmlagility pack to locate elements in large loops through tables because its much faster than usi开发者_C百科ng the native methods provided through webdriver.

I'm using htmlagility pack to locate elements in large loops through tables because its much faster than usi开发者_C百科ng the native methods provided through webdriver.

After locating a specific element through the looping I'm getting its xpath, then feeding that to webdriver to return an element.

However I'm getting an exception;

OpenQA.Selenium.NoSuchElementException: FindElementByXPath.

//html[1]//body[1]//mpns:multipage[1]//mpns:pageview[1]//table[1]//

[Test]
[Browser(BrowserType.IE)]
public void Simple_HtmlAgility_Xpath()
{
    Browser.GoTo("http://test/");
    Browser.Login().Login_Admin();

    using (Browser.SwitchToFrame("main"))
    {
        var html = new HtmlAgilityPack.HtmlDocument();
        html.LoadHtml(Browser.PageSource);
        var node = html.DocumentNode.SelectSingleNode("//table[@id='testtable']");
        var xpath = node.XPath.Replace("/", "//");
        Log.Debug(xpath);
        var element = Browser.FindElement(By.XPath(xpath));
        Log.Debug(element.Text);
    }
}

an example of the page;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>Welcome</title>        
    </HEAD>
    <body>
        <?XML:NAMESPACE PREFIX="MPNS" /><?IMPORT NAMESPACE="MPNS" IMPLEMENTATION="/webctrl_client/1_0/multipage.htc" />
      <MPNS:MultiPage id="test1">
       <MPNS:PageView>        
        <table width="100%" id="testtable">
            <tr>
                <td>TEST</td>
            </tr>
        </table>                            
       </MPNS:PageView>
      </MPNS:MultiPage>
    </body>
</HTML>


Seems that Xpath cannot reference mpns namespace and you need to define it in your XPath query. Please see this for some details

0

精彩评论

暂无评论...
验证码 换一张
取 消