[英]Selecting a dynamically generated option element with a c# webbrowser control
I am using ac# webbrowser control to navigate through a commercial website I work with. 我正在使用ac#webbrowser控件浏览与我合作的商业网站。 I log in and navigate to a particular list. 我登录并导航到特定列表。 The list is prospective jobs. 该清单是预期的工作。 Then I continue through some links to bid on those jobs. 然后,我继续通过一些链接来竞标这些工作。 In the process I ran into a problem. 在这个过程中,我遇到了一个问题。
On one of the forms there are 2 select elements (drop down lists). 在其中一种表格上有2个选择元素(下拉列表)。 The options on those lists are generated dynamically by means of some javascript scripts - most of which are available in the source. 这些列表中的选项是通过一些javascript脚本动态生成的-大多数脚本都可以在源代码中获得。
In the page source code there is nothing to select. 在页面源代码中,没有任何选择。 The options appear dynamically when navigating the page manually - but I am trying to navigate it by means of c# in a webbrowser. 手动浏览页面时,这些选项会动态显示-但我正在尝试通过Web浏览器中的c#对其进行浏览。
Here's the form. 这是表格。 (I cut out styles and changed some of the text - and I know it is badly formed, but it is not mine) (我剪掉样式并更改了一些文本-我知道它的格式不好,但不是我的)
<form name="frm1" id="frm1" action="/tab/Transport/LoadAssigned2.asp" method="post">
<table class="section">
<tr>
<td>Name</td>
<td>
<input type="text" name="s_name" id="s_name" size="25" maxlength="50"></td>
</tr>
<tr>
<td>Fax</td>
<td>
<input type="text" name="txtFaxNumber" id="txtFaxNumber" size="25" maxlength="15" value="1234567890"></td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="txtEmail" id="txtEmail" size="25" maxlength="225"></td>
</tr>
<tr>
<td>Pickup will occur on or before</td>
<td>
<select name="stransp_pickup_date" id="stransp_pickup_date" style="width: 173px;" onchange="setDeliveryDate()">
</select>
</td>
<tr>
</tr>
<td>Delivery will occur on or before</td>
<td>
<select name="stransp_delivery_date" id="stransp_delivery_date" style="width: 173px;">
</select>
</td>
</tr>
</table>
<input type="hidden" name="nload_id" id="nload_id" value="123456789">
</form>
As you can see the two select elements have no option children. 如您所见,两个select元素没有选项子级。 Those are created by the scripts, starting with setDeliveryDate: 这些是由脚本创建的,从setDeliveryDate开始:
function setDeliveryDate(){
var distance = 226;
var delivery = $("#stransp_delivery_date");
var pickupDate = $("#stransp_pickup_date option:selected").val();
$("#stransp_delivery_date option").remove();
delivery.append("<option value='-1'>SELECT DATE</option>");
if(distance <=200){
generateDeliveryDates(delivery,pickupDate,2);
}else if(distance >=201 && distance <= 400){
generateDeliveryDates(delivery,pickupDate,3);
}else if(distance >=401 && distance <= 700){
generateDeliveryDates(delivery,pickupDate,4);
}else if(distance >=701 && distance <= 1400){
generateDeliveryDates(delivery,pickupDate,5);
}else if(distance >=1401 && distance <= 1800){
generateDeliveryDates(delivery,pickupDate,6);
}else if(distance >=1801 && distance <= 2200){
generateDeliveryDates(delivery,pickupDate,7);
}else if(distance >=2201 && distance <= 2500){
generateDeliveryDates(delivery,pickupDate,8);
}else if(distance >=2501 && distance <= 4000){
generateDeliveryDates(delivery,pickupDate,9);
}
}
And the generateDeliveryDates functions is: generateDeliveryDates函数为:
function generateDeliveryDates(delivery,pickupDate,index){
for (var i = 0; i < index; i++) {
if (moment(pickupDate).add('days', i).format('dddd') == 'Sunday') {
index++;
delivery.append("<option value='" + moment(pickupDate).add('days', i).format('MM/DD/YYYY') + "'>" + moment (pickupDate).add('days', i).format('dddd')+" - "+ moment(pickupDate).add('days', i).format('MM/DD/YYYY') + "</option>");
} else {
delivery.append("<option value='" + moment(pickupDate).add('days', i).format('MM/DD/YYYY') + "'>" + moment (pickupDate).add('days', i).format('dddd')+" - "+ moment(pickupDate).add('days', i).format('MM/DD/YYYY') + "</option>");
}
};
}
IfI can keep showing more of the scripts - but I'm hoping the idea is clear. 如果我可以继续显示更多脚本,但我希望这个主意很清楚。 The options under the select element are created based on an onchange event in the first select element. select元素下的选项是根据第一个select元素中的onchange事件创建的。 It is a list of dates. 这是日期列表。
What I want to do is to select the last of the date options in both cases - but I can't see how to do it before the exist. 在两种情况下,我想选择最后一个日期选项-但在存在之前我看不到如何做。 Also, the number of options in the list varies based on the distance, as you can see above. 另外,如上所示,列表中的选项数根据距离而异。
How can I access those elements when I can't see them in the page source code? 在页面源代码中看不到这些元素时,如何访问这些元素?
Very appreciative of any help or guidance. 非常感谢任何帮助或指导。
In case anyone reads this in the future, I ended up solving this myself, and want to share the solution, which is pretty straight forward. 万一将来有人读过这个,我最终自己解决了这个问题,并想分享解决方案,这很简单。
1 - please note that the first "select" element has an onchange function- setDeliveryDate. 1-请注意,第一个“选择”元素具有onchange函数setDeliveryDate。 So the first think I do is invoke that script function once: 因此,我首先想到的是一次调用该脚本函数:
webBrowser1.Document.InvokeScript("setDeliveryDate");
Then I collect the child nodes in a collection, and select the last of them: 然后,我将子节点收集到一个集合中,并选择它们中的最后一个:
HtmlElementCollection colOptions =
webBrowser1.Document.GetElementById("stransp_pickup_date").Children;
int colCount = colOptions.Count;
colOptions[colCount - 1].SetAttribute("selected", "selected");
The I re-run the same script, which also creates the options for the second select, and do the same thing (get the option elements into a collection and select the last one): 我重新运行相同的脚本,它还会为第二个选择创建选项,并执行相同的操作(将选项元素放入集合中并选择最后一个):
webBrowser1.Document.InvokeScript("setDeliveryDate");
HtmlElementCollection colOptions2 =
webBrowser1.Document.GetElementById("stransp_delivery_date").Children;
int colCount2 = colOptions2.Count;
colOptions2[colCount2 - 1].SetAttribute("selected", "selected");
The last step is of course to submit the form: 最后一步当然是提交表单:
webBrowser1.Document.GetElementById("frm1").InvokeMember("submit");
Hope this helps other newbies like me. 希望这对像我这样的新手有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.