2013-02-23 130 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