【发布时间】:2017-11-17 17:44:57
【问题描述】:
我的网络应用程序是如何工作的:
在更改上述表单中的条件后,我的 Web 应用程序会加载一个带有单选按钮输入元素的 div。上面的表格将允许经理选择日期和时间。下面的 div 将加载在该日期和时间可用的所有员工。我给这个 div 的 id 为“EmployeeListBox”。每当 EmployeeListBox 上方的表单中的条件发生变化时,都会使用 jquery 的 append 方法注入单选按钮。
好的,这就是我的 Web 应用程序如何工作的想法。我要做的是在表单更改后单击 div EmployeeListBox 中的第一个单选按钮,在 java 中使用 Firefox 的 selenium Web 驱动程序。这是我需要修复的行:
`driver.findElement(By.xpath("//[@id="EmployeeCell"]/span[2]/input")).click();`
当我运行这个 xpath 时,我收到了这个错误:
Exception processing script - Element not found in the cache - perhaps the page has changed since it was looked up.
我猜我尝试过的 xpath 不起作用,因为元素是动态加载的。
这是加载 EmployeeListBox 的 JS 代码。
AvialableStaff = JSON.parse( data );
var MSG = "";
$('#EmployeeListBox').empty();
/*for each employee in AvialableStaff load the #CreateShiftLifeOfEmployees Select option*/
var eico = 0;
for ( var Staff in AvialableStaff )
{
var ID = AvialableStaff[Staff]["employeeID"];
var name = AvialableStaff[Staff]["Firstname"]+" "+ AvialableStaff[Staff]["Lastname"];
MSG += "<div id = \"EmployeeCell\">";
MSG += "<span class = \"MakeLeft\" id = \"EI"+eico+"\" > "+name+" </span>";
MSG += "<span class = \"MakeRight\"><input type = \"radio\" name=\"ChooseStaff\" value = \""+ID+"\" class = \"CSTAFFradioButtons\" /> </span>";
MSG += "</div>";
eico++;
}
$('#EmployeeListBox').append( MSG );
这是没有员工单元格的 EmployeeListBox:
<p>Select an available employee</p>
<div id = "CSEmployeeList" >
<div id = "StaffAvaileP"> <h3> Staff Available</h3> </div>
<div id = "EmployeeListBox">
<!-- Radio Buttons go here -->
</div>
</div>
这是更改表单并插入员工单选按钮后员工框的外观:
<div id="EmployeeListBox">
<div id="EmployeeCell">
<span class="MakeLeft" id="EI0"> Person One </span>
<span class="MakeRight">
<input type="radio" name="ChooseStaff" value="9" class="CSTAFFradioButtons">
</span>
</div>
<div id="EmployeeCell">
<span class="MakeLeft" id="EI1"> Person Two </span>
<span class="MakeRight">
<input type="radio" name="ChooseStaff" value="10" class="CSTAFFradioButtons">
</span>
</div>
<div id="EmployeeCell">
<span class="MakeLeft" id="EI2"> Person Three </span>
<span class="MakeRight">
<input type="radio" name="ChooseStaff" value="11" class="CSTAFFradioButtons">
</span>
</div>
</div><!--End of EmployeeListBox -->
这是我运行测试的 java 方法:
/** Method testCreateShifts
* purpose: Loads shifts with realistic shift data like the
* real life Saturday schedule.
*/
public static void testCreateShifts()
{
driver.get(theurl);
String createshiftPage = "https://***/index.php?/CreateAShift";
driver.get(createshiftPage);
new Select(driver.findElement(By.id("Day"))).selectByVisibleText("11");
new Select(driver.findElement(By.id("StartTime"))).selectByVisibleText("8:30am");
new Select(driver.findElement(By.id("EndTime"))).selectByVisibleText("2:00pm");
driver.findElement(By.id("Instructor")).click();
/*TODO: Currently trying to click on first employee in employee list box*/
driver.findElement(By.xpath("//*[@id=\"EmployeeCell\"]/span[2]/input")).click();
driver.findElement(By.id("CreateShiftSubmit")).click();
}/*End of Method testCreateShifts*/
那么在 java 中使用 selenium 如何在 EmployeeListBox 被 jquery 重新加载后单击列表中的第一个单选按钮?
【问题讨论】:
标签: java jquery selenium testing xpath