简体   繁体   English

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

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

First the specs, I am writing Selenium Webdriver tests in java for an application written largely in ExtJS and running on the firefox browser v14. 首先,我使用Java编写Selenium Webdriver测试,以测试主要用ExtJS编写并在firefox浏览器v14上运行的应用程序。 The interesting thing is that Selenium can find the element that I want to click, but the click does not seem to be executed or if it is getting executed the desired outcome (a popup appearing) does not happen. 有趣的是,Selenium可以找到我想要单击的元素,但是单击似乎没有被执行,或者如果执行了该单击,则期望的结果(不会出现弹出窗口)不会发生。 I have also verified in Selenium IDE that the element I am looking for (a span element that I locate via Xpath) exists, but in Selenium IDE I run into the same issue of not being able to click on it. 我还在Selenium IDE中验证了我要查找的元素(我通过Xpath找到的span元素)存在,但是在Selenium IDE中,我遇到了无法单击该元素的问题。

If I manually click on the button the popup window appears asking for what file I want to upload. 如果我手动单击按钮,则会出现弹出窗口,询问要上传的文件。 I have also tried other elements such as the span's parent 'button' element and parent 'em' element and parent 'div' element all with no luck. 我还尝试了其他元素,例如span的parent'button'元素,parent'em'元素和parent'div'元素,但都没有运气。

What kills me is that I have been writing Selenium Tests for this application for weeks now and always been able to use this method to click on buttons and for this particular button it no longer works. 令我感到震惊的是,我已经为该应用程序编写了Selenium Tests数周了,并且始终能够使用此方法单击按钮,并且对于该特定按钮,它不再起作用。

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

Edit 1: Thought that the code of the button itself might help 编辑1:以为按钮本身的代码可能会有所帮助

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

Note the id is the dynamic id created by ExtJS 注意,id是由ExtJS创建的动态ID。

It can be because of lots of reasons. 原因可能有很多。 I would suggest that you debug while your test is running. 我建议您在测试运行时进行调试。

It is possible to install FireBug in your Selenium Firefox instance: 可以在您的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);

I assume you can launch your test via JUnit (in an IDE like Eclipse). 我假设您可以通过JUnit(在像Eclipse这样的IDE中)启动测试。 Launch your test in debug mode and set a breakpoint just before clicking on the button. 单击按钮之前,以调试模式启动测试并设置断点。 Inspect the html code via FireBug then. 然后通过FireBug检查html代码。 It might give you a start. 它可能会给您一个开始。

Another posibility is to select the button by css class (By.className) or selector (By.cssSelector): 另一个可能性是通过CSS类(By.className)或选择器(By.cssSelector)选择按钮:

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

If there are multiple buttons on the page you would have to use 如果页面上有多个按钮,则必须使用

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

Try this out: 试试看:

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

or 要么

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

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

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