使用 C# 的 XPath 使用什么目标?

What target to use for XPath using C#?

目标:当我点击按钮时,它应该会转到我想要的网站并在文本框中输入 text = "test" (id="1648750690212EditingInput")。

我可以使用 chrome 中的 selenium 扩展来完成它,但工作的目标是 xpath:idRelative,它看起来像这样 xpath=//table[@id= 'gridList_headers']/thead/tr[2]/td[2]/div/div[2]/span/input.

问题:如何在 C# 中格式化 xpath 以实现此目的?

HTML <input type="text" class="ui-igedit-input ui-igedit-placeholder ui-iggrid-filtereditor" id="1648750690212EditingInput" placeholder="Contains..." role="textbox" aria-label="Text Editor" style="height: 100%; text-align: left;">

C#

        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://fakesite.com");
            driver.FindElement(By.XPath("//table[@id='gridList_headers']/thead/tr[2]/td[2]/div/div[2]/span/input")).SendKeys("test");
        }```

通常输入字段使用 post 请求 这样您就可以通过 Get 请求获取文本,并将 post 值返回给服务器

简单的 GET 请求

using System.Net;

...

using (var wb = new WebClient())
{
    var response = wb.DownloadString(url);
}

简单POST请求

using System.Net;
using System.Collections.Specialized;

...

using (var wb = new WebClient())
{
    var data = new NameValueCollection();
    data["username"] = "myUser";
    data["password"] = "myPassword";

    var response = wb.UploadValues(url, "POST", data);
    string responseInString = Encoding.UTF8.GetString(response);
}

正如您在 xpath 上询问的那样,您可以将它与正则表达式一起使用,它将为您提供强大的 html 文件搜索

{
// first use regex to get a part of html
// the (.*) matches every character between the two strings
// you get its value by using ".Groups(1).Value;"       
var tagsac = Regex.Match(page_Source_Str, Regex.Escape("<div class=\"video-metadata video-tags-list ordered-label-list cropped\"><ul>") + "(.*)" + Regex.Escape("</ul></div>")).Groups(1).Value;

// now you add the tag back to use xpath       
var tags = "<div class=\"video-metadata video-tags-list ordered-label-list cropped\"><ul>" + tagsac + "</ul></div>";
    
// now using xpath
var xml = XElement.Load(new System.IO.StringReader((tags)));
// you dig down elements to what you want    
var k = xml.<ul>.<li>.ToList;

// you get the html element value or attribute value like so    
var Tag_List = k.FindAll(c => (XElement)c.<a>.Attributes.FirstOrDefault.Value.StartsWith("/tags")).Select(xc => xc.<a>.FirstOrDefault.Value.ToLower.Replace("-", " ")).ToList;
    }