2013-02-23 177 views
1

好吧,这似乎是一件容易的事情,但我找不到如何去做。我使用了htmlagility包来解析网页,并且效果很好。现在,问题在于以下几点。设置html使用c选择选项#

<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可以做到这一点吗?如果不是,我能做些什么来选择其中一个选项?

回答

0

我认为你是什么HtmlAgilityPack可以做一个小迷糊......

HtmlAgilityPack - 仅仅是一个praser。

browser's point of view,选择其中一个选项将导致浏览器发送一个POST类型的请求到页面。

什么,你现在能做的就是,模拟出POST要求要么WebClientHttpWebRequest,那么你会得到你new web page您可以使用该HtmlAgilityPack新网页的工作。

+0

问题是,它不是一个新的网页,它只是加载一个表格与我想分析的信息。 – Fatstink 2013-02-23 03:45:48

+0

@Fatstink - 再检查一下给出的答案!我已经读了几个地方'Selenium WebDriver'能够做到这一点! – 2013-02-23 03:47:15

0

这可以通过使用硒webdriver轻松完成。 了解它,适合处理这种东西。

在这里,我首先选择使用图书馆的webdriver了该选项的元素
var selectElem = driver.FindElement(By.Id("ctl00_BodyContent_ddlChooseView"));

现在使用WebDriver.Support.UI库我得到的所有的选项
SelectElement selectOption = new SelectElement(selectElem);

现在ü可以执行元素上的任何操作。即

selectOption.SelectByValue("here u give the value")

selectOption.SelectByText("here u give the value")

还有更多...你发现了。

+0

因此,如果我正在寻找在很多计算机上使用它,我将不得不向每个用户分发Selenium? – Fatstink 2013-02-23 03:47:31

+0

好吧,我喜欢这个选项,我在SelectOption出现问题时,我已经链接了支持UI库,但是Intellisense找不到SelectOption? – Fatstink 2013-02-23 04:03:40

+0

您需要使用webdriver库才能使用它,并为您的项目添加引用。您可以轻松地从他们的网站下载它。 – Ulquiorra 2013-02-23 04:06:40

0

此代码可能对您有用它包含基本的详细信息。

<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> 

对不起。

+0

我很感激它,有没有办法设置它,以便它实际上不打开浏览器,我只想选择下拉项目,以便我可以再次解析页面。我需要这个不打开别的东西。 – Fatstink 2013-02-23 04:43:49