登录后,页面重定向到一个页面(我想等待页面加载),我在那里通过tagName找到元素,
By inputArea = By.tagName("input");
List <WebElement> myIput = driver.findElements(inputArea);
在这里,我想给明确等待findElements,我想等待它的所有可见性或存在。我的网页只有两个输入。如果我给Implicit Wait很长时间,代码就可以了。但它有所不同。所以我决定给出显式等待,我怎样才能明确等待findElements?或者如何从列表中检查第二个的可见性(列出myIput)。即,myIput.get(1)。当我给下面的visibilityOfAllElements()时,它会抛出错误。
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");
以下是我在自动化程序中使用的代码列表。
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");
}
}
请注意:如果我在代码“driver.navigate()。to(baseUrl +”/ anglr / form-builder / dist /#/ forms“)之后给了Thread.sleep很长时间;”,我会得到所有WebElements。但我想避免这种情况,我想等待WebElements加载()。
任何人都请帮忙。
答案 0 :(得分:4)
您可以这样做:
//explicit wait for input field field
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input")));
ExpectedConditions
类在很多情况下都很有用,并提供一些预定义条件来等待元素。以下是其中一些:
alertIsPresent
:警报存在elementSelectionStateToBe
:元素状态是选择。elementToBeClickable
:元素存在且可点击。elementToBeSelected
:已选择元素frameToBeAvailableAndSwitchToIt
:框架可用且框架已选中。invisibilityOfElementLocated
:元素不可见presenceOfAllElementsLocatedBy
:当前元素位于。textToBePresentInElement
:特定元素上的文字textToBePresentInElementValue
:和特定元素的元素值。visibilityOf
:可见的元素。titleContains
:title包含答案 1 :(得分:0)
我决定弄清楚如何解决我的用例,我的用例想对特定的FindElements调用(但不是全部)进行显式等待,摆弄隐式等待感到不干净和可怕。因此,我实现了对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;
});
}