如何专注于javascript动态Windows在webdriver



如何在页面加载时将光标集中在特定的输入框上?

按钮为"添加新约会",脚本为:

<input type="button" onclick="open_win()" value="add new appointment"/>
 WebElement ek = driver.findElement(By.xpath("//input[@value='add new appointment']"));
 jse.executeScript("arguments[0].click();", ek); 

这将打开一个弹出窗口。现在我需要把注意力集中在窗户上。
Ajax用于从这个弹出框中检索信息。
我尝试了每一个方面,专注于新的弹出窗口。输入文本字段是隐藏的,我试过的代码是:

jse.executeScript("window.focus();");
driver.switchTo().defaultContent();
driver.switchTo().activeElement().findElement(By.xpath("//*[@id='barge:bargeId:txtInput']"));
driver.switchTo().alert();

我想用下面的代码来关注文本字段:

jse.executeScript("document.getElementById('barge:bargeId:txtInput')[0].setAttribute('type', 'text');");
                driver.findElement(By.xpath("//*[@id='barge:bargeId:txtInput']")).sendKeys("suxs");

这也没有工作。
即使jse.executeScript("document.title")也没有起作用。
文本字段

的代码
<span id="barge:bargeId:input">
  <input id="barge:bargeId:txtInput" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all inputbox doubleInputbox ui-state-hover" type="text" tabindex="" maxlength="17" accesskey="" name="barge:bargeId:txtInput" role="textbox" aria-disabled="false" aria-readonly="false" aria-multiline="false"/>
</span>
  1. 点击按钮你可以简单这样做:driver.findElement(By.xpath("//input[@value='add new appointment']")).click();

  2. 说"这打开了一个弹出窗口"你是指一个实际的新窗口,新标签或对话框这样或这样吗?

我认为它打开了dialog,在这种情况下,它通常只是页面上的另一个元素。在这个例子中,应该是这样的:

driver.findElement(By.xpath("//*[@id='barge:bargeId:txtInput']")).sendKeys("suxs");

更新:
由于这是一个新窗口,有以下方法可以切换到这里描述的新窗口:

driver.switchTo().window("windowName");

同时,检查open_win() javascript函数的代码也很有帮助。

最新更新