Java 在页面加载之前,我们如何编写“waitforajax”函数,并在其他测试方法中使用相同的函数?

Java 在页面加载之前,我们如何编写“waitforajax”函数,并在其他测试方法中使用相同的函数?,java,ajax,selenium,selenium-webdriver,Java,Ajax,Selenium,Selenium Webdriver,我尝试了不同的等待选项,但无法单击按钮,我觉得ajax加载页面需要时间。我怎样才能为wait-for-ajax创建一个函数,并在我需要它的地方在其他方法中使用它呢 for (int row=1;row<shipmentCount;row=+1) { try{ WebElement onOfSkuWE= driver.findElement(By.xpath(".//*[@id='fba-core-workflow-ship

我尝试了不同的等待选项,但无法单击按钮,我觉得ajax加载页面需要时间。我怎样才能为wait-for-ajax创建一个函数,并在我需要它的地方在其他方法中使用它呢

for (int row=1;row<shipmentCount;row=+1)
        {

            try{

            WebElement onOfSkuWE= driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[3]"));
            String noOfSku = onOfSkuWE.getText();
            int noOfSkuValue = Integer.parseInt(noOfSku);

            if(mskuType.equalsIgnoreCase("single"))
            {
            if(noOfSku.equalsIgnoreCase("1"))
            {
                waitForAjaxRefresh(driver);
                driver.findElement(By.xpath("..//button["+row+"][@name='Work on shipment']")).click();
                break;
            }
            }
我无法在driver.findElementBy.xpath..//button[+row+][@name='Work on shipping']上找到异常。单击;这条线

<tbody id="fba-core-workflow-shipment-summary-shipment">
<tr>
<td>                      FBA (11/7/16 7:25 PM) - 1              </td>
<td>FBA43WZTCC</td>
<td class="number total-line-items">1</td>
<td class="number total-quantity">1</td>
<td>
<td>
<button class="amznBtn btn-lg-pri-arrowr" onclick="window.location='#FBA43WZTCC/prepare'" style="width: 28ex;" type="button" name="Work on shipment">
<p class="action-links content-bottom">
<p class="action-links">
<p/>
</td>
</tr>
<tr style="display: none;">
<tr>
<tr style="display: none;">
</tbody>

使用显式等待元素定位。在抛出异常之前,它将等待条件或提供的最大等待时间

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); // pass time in seconds as much you need
WebElement myElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your element's id")));
这是鲁比,但是

Selenium正在尝试完成一些操作,例如在验证目标元素是否已呈现在页面上之前单击按钮或链接。硒可以更耐心,但你必须明确要求他耐心

例如,如果您正在测试发出AJAX请求的内容,您可以在Ruby中尝试以下内容:

# timeout is in seconds
def wait_for_ajax(timeout=x)
 time_limit, interval = (Time.now + timeout), 0.5
 loop do
   break if @driver.execute_script "return jQuery.active == 0"
   sleep interval
   raise "Wait for AJAX timed out after waiting for #{timeout} seconds" if Time.now > time_limit
 end
end
为了确保您的测试完全全面,请始终使Selenium在运行任务之前等待加载元素


这是针对AJAX请求的,但是如果这与AJAX没有特别的关系,那么您只需要使用隐式seleniumwait。检查此链接:

向我们展示您尝试过的内容,或您迄今为止调查过的内容是将该行置于方法顶部,但结果相同WebElement noOfShipment=new WebDriverWaitdriver,100.UntelexpectedConditions.presenceOfElementLocatedBy.idfba-core-workflow-shipment-summary-shipment;