选择加载文档后加载的html元素,使用selenium按xpath查找元素以选择元素

时间:2017-11-17 17:44:58

标签: java jquery selenium testing xpath

我的网络应用程序如何运作:

我的网络应用程序在更改上述表单中的条件后,使用单选按钮输入元素加载div。上面的表格将允许经理选择日期和时间。下面的div将加载该日期和时间的所有员工。我给这个div id" EmployeeListBox"。每次条件在EmployeeListBox上面的表单中发生变化时,都会使用jquery的append方法注入单选按钮。

好的,这就是我的网络应用程序如何运作的想法。我想要做的是在表单更改后单击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如何在jquery重新加载EmployeeListBox之后单击列表中的第一个单选按钮?

1 个答案:

答案 0 :(得分:0)

我发现了什么问题。我需要给web应用程序加载时间。 我告诉我的java测试用这一行等待1秒钟。

preg_replace("/<font(.*?)>$key2<\/font>/","<b>$key2</b>",$out,1)
相关问题