1

这是 HTML

通过硒我可以简单地获得innerHTML,但不能获得包括div“cont”在内的整个html

<div id="cont" class="container_CssClss">
    <div class="Header_CssClss">   Title...     </div>
    <div class="Url_CssClss">     www.example.co.il   </div>
    <div class="ResConent_CssClss">     bla..     </div>
</div>

试图解决这个问题我找到了向文档注入 jQuery 支持的方法

通过此代码

public static string AddJQueryToDoc(this IWebDriver driver, string jsCommand = "") {
    string injectjQuery = @"
               function loadScript(scriptUrl) 
               {
                   var head =  document.getElementsByTagName('head')[0];
                   var script = document.createElement('script');
                   script.type = 'text/javascript';
                   script.src = scriptUrl;
                   head.appendChild(script);
                /* alert(script.src);*/
               }
               loadScript('https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js');
               loadScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js');
               ".Replace("\r\n", "");

    ((IJavaScriptExecutor) driver).ExecuteScript(injectjQuery);
    // i have added an option to return the value of the return 

    if (jsCommand != string.Empty) return (string)((IJavaScriptExecutor) driver).ExecuteScript(jsCommand);
    return "No Results returned";
}

jsxcutrPath = openFileDialog1.FileName;
public static string getElmOuterHtmlViaJsExcutr_UsingjQuery(string clsName) {
    WebDriver = new FirefoxDriver();

    IJavaScriptExecutor jsExcutr;
    INavigation nav = WebDriver.Navigate();
    nav.GoToUrl(jsxcutrPath);

    string jsScrpt_jQ = @"
                              $('document').ready(function () {

                                        function getCLassName (){
                                              return $('#DivSearchResWraper').attr('class');
                                         }   
                                        getCLassName();

                               });
                           ";


    WebDriver.AddJQueryToDoc(jsScrpt_jQ);
}

我怎样才能让实现工作并将 jquery 结果/exectuiton 值返回到我的 C# winforms 应用程序?

4

1 回答 1

0

source html - 获取 di"sa_mc" 的所有内容

<div id="DivSearchResWraper" class="Maincontainer_CssClss">

    <div class="sa_mc">
        <div class="sb_tlst">
            <h3>
                <a id="SERP,5067.1" ref="http://www.....">contact Us</a>
            </h3>
            <div id="DivSeResParContainer_00" class="Div_SeResParContainer_CssClss">
                <div id="DivSeResParagraph_00" class="Div_SeResParagraph_CssClss">
                              Some Content
                </div>
                 <div id="DivSeResMail_00" class="Div_SeResMail_CssClss">
                    <a href="mailto: mail@domain.com">mail@domain.com</a>
                </div>
           </div>
        </div>
   </div>

    <div class="sa_mc">
        <div class="sb_tlst">
            <h3>
                <a id="SERP,5068.1" ref="http://www.....">contact Us</a>
            </h3>
            <div id="DivSeResParContainer_01" class="Div_SeResParContainer_CssClss">
                <div id="DivSeResParagraph_01" class="Div_SeResParagraph_CssClss">
                              Some Content
                </div>
                <div id="DivSeResMail_01" class="Div_srMail_CssClss">
                   <a href="mailto: mail@domain.com">mail@domain.com</a>
                </div>
            </div>
        </div>
     </div>


</div>

C#

 HtmlAgilityPack.HtmlNodeCollection seResultsDivsColl = bodyNode.SelectNodes("//div[@class='sa_mc']");
            _Globals.Clientsdata.counterinner = 0;

            foreach (var ClientSeRes in seResultsDivsColl)
            {

                    _Globals.Clientsdata.info.SePhraseUsed = bodyNode.SelectSingleNode("//div[@id='DivSearchResWraper']").GetAttributeValue("title", "");
                    _Globals.Clientsdata.info.title = ClientSeRes.SelectSingleNode("//a").InnerText;
                    _Globals.Clientsdata.info.URL = ClientSeRes.SelectSingleNode("//a").Attributes["href"].Value;
                    _Globals.Clientsdata.info.ResultText = ClientSeRes.SelectSingleNode("//div[@class='Div_srParagraph_CssClss']").InnerText;
                                              //  this is what i was missing out- ClientSeRes.OuterHtml  //
                    _Globals.Clientsdata.info.ResultHTML = ClientSeRes.OuterHtml.Replace("'", "&#39;");
                    _Globals.Clientsdata.info.Mail = ClientSeRes.SelectSingleNode("//div[@class='Div_srMail_CssClss']/a[@href]").Attributes[0].Value.Replace("mailto: ", "");
            }
于 2013-06-09T14:06:02.413 回答