【问题标题】:Selecting an html element that is loaded after documents loads, using selenium find element by xpath to select the element选择在文档加载后加载的html元素,使用selenium find element by xpath来选择元素
【发布时间】: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


    【解决方案1】:

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

    try {Thread.sleep(2000);}catch (InterruptedException ex) {  } 
    

    【讨论】:

      猜你喜欢
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多