繁体   English   中英

使用硒Webdriver元素上的click不会产生预期的结果

[英]Click on element does not produce expected outcome using selenium webdriver

首先,我使用Java编写Selenium Webdriver测试,以测试主要用ExtJS编写并在firefox浏览器v14上运行的应用程序。 有趣的是,Selenium可以找到我想要单击的元素,但是单击似乎没有被执行,或者如果执行了该单击,则期望的结果(不会出现弹出窗口)不会发生。 我还在Selenium IDE中验证了我要查找的元素(我通过Xpath找到的span元素)存在,但是在Selenium IDE中,我遇到了无法单击该元素的问题。

如果我手动单击按钮,则会出现弹出窗口,询问要上传的文件。 我还尝试了其他元素,例如span的parent'button'元素,parent'em'元素和parent'div'元素,但都没有运气。

令我感到震惊的是,我已经为该应用程序编写了Selenium Tests数周了,并且始终能够使用此方法单击按钮,并且对于该特定按钮,它不再起作用。

        WebElement uploadButton = driver.findElement(By.xpath("//span[contains(text(), 'Upload Popup')]"));
    uploadButton.click();

编辑1:以为按钮本身的代码可能会有所帮助

<button id="filefield-1158-buttonEl-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="background-color: transparent;">

注意,id是由ExtJS创建的动态ID。

原因可能有很多。 我建议您在测试运行时进行调试。

可以在您的Selenium Firefox实例中安装FireBug:

File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen

WebDriver driver = new FirefoxDriver(firefoxProfile);

我假设您可以通过JUnit(在像Eclipse这样的IDE中)启动测试。 单击按钮之前,以调试模式启动测试并设置断点。 然后通过FireBug检查html代码。 它可能会给您一个开始。

另一个可能性是通过CSS类(By.className)或选择器(By.cssSelector)选择按钮:

WebElement uploadButton = driver.findElement(By.className("x-btn-center"));
uploadButton.click();

如果页面上有多个按钮,则必须使用

List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();

试试看:

     driver.findElements(By.xpath("//button[@class='x-btn-center']").click()

要么

     driver.findElements(By.xpath("//*[@class='x-btn-center']").click()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM