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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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 Selenium Webdriver中显式等待findElements_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium Webdriver中显式等待findElements

Java Selenium Webdriver中显式等待findElements,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,登录后,页面将重定向到一个页面(我想等待页面加载),在这里我将按标记名查找元素 By inputArea = By.tagName("input"); List <WebElement> myIput = driver.findElements(inputArea); 按inputArea=按.tagName(“输入”); List myIput=driver.findElements(inputArea); 在这里,我要对findElements进行显式的等待,我要等待它的所

登录后,页面将重定向到一个页面(我想等待页面加载),在这里我将按标记名查找元素

By inputArea = By.tagName("input");
 List <WebElement> myIput = driver.findElements(inputArea);
按inputArea=按.tagName(“输入”);
List myIput=driver.findElements(inputArea);
在这里,我要对findElements进行显式的等待,我要等待它的所有可见性或存在性。我的网页中只有两个输入。如果我让隐式等待很长时间,代码就会工作。但情况各不相同。所以我决定给予显式等待,我如何给予显式等待?。或者如何检查列表中第二个(列表myIput)的可见性。例如,myIput.get(1)。当我给出AllElements()的可见性时,如下面所示,它会抛出错误

WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);      
By inputArea = By.tagName("input");
List <WebElement> myIput = driver.findElements(inputArea);
waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput));
myIput.get(1).sendKeys("Test");
webdriverwaitforformlabel=新的WebDriverWait(驱动程序,30);
按输入区域=按.tagName(“输入”);
List myIput=driver.findElements(inputArea);
waitForFormLabel.until(所有元素的预期条件可见度(myIput));
myIput.get(1).sendKeys(“测试”);
下面是我在自动化程序中使用的代码列表

package mapap;
import java.util.ArrayList;
import java.util.List;

import lib.ReadExcellData;
import lib.WriteExcellData;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class EditForm {
    public static WebDriver driver;
    static String excelName         = "D:\\xlsx\\map2.xlsx";
    ReadExcellData readData         = new ReadExcellData(excelName);
    WriteExcellData writeData       = new WriteExcellData(excelName); 
    String baseUrl                   = readData.getExcellData("base", 0, 0);    
    By colRadio;
    ExtentReports  report;
    ExtentTest logger;

    @BeforeClass
    public void browserOpen() throws Exception{

        report = new ExtentReports("D:\\driver\\extentRepo\\Edit Map Forms.html", true);
        logger = report.startTest("Map Forms Automation Testing", "Adding Forms");

        driver = new FirefoxDriver();       
        driver.get(baseUrl);
        String username = readData.getExcellData("user", 1, 0);
        String password = readData.getExcellData("user", 1, 1); 
        WebDriverWait waitForUserName = new WebDriverWait(driver, 250);
        By usernameField = By.name("username");
        waitForUserName.until(ExpectedConditions.elementToBeClickable(usernameField)).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.xpath("//input[contains(@src,'/images/signin.png')]")).click();

    }

    @Test(priority = 1)
    public void addingForm() throws Exception{      
            driver.navigate().to(baseUrl+"/anglr/form-builder/dist/#/forms");
        driver.manage().window().maximize();       
        WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);      
        By inputArea = By.tagName("input");
        List <WebElement> myIput = driver.findElements(inputArea);
        waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput));
        myIput.get(1).sendKeys("Test");


    }

}
packagemapap;
导入java.util.ArrayList;
导入java.util.List;
导入lib.readExcelData;
导入lib.WriteExcellData;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.Select;
导入org.openqa.selenium.support.ui.WebDriverWait;
导入org.testng.annotations.BeforeClass;
导入org.testng.annotations.Test;
导入com.relevantcodes.extentreports.extentreports;
导入com.relevantcodes.extentreports.extendettest;
导入com.relevantcodes.extentreports.LogStatus;
公共类编辑表单{
公共静态WebDriver;
静态字符串excelName=“D:\\xlsx\\map2.xlsx”;
ReadExcelData readData=新的ReadExcelData(excelName);
WriteExcellData writeData=新的WriteExcellData(excelName);
字符串baseUrl=readData.getExcelData(“base”,0,0);
通过无线电;
扩展报告;
伸长计记录器;
@课前
public void browserveOpen()引发异常{
报告=新的扩展端口(“D:\\driver\\extentRepo\\Edit Map Forms.html”,true);
logger=report.startTest(“映射表单自动化测试”、“添加表单”);
驱动程序=新的FirefoxDriver();
get(baseUrl);
字符串username=readData.getExcelData(“用户”,1,0);
字符串密码=readData.getExcelData(“用户”,1,1);
webdriverwaitforusername=新的WebDriverWait(驱动程序,250);
By usernameField=By.name(“用户名”);
waitForUserName.until(ExpectedConditions.elementToBeClickable(username字段)).sendKeys(用户名);
driver.findElement(按名称(“密码”)).sendKeys(密码);
findElement(By.xpath(//input[contains(@src,'/images/sign.png')))))。单击();
}
@测试(优先级=1)
public void addingForm()引发异常{
driver.navigate().指向(baseUrl+“/anglr/form builder/dist/#/forms”);
driver.manage().window().maximize();
WebDriverWaitForFormLabel=新的WebDriverWait(驱动程序,30);
按输入区域=按.tagName(“输入”);
List myIput=driver.findElements(inputArea);
waitForFormLabel.until(所有元素的预期条件可见度(myIput));
myIput.get(1).sendKeys(“测试”);
}
}
请注意:如果我在代码“driver.navigate().to(baseUrl+”/anglr/form builder/dist/#/forms”);”之后很长时间使用Thread.sleep,我将获得所有WebElements。但我想避免这种情况,我只想等待WebElements加载()


任何人都可以帮忙。

你可以这样做:

//explicit wait for input field field
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input")));
ExpectedConditions
类在很多情况下都很有用,它提供了一组预定义的条件来等待元素。以下是其中一些:

  • 警报显示
    :警报显示
  • element selections statetobe
    :元素状态为selection
  • element可单击
    :存在一个元素并可单击
  • 要选择的元素
    :已选择元素
  • frame-to-beavailable和switch-to-it
    :frame可用并已选择frame
  • 不可见FelementLocated
    :元素不可见
  • 链接的所有元素的呈现:由定位的呈现元素
  • texttobepresentElement
    :特定元素上存在的文本
  • textToBePresentElementValue
    :和特定元素的元素值
  • 可视性
    :可见的元素
  • 标题内容
    :标题包含

我决定弄清楚如何解决我的用例,我的用例想要在特定的findelement调用上进行显式等待,但不是所有调用,摆弄隐式等待感觉既不干净又可怕。因此,我实现了对IWebDriver的扩展方法,该方法使用WebDriverWait(请参阅Selenium中的显式等待)来实现这一点

    /// <summary>
    /// Allows you to execute the FindElements call but specify your own timeout explicitly for this single lookup
    /// </summary>
    /// <remarks>
    /// If you want no timeout, you can pass in TimeSpan.FromSeconds(0) to return an empty list if no elements match immediately. But then you may as well use the original method
    /// </remarks>
    /// <param name="driver">The IWebDriver instance to do the lookup with</param>
    /// <param name="findBy">The By expression to use to find matching elements</param>
    /// <param name="timeout">A timespan specifying how long to wait for the element to be available</param>
    public static ReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By findBy, TimeSpan timeout)
    {
        var wait = new WebDriverWait(driver, timeout);
        return wait.Until((d) =>
        {
            var elements = d.FindElements(findBy);
            return (elements.Count > 0)
                ? elements
                : null;
        });
    }
//
///允许您执行FindElements调用,但要为此单一查找明确指定自己的超时
/// 
/// 
///如果不希望超时,可以传入TimeSpan.FromSeconds(0)以在没有立即匹配的元素时返回空列表。但是你也可以使用原来的方法
/// 
///用于执行查找的IWebDriver实例
///用于查找匹配元素的By表达式
///指定等待元素可用的时间跨度
公共静态只读集合FindElements(此IWebDriver驱动程序,按findBy,TimeSpan超时)
{
var wait=新的WebDriverWait(驱动程序,超时);
返回等待时间((d)=>
{
变量元素=d.Fi