【发布时间】:2017-02-15 13:19:52
【问题描述】:
这是另一个关于 Selenium 和点击的问题。我已经挣扎了大约两天,无法让它发挥作用 - 我已经在互联网上尝试了答案,现在我需要齐心协力。提前致谢!!
我正在以下网站上工作 http://144.76.109.38/peTEST - 如果您想追溯我的步骤,这可能会有所帮助。
我正在尝试填写登录表单,然后单击登录并查看答案页面。
这是我的代码:
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
public class toJava {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/home/tallen/RTI/lib/geckodriver/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("http:144.76.109.38/peTEST");
File SF2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(SF2, new File("./out-004.png"));
}catch(IOException ioe){
System.out.println("There was an IO error");
}
driver.findElement(By.id("user_login_name")).click();
WebElement WE4 = driver.findElement(By.id("user_login_name"));
WE4.sendKeys("Superuser");
driver.findElement(By.id("user_password")).click();
WebElement WE6 = driver.findElement(By.id("user_password"));
WE6.sendKeys("Jkerouac1!");
WebElement WE7 = driver.findElement(By.xpath(".//*[@type='button'][@onclick='login()'][@value='Login']"));
WE7.sendKeys(Keys.ENTER);
File SF8 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(SF8, new File("./out-005.png"));
}catch(IOException ioe){
System.out.println("There was an IO error");
}
driver.quit();
}
}
所以基本上我是打开页面,截图输入用户名和密码,点击登录,然后再截图。
对此的编译和运行是干净的——没有异常也没有问题。我什至得到了两张截图。第一个屏幕截图显示了登录页面 - 没有输入任何数据。第二个屏幕截图显示登录页面已填充,我要单击的按钮已标记,但没有成功登录后您将获得的“欢迎页面”。屏幕截图显示已标记的按钮,我知道我找到了该元素.我已经在脚本中提供了登录信息,以防你想先用 Selenium 试试。
为什么登录按钮没有被“点击”。我试过点击、执行等都无济于事。我什至尝试过隐式等待 - 仍然没有。
我尝试了高级使用交互 - 仍然没有。
我对 Selenium 和 Java 还很陌生,我希望这只是我忽略的一些愚蠢的东西。但是在浏览了网络之后,那里的解决方案并没有帮助。
我正在研究 Debian-70-Wheezy-64-LAMP
我的 Selenium 库来自 client-combined-3.0.1-nodeps.jar
我的 Geckodriver 是 v0.11.1-linux64
感谢您的帮助!!!
【问题讨论】:
-
假设点击登录按钮的语句是
WE7.sendKeys(Keys.ENTER),你为什么要向按钮发送回车键而不是像user_login_name和@那样调用click()987654326@ 字段?我不一定说这是错误的(我不知道也没有检查),但我只会click()它。另外,为什么在user_login_name和user_password字段上调用click()?给他们sendKeys。 -
嗨 SantiBailors - 这只是一种演变。我试过 click() 但这没有用。我在网上其他地方看到了这个提示,所以我试了一下。它可以工作 - 也就是说,它不会产生任何问题,但屏幕截图仍然不正确,这意味着点击没有发生。谢谢!!!
标签: java button selenium-webdriver click