登录后,页面将重定向到一个页面(我想等待页面加载),在这里我通过tagName查找元素,
By inputArea = By.tagName("input"); ListmyIput = driver.findElements(inputArea);
在这里,我想给Explicit Wait等待findElements,我要等待它的所有可见性或存在性。我的网页中只有两个输入。如果我让隐式等待时间很长,代码将起作用。但这有所不同。因此,我决定给Explicit Wait,如何给露骨的Wait for findElements?或如何从列表(列表myIput)中检查第二个的可见性。即myIput.get(1)。当我像下面这样给可视性OfAllElements()时,它将引发错误。
WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30); By inputArea = By.tagName("input"); ListmyIput = 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"); ListmyIput = driver.findElements(inputArea); waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput)); myIput.get(1).sendKeys("Test"); } }
请注意:如果在代码“ driver.navigate()。to(baseUrl +” / anglr / form-builder / dist /#/ forms”);”之后给了Thread.sleep很长时间,我将获得所有WebElement。但是我想避免这种情况,我只想等待WebElements加载()。
有人请帮忙。
您可以执行以下操作:
//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
:标题包含