Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.openqa.selenium.NoSuchElementException:无法定位元素:_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java org.openqa.selenium.NoSuchElementException:无法定位元素:

Java org.openqa.selenium.NoSuchElementException:无法定位元素:,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我无法单击某个按钮,并且由于无法定位元素而引发异常,并在屏幕截图的标记行上引发异常 加载按钮需要一些时间,我已经将时间增加到100秒,但这并没有修复错误 代码: public static WebElement viewShipment(WebDriver driver, String mskuType) { WebElement noOfShipment = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipme

我无法单击某个按钮,并且由于无法定位元素而引发异常,并在屏幕截图的标记行上引发异常

加载按钮需要一些时间,我已经将时间增加到100秒,但这并没有修复错误

代码:

public static WebElement viewShipment(WebDriver driver, String mskuType) {
    WebElement noOfShipment = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']"));
    WebDriverWait wait = new WebDriverWait(driver, 15);
    List<WebElement> shipmentList = noOfShipment.findElements(By.tagName("tr"));
    int shipmentCount = shipmentList.size();

    for (int row=1;row<shipmentCount;row=+1)
    {
        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);
        for(int i=0;i<2;i++)
        {
            try{
                if(mskuType.equalsIgnoreCase("single"))
                {
                    if(noOfSku.equalsIgnoreCase("1"))
                    {
                        Thread.sleep(10000);
                        WebElement workOnShipmentWE = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.visibilityOf(workOnShipmentWE));
                        workOnShipmentWE.click();
                        break;
                    }
                }
                else if(mskuType.equalsIgnoreCase("multiple"))
                {
                    if(noOfSkuValue>1)
                    {
                        WebElement moreThanOneUnit = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.elementToBeClickable(moreThanOneUnit));
                        moreThanOneUnit.click();
                        break;
                    }
                }
            }
            catch(Exception e)
            {
                driver.navigate().refresh();

                e.getMessage();
            }
        }
    }
    return element;
}   
publicstaticwebelement视图装运(WebDriver驱动程序,字符串mskuType){
WebElement noOfShipment=driver.findElement(By.xpath(“./*[@id='fba-core-workflow-shipment-summary-shipment']);
WebDriverWait wait=新的WebDriverWait(驱动程序,15);
List-shipmentList=noOfShipment.findElements(按.tagName(“tr”));
int shipmentCount=shipmentList.size();

对于(int row=1;row1)。如果页面加载速度太慢,请尝试以下预期条件:

 WebElement myDynamicElement = (new WebDriverWait(driver, **10**)).until(**ExpectedConditions.presenceOfElementLocated**(By.id("fba-core-workflow-shipment-summary-shipment")));
将代码保留为
viewShipment
方法的第一条语句。这将确保web驱动程序在配置的时间(代码中的10秒)内等待元素(在本例中由Id指定)加载。如果页面加载太慢,请根据您的要求增加此值。

使用ExpectedCondition优于Thread.sleep的优点是,只要找到一个元素,它就会立即返回(无需等待10秒,例如Thread.sleep)

这将在抛出TimeoutException之前等待10秒,或者如果它找到元素,将在0-10秒后返回它。默认情况下,WebDriverWait每500毫秒调用ExpectedCondition一次,直到它成功返回为止。ExpectedCondition函数类型的成功返回值是布尔值true或非null对象。

  • 如果要查找的元素是iframe或新窗口的一部分: 如果元素位于框架内(检查您尝试查找的元素是否为iframe标记的子元素),则首先查找框架,然后切换到它。然后使用XPath查找元素
  • 如果元素位于新窗口中(如单击链接时在“新建”选项卡中打开),则首先查找该窗口,然后切换到该窗口,然后尝试查找该元素

    参考资料:


  • 您是否尝试过在XPath中不使用
    。例如
    /*[@id='fba-core-workflow-shipping-summary-shipping']
    没有。和没有之间有什么不同。?我不认为
    是启动xpath的可接受字符。因此,尝试一下。尝试一下没有。但是问题是相同的。请尝试WaitForPageLoad。我尝试了相同的方法,并将给定的行更改为该方法的第一行。但是问题发生了相同的。请e建议我进一步调试,在运行时暂停执行(使用100秒睡眠或使用扫描仪读取一些输入),并在加载的页面上尝试xpath(使用FireBug插件)并查看是否有任何匹配的元素。是的元素在页面上匹配,但加载按钮比加载按钮需要一些额外的时间。有什么想法吗?根据建议,从10秒(new WebDriverWait(driver,10))增加到更高的值,例如120秒。一旦识别该元素,将返回该方法(不会等到120秒)我增加了时间,但结果是一样的,有一个iframe标记,但它在不同的div标记中。这会是一个问题吗?
     WebElement myDynamicElement = (new WebDriverWait(driver, **10**)).until(**ExpectedConditions.presenceOfElementLocated**(By.id("fba-core-workflow-shipment-summary-shipment")));