//使用watin测试一些ajax网站,当点击td时,div(“appendProdctTreeDiv”)将显示一些响应html,但我不知道如何获得这个div innerhtml并点击链接whick我想要; 可以通过测试“http://www.google.com/ig"?thanks提前给出一些例子!
------使用watin --------
IE ie = new IE();
....
ie.Element(Find.ById( “树”))点击();
textbox1.text = ie.Div(“appendProdctTreeDiv”)。innerHtml; //这种方式错了;
...
.. html代码..
<'TD id = tree onclick = showAppendProductTree()noWrap> tab1<'/ TD>
...
<'DIV style =“WIDTH:100%; HEIGHT:96%; OVERFLOW:auto”id = appendProdctTreeDiv loaded =“false”><'/ DIV>
....
function showAppendProductTree(){
showTreeTab(3);
if(document.getElementById(“appendProdctTreeDiv”)。loaded ==“false”){
var url ="product!changeAppendProduct.do";
var params = "";
var newProductId = document.getElementById("newProductId").value;
new Ajax.Request(
url,
{
method: 'get',
parameters: params+"&random="+Math.random(),
requestHeaders:["Cache-Control","no-cache"],
onComplete: function(originalRequest){
var message = originalRequest.responseText;
document.getElementById("appendProdctTreeDiv").innerHTML= message;
document.getElementById("appendProdctTreeDiv").loaded = "true";
synAppendTree();
document.getElementById("waitLoadAppendProd").style.display = "none";
document.getElementById("searchDiv").style.display = "block";
}
});
}
}
function showTreeTab(tabId){
document.getElementById("treeDiv").style.display ="block";
for(var i=1;i<=3;i++){
if(i==tabId){
document.getElementById("tree"+i).style.display = "block";
}
else{
document.getElementById("tree"+i).style.display = "none";
}
}
}
答案 0 :(得分:5)
我认为您有几种方法可以等待响应。选项1:
首先,WatiN默认等待30秒,以便在页面上显示元素。因此,如果您知道要单击的链接的ID或文本,我希望以下代码能够正常工作:
ie.Div(“appendProdctTreeDiv”)。链接(Find.ByText(“链接文本”))。点击();
如果没有,这里是选项2:
在触发ajax调用的脚本中,在oncomplete回调函数中,div的loaded属性设置为true(在你包含的html中为false)。您可以在等到调用之前使用它,然后获取innerhtml或单击要单击的链接:
ie.Div(“appendProdctTreeDiv”)。WaitUntil(“loaded”,“true”);
HTH, 的Jeroen
答案 1 :(得分:0)
当您获得innerHtml时,异步AJAX调用可能仍在进行中。
ie.Element(Find.ById( “树”))点击();
textbox1.text = ie.Div(“appendProdctTreeDiv”)。innerHtml; //这种方式错了;
尝试这样的事情,看看你是否最终得到了预期的结果。这不是那么好的方法,但检查很容易。
ie.Element(Find.ById( “树”))点击();
System.Threading.Thread.Sleep(5000); //等待5秒;如果您的异步调用需要更长时间,则会超过5秒。
textbox1.text = ie.Div(“appendProdctTreeDiv”)。innerHtml; //这种方式错了;
如果网站使用的是ASP.NET,要确定该网页是否在异步调用中,您可以评估以下javascript。
Sys.WebForms.PageRequestManager.getInstance()。get_isInAsyncPostBack()
像
一样打电话bool isInAsync = bool.Parse(ie.Eval(“Sys.WebForms.PageRequestManager.getInstance()。get_isInAsyncPostBack()”);
但是,google.com/ig确实使用了MS AJAX,因此我无法使用上述javascript提供一个有效的示例。
使用wait和google.com/ig的翻译小工具的示例。在每次运行时在TypeText中添加不同的单词或清除缓存。 ie.GoTo( “www.google.com/ig”); System.Threading.Thread.Sleep(2000); //等待页面小工具加载 Console.WriteLine(。ie.Frame( “remote_iframe_10”)股利( “dict_content”)的innerHTML);
ie.Frame("remote_iframe_10").Form("dictForm").TextField("dictQuery").TypeText("bajar");
ie.Frame("remote_iframe_10").Form("dictForm").Buttons[0].Click();
Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);
System.Threading.Thread.Sleep(2000); //Wait for gadget to finish here.
Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);