我想点击'撰写' gmail收件箱页面中的按钮。
虽然我已经提供了有效的用户名和密码,但我的脚本无法使用。
WebDriver driver=new FirefoxDriver();
driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("next")).click();
Thread.sleep(1000);
WebElement Err=driver.findElement(By.xpath("//*[@id='errormsg_0_Email']"));
String errtext=Err.getText();
if(!Err.isDisplayed()){
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
Thread.sleep(1000);
List<WebElement> errorDivs = driver.findElements(By.xpath("//div[@class='error-msg']"));
if(errorDivs.isEmpty()){
driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click();
System.out.println("User loged in successfully");
}else{
errorDivs.get(0).getText();
driver.quit();
}
}else{
System.out.println("wrong password");
}
}
答案 0 :(得分:0)
在Gmail中,&#39; ID&#39;元素永远不会一致。
更改您的代码,以便找到&#39;撰写&#39;来自
的按钮driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click();
到
driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();
希望有所帮助。
答案 1 :(得分:0)
我可以在2次更改后运行您的脚本:
1.Comment driver.manage().timeouts().implicitlyWait(500, TimeUnit.SECONDS); ,you used 2 times in your script and specified 500 so it takes time.You can use explicit wait for specific element.Please read [this][1] .
2.Replace driver.findElement(By.xpath("//*[@id=':i7']/div/div")).click(); with driver.findElement(By.xpath("//div[text()='COMPOSE']")).click();, which is suggested by Vikas.
[1]: http://www.toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/
答案 2 :(得分:0)
以下是登录&amp;的代码从Gmail发送邮件
//load login page
driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en");
//login logic
driver.findElement(By.id("Email")).sendKeys(userName);
driver.findElement(By.cssSelector("[value='Next']")).click();
driver.findElement(By.id("Passwd")).sendKeys(password);
driver.findElement(By.id("signIn")).click();
//click on Compose button
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
//typing subject & to address
driver.findElement(By.xpath("//*[text()='To']/../../..//textarea")).sendKeys(to);
driver.findElement(By.name("subjectbox")).sendKeys(subject);
//click on Send
driver.findElement(By.xpath("//div[text()='Send']")).click();
P.S:你需要在不同的地方适当地等待元素正确加载。否则上面的代码将抛出异常。