我正在寻找类似于waitForElementPresent检查元素是否在我点击之前显示的东西.我认为这可以通过implicitWait,所以我使用以下内容:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
然后单击
driver.findElement(By.id(prop.getProperty(vName))).click();
Run Code Online (Sandbox Code Playgroud)
不幸的是,有时它等待元素,有时候不等.我找了一会儿找到了这个解决方案:
for (int second = 0;; second++) {
Thread.sleep(sleepTime);
if (second >= 10)
fail("timeout : " + vName);
try {
if (driver.findElement(By.id(prop.getProperty(vName)))
.isDisplayed())
break;
} catch (Exception e) {
writeToExcel("data.xls", e.toString(),
parameters.currentTestRow, 46);
}
}
driver.findElement(By.id(prop.getProperty(vName))).click();
Run Code Online (Sandbox Code Playgroud)
它等了很久,但在超时前它必须等待5次,50秒.有点多.所以我把隐式等待设置为1秒,直到现在一切都好了.因为现在有些事情在超时之前等待10秒,但是其他一些事情在1秒之后会超时.
您如何覆盖代码中存在/可见元素的等待?任何提示都很明显.
我在用:
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
但是对于下面的元素,它仍然会不断失败
driver.findElement(By.id("name")).clear();
driver.findElement(By.id("name")).sendKeys("Create_title_01");
Run Code Online (Sandbox Code Playgroud)
我添加了等待代码:
for (int second = 0;; second++) {
if (second >= 120) fail("timeout");
try { if (isElementPresent(By.id("name"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
不应该隐含等待,直到找到一个元素?如果我使用显式等待而不是我添加的代码,它会更好Thread.sleep()吗?
我只是想知道,在点击链接之前如何让浏览器等待?我的目标是从动态网页上抓取内容,内容是动态的,但我设法得到表单ID.唯一的问题是提交按钮仅在2-3秒后显示.但是,我的Firefox驱动程序在加载页面时立即开始单击链接(而不是动态部分).
有什么方法可以让我的浏览器等待2-3秒,直到出现提交按钮?我尝试使用time.sleep()但暂停一切,提交按钮不会出现,time.sleep但在time.sleep结束后2-3秒后出现.
我正在学习Java Maven Selenium.我想在Selenium中使用这样的东西implicitlyWait.
这是我的简单代码:
package com.org.learningMaven;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class HelloWorldTest {
@Test
public void login() {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("email")).sendKeys("myemail@yahoo.com");
}
private void sendKeys(Keys enter) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
此代码无效.它只需打开Facebook,点击电子邮件字段并输入我的电子邮件ID,而不是在输入我的电子邮件前等待10秒.