2013-03-07 43 views
4

下面是一個html腳本,我從一個網站上抓取。我想使用.NET以編程方式選擇項目以編程方式從.NET下拉菜單中選擇一個項目Web瀏覽器控件

<div id="MySite.condition_s-wrp" class="c-wrp-slctbx" style="z-index: 1;"> 
    <input id="MySite.condition_s-input" type="text" autocomplete="off" readonly="readonly" tabindex="0" class=" c-slctbx-medium" style="width: 268px;"> 
    <ul class="c-ul-slctbx max_height_300" style="width: 285px; display: none; top: 21px;"> 
     <li id="MySite.condition_s-option-" class="c-li-slctbx">Please choose</li> 
     <li id="MySite.condition_s-option-First" class="c-li-slctbx">First</li> 
     <li id="MySite.condition_s-option-Second" class="c-li-slctbx">Second</li> 
    </ul> 
    <select id="MySite.condition_s" name="attributeMap[MySite.condition_s]" class=" c-slctbx-medium" style="display: none;"> 
     <option value="">Please choose</option> 
     <option value="First">First</option> 
     <option value="Second">Second</option> 
     </select> 
</div> 

請注意以下代碼根本不起作用。

webBrowser1.Document.GetElementById("MySite.condition_s").SetAttribute("value", "First"); 

任何快速幫助將不勝感激。

+0

你可以獲取'select's兒童和設置屬性'selected'的其中之一。 – 2013-03-07 07:53:54

+0

說明「根本不工作」。 – 2013-03-07 09:36:02

回答

4

我終於弄明白一個我的朋友。這個小功能將很容易地完成剩下的工作。

感謝Farrukh Momin和他的時間。

public void SetComboItem(string id, string value) { 
     HtmlElement ee = this.webBrowser1.Document.GetElementById(id); 
     foreach (HtmlElement item in ee.Children) { 
      if (item.OuterHtml.ToLower().IndexOf(value.ToLower()) >= 0) { 
       item.SetAttribute("selected", "selected"); 
       item.InvokeMember("onChange"); 
      } 
      else { 
       item.SetAttribute("selected", ""); 
      } 
     } 

     ee = this.webBrowser1.Document.GetElementById(id + "-input"); 
     ee.InnerText = value; 
    } 

調用功能

this.SetComboItem("MySite.condition_s", "First"); 
1

試試這個。

HtmlDocument document = webBrowser1.Document; 
     HtmlElement siteCondition = document.GetElementById("MySite.condition_s"); 

     var option = siteCondition.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("First")); 
     option.SetAttribute("selected", "selected"); 
+0

沒有解決方案正在工作,我認爲這與INPUT和UL有關,而與SELECT和OPTION無關。我真的差異很大的HTML和JavaScript :(。 – 2013-03-07 08:20:30

1
+0

沒有任何解決方案工作,我認爲與INPUT和UL有關,而不是與選擇和選擇。 – 2013-03-07 08:15:54

+0

看看你的代碼再次用酷的mind.u會得到it.all d最好 – Amrendra 2013-03-07 08:17:54

2

你有沒有嘗試過這樣的:

webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1 
+1

恐怕,但沒有selectedIndex屬性 – 2013-03-07 07:48:22

+0

你可能將不得不先轉換到下拉控件,'GetElementById'返回一個'元素',它沒有那個屬性 – 2013-03-07 07:49:36

+0

沒有解決方案正在工作,我認爲是與INPUT和UL有關,而不是與SELECT和OPTION有關。 – 2013-03-07 08:19:50

0

我創辦,如果你只是調用點擊一個接一個,你應該能夠找到你想要做一個對裏面循環點擊付費。

HtmlElement site = this.webBrowser2.Document.GetElementById("myId"); 
foreach (HtmlElement item in site.Children) 
{ 
    if (item.InnerText.ToString() == "something") 
    { 
     item.InvokeMember("Click"); 
      break; 
    } 
     else 
    { 
     item.InvokeMember("Click"); 
    } 
} 
0

100%的工作碼(Win7上測試 - IE11)

摘自:

C#| WebBrowser控件 - 對HTML編程方式選擇項中選擇
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html

HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("select") 
foreach (HtmlElement heItem in col) 
{ 
    if (heItem.GetAttribute("className").Contains("exampleClassName") == true) 
    { 
    heItem.SetAttribute("selectedIndex", "3"); // select value at #3 
    break; // incase of needed... 
    } 
} 
相關問題