使用c#設置html選擇選項

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','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做嗎?如果沒有,我該怎麼做才能選擇其中一個選項?

熱門答案

此代碼可能對您有用它包含基本詳細信息。

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

對不起縮進。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow