C# 使用c设置html选择选项

C# 使用c设置html选择选项,c#,html,html-agility-pack,C#,Html,Html Agility Pack,好吧,这看起来是件容易的事,但我找不到怎么做。我使用了htmlagility包来解析网页,效果非常好。现在,问题是以下几点 <td width="45%" class="TextBold" nowrap> <select name="ctl00$BodyContent$ddlChooseView" onchange="if (this.selectedIndex > 0 {pageTracker._trackEvent('webpage tracker','complet

好吧,这看起来是件容易的事,但我找不到怎么做。我使用了htmlagility包来解析网页,效果非常好。现在,问题是以下几点

<td width="45%" class="TextBold" nowrap>
<select name="ctl00$BodyContent$ddlChooseView" onchange="if (this.selectedIndex > 0
{pageTracker._trackEvent('webpage tracker','complete report',this.options
[this.selectedIndex].text);}
ShowProcessing(this);setTimeout('__doPostBack(\'ctl00$BodyContent$ddlChooseView\',\'\')', 
    0)" id="ctl00_BodyContent_ddlChooseView" class="TextBold">
        <option selected="selected" value=""> -- Select a view -- </option>
        <option value="H">Option1</option>
        <option value="R">Option2</option>
        <option value="N">Option3</option>
        <option value="NA">Option4</option>
        <option value="RN">Option5</option>
        <option value="QP">Option6</option>

</select>
</td>

如果格式不正确,我深表歉意。我想在html选择对象中选择一个选项。触发页面上的新显示,然后解析该新网页。htmlagilitypack可以做到这一点吗?如果没有,我可以做什么来选择其中一个选项?

我想你对HtmlAgilityPack的功能有点困惑

HtmlAgilityPack-只是一个praser

从浏览器的角度来看,选择其中一个选项将导致浏览器向页面发送帖子类型请求


您现在可以做的是,使用WebClient或HttpWebRequest模拟POST请求,然后您将获得新网页,您可以使用HtmlAgilityPack在新网页上工作。

这可以通过使用selenium webdriver轻松完成。 读一下,这对处理这类事情很有好处

在这里,我首先选择使用Webdriver库获得选项的元素 var selectElem=driver.FindElementBy.Idctl00\u BodyContent\u ddlcooseview

现在使用WebDriver.Support.UI库,我获得了所有选项 SelectElement selectOption=新建SelectElement SelectElem

现在,您可以对元素执行任何操作。即 喜欢 选择Option.SelectByValue在此处输入值 或 选择Option.SelectByText在此处输入值


还有更多。。。这段代码可能对您很有用,它包含了基本的细节

<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

//Need to add these two libarary
//For that u need to have WebDriver.dll and WebDriver.Support.dll 
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace Test
{
class Program
{
static void Main(string[] args)
{
//Intializing the webdriver. 
//Note i m using firefox driver, others can also be used.
IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
//Navigating to the given page.
driver.Navigate().GoToUrl("url of the page you want to get the option from");
//Finding the element. If element not present it throws exception so do remember to handle it.
var element = driver.FindElement(By.Id("ctl00_BodyContent_ddlChooseView"));
//No intializing the select element option.
SelectElement selectElem = new SelectElement(element);
selectElem.SelectByValue("H"); 
//or i can select option using text that is
selectElem.SelectByText("Option1"); 
}

}
}
</code>

很抱歉出现缩进。

问题在于它不是一个新网页,它只是加载了一个表,其中包含了我要解析的信息。@fatboond-请再检查一个给定的答案!我读过一些地方SeleniumWebDriver能够做到这一点!因此,如果我希望在许多计算机上使用此选项,我也必须将Selenium分发给每个用户?好的,我喜欢此选项,我在显示SelectOption时遇到问题,我已链接了支持UI库,但Intellisense找不到SelectOption?您需要使用webdriver库才能使用它,并添加对项目的引用。你可以很容易地从他们的网站下载。我很感激,有没有办法设置它,这样它就不会真正打开浏览器,我只想选择下拉项,这样我就可以再次解析页面了。我需要这个不要打开其他任何东西。