使用C#用于XPATH的目标是什么?

发布于 2025-01-18 03:31:57 字数 745 浏览 0 评论 0原文

目标:当我单击按钮时,它应该转到我想要的站点,然后在文本框中输入文本=“ test”(id =“ 1648750690212editingInput”)。

我能够用Chrome中的硒扩展来完成它,但是目标是XPATH:iDrelative,看起来像xpath = // table [@id ='gridlist_headers']]/thead/thead/tr [2]/td [2 ]/div/div [2]/跨度/输入。

问题:如何格式化C#中的XPATH来实现这一目标?

html < input type =“ text” class =“ ui-igedit输入ui-igedit-placephorder ui-iggrid-filterereditor” label =“ text Editor”样式=“高度: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");
        }```

GOAL: when I click the button, it should go to the site I want and enter text = "test" in a textbox (id="1648750690212EditingInput").

I am able to accomplish it with the selenium extension in chrome but the target the works is xpath:idRelative and it looks like this xpath=//table[@id='gridList_headers']/thead/tr[2]/td[2]/div/div[2]/span/input.

QUESTION: How do I format the xpath in C# to accomplish this?

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");
        }```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2025-01-25 03:31:57

通常输入字段使用发布请求
因此,您可以将文本带有get request并将其发布回到服务器中的

简单请求请求

using System.Net;

...

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

简单的

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上所要求的,您可以将其与Regex一起使用,它将为您提供在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;
    }

usually input fields use post requests
so you can get the text with Get request and post back values to the server

Simple GET request

using System.Net;

...

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

Simple POST request

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);
}

and as you asked on xpath you can use it with regex and it will give you a powerfull searching on html files

{
// 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文