I recently used selenium as a simple automation tool, but when I tested it, I felt that WebDriverWait wait = new WebDriverWait(driver,20); had no effect. After I entered the mailbox,
wait.until(ExpectedConditions. presenceOfElementLocated(By.id("_mail_component_68_68"))).click(); //Open the inbox
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@sign='letter'][1]"))).click();
这两段代码明明页面已经有这个元素了,为啥还要等一段时间才进行操作,而且等待是时间也不是固定的,是我电脑问题,很头疼啊,演示的时候非常尴尬的等好长时间。
selenium 用的3.4
火狐用的5.3
static {System.setProperty("webdriver.firefox.marionette","C:Program Files (x86)Mozilla Firefoxgeckodriver.exe");} //Firefox browser driver
static String url="http://freemail.163.com/"; //The address opened by the browser
public String openEmail(Stream<String> userNameSream,Stream<String> passwordStream) throws Exception {
long startTime=System.currentTimeMillis(); //获取开始时间
WebDriver driver = new FirefoxDriver(); //启动浏览器
driver.manage().window().maximize(); //最大化浏览器
WebDriverWait wait = new WebDriverWait(driver,20);
driver.get(url); //打开连接
driver.switchTo().frame("x-URS-iframe"); //定位到iframe框架中
userNameSream.forEach(e -> driver.findElement(By.name("email")).sendKeys(e)); //输入账号
passwordStream.forEach(p -> driver.findElement(By.name("password")).sendKeys(p)); //输入账号
driver.findElement(By.id("dologin")).click(); //点击登录
Thread.sleep(3000);
if(driver.getTitle().contains("网易邮箱")){
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("_mail_component_68_68"))).click(); //打开收件箱
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@sign='letter'][1]"))).click();
return "成功,从程序开始到打开第一封邮件用时"+(System.currentTimeMillis()-startTime)/1000+"秒";
}else{
try {
WebElement loginMsg = driver.findElement(By.className("ferrorhead"));
return loginMsg.getText();
}catch(Exception e){
return "出现其他问题";
}
}
WebDriverWait will not execute click until everything is loaded
I don’t know why, but it solved the problem. I used Firefox and it became faster after switching to Google.