java - Selenium、Java、屏幕截图和单击按钮的问题

标签 java button selenium-webdriver click

这是另一个关于 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();
        }
}

所以基本上我会打开页面,截取屏幕截图,输入用户名和密码,单击“登录”,然后截取另一个屏幕截图。

编译和运行都很干净——没有异常(exception),没有问题。我什至得到了两张截图。第一个屏幕截图显示了登录页面 - 没有输入任何数据。第二个屏幕截图显示已填充的登录页面,我要单击的按钮已标记,但不是成功登录后您将获得的“欢迎页面”。该屏幕截图显示已标记的按钮,我知道我已找到该元素。我已经在脚本中提供了登录信息,以防您想先使用 Selenium 进行尝试。

为什么登录按钮没有被“点击”。我尝试过点击、执行等,但没有成功。我什至尝试过加入隐式等待 - 仍然没有任何结果。

我尝试过高级使用交互 - 但仍然一无所获。

我对 Selenium 和 Java 还很陌生,希望这只是我忽略的一些愚蠢的事情。但在浏览了网络之后,发现那里的解决方案并没有帮助。

我正在开发 Debian-70-Wheezy-64-LAMP

我的 Selenium 库来自 client-combined-3.0.1-nodeps.jar

我的 Geckodriver 是 v0.11.1-linux64

感谢您的帮助!!!

最佳答案

嗨,
我不知道它是否会导致问题,但无论如何对于登录按钮我会使用

WE7.click();
I just think that it's easier to understand what you're trying to do with the element.

I've been working with the GeckoDriver for a while and talking with some experienced people in the area and they told me that Gecko has many problems that are not fixed yet.
They always recommended me not to use GeckoDriver because it seems to fail very often and told me to use FirefoxDriver instead.
To try it this way, and this is important, you'll just need to keep working with an older version of Firefox as the version 46 that is compatible with FirefoxDriver (that version worked for me and you can download it from places like this) and avoid using GeckoDriver. Also the version 47 seems to work with the FirefoxDriver as I've found here.
Remember: When you install one of these previous versions of Firefox, don't forget to go to settings and disable the automatic updates and background updates because if you don't do this, you'll end soon again with the latest version that requires GeckoDriver.

In addition you can try some validation as the following:

if(WE7.isDisplayed() && WE7.isEnabled()){
    WE7.click();
}

如果您尝试对网络元素执行操作时页面未完全加载,这种验证会有所帮助。如果元素尚未准备好,您将单击它而不会出现错误,但它只是不起作用

希望这也适合您!

关于java - Selenium、Java、屏幕截图和单击按钮的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250541/

相关文章:

testing - SAPUI5 OPA 测试 - 消息框 - 错误

python - 使用 python selenium 的网页抓取问题

java - 更改我的世界中的 "SuperFlat"世界生成器

java - 文本分类,如何将文本字符串转换为 vector 表示

html - 如何合并 g :link into an ordinary button?

python - 当要求查找元素时出现奇怪的 Python Selenium 错误消息

python-3.x - 在后台使用 selenium chrome 浏览器(不是 headless 模式)?

java - BST 仅保留最后插入的值并将其设为根

Java缓存对象到磁盘

jquery-ui - 按钮无法通过 jQuery UI Draggable() 进行拖动