Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击元素不会使用selenium webdriver产生预期结果_Java_Junit_Selenium Webdriver_Selenium Ide - Fatal编程技术网

Java 单击元素不会使用selenium webdriver产生预期结果

Java 单击元素不会使用selenium webdriver产生预期结果,java,junit,selenium-webdriver,selenium-ide,Java,Junit,Selenium Webdriver,Selenium Ide,首先是规范,我正在用java编写SeleniumWebDriver测试,用于一个主要用ExtJS编写并在firefox浏览器v14上运行的应用程序。有趣的是,Selenium可以找到我想要单击的元素,但是单击似乎没有被执行,或者如果它被执行,则所需的结果(弹出窗口)不会发生。我还在Selenium IDE中验证了我正在寻找的元素(通过Xpath定位的span元素)是否存在,但在Selenium IDE中,我遇到了同样的问题,即无法单击它 如果我手动单击按钮,弹出窗口将出现,询问我要上载的文件。我

首先是规范,我正在用java编写SeleniumWebDriver测试,用于一个主要用ExtJS编写并在firefox浏览器v14上运行的应用程序。有趣的是,Selenium可以找到我想要单击的元素,但是单击似乎没有被执行,或者如果它被执行,则所需的结果(弹出窗口)不会发生。我还在Selenium IDE中验证了我正在寻找的元素(通过Xpath定位的span元素)是否存在,但在Selenium IDE中,我遇到了同样的问题,即无法单击它

如果我手动单击按钮,弹出窗口将出现,询问我要上载的文件。我还尝试了其他元素,如span的父“button”元素、父“em”元素和父“div”元素,但都没有成功

最要命的是,我已经为这个应用程序编写了几个星期的Selenium测试,并且总是能够使用这个方法来点击按钮,而对于这个特定的按钮,它不再工作

        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类(按.className)或选择器(按.cssSelector)选择按钮:

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

List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();
List buttons=driver.findElements(By.className(“x-btn-center”);
WebElement uploadButton=按钮.get(索引);
上传按钮。单击();

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

可以在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类(按.className)或选择器(按.cssSelector)选择按钮:

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

List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();
List buttons=driver.findElements(By.className(“x-btn-center”);
WebElement uploadButton=按钮.get(索引);
上传按钮。单击();
试试这个:

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

试试这个:

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


这是一个好主意,尽管现在我已经意识到SeleniumWebDriver启动的firefox实例没有firebug,并且简单地为该实例安装插件不会起作用(一旦我尝试调试测试,第二次firebug再次消失)。在我解决了这个小问题后,我会提供一个更新。我知道,通常selenium启动的firefox没有firebug,因此上面的代码片段。您的第二种方法似乎是我应该尝试的方法,尽管目前我很难确定应该是什么索引。我必须导入java.util.List;对于列表,但eclipse IDE仍然不满意indexFor查找索引,请检入firebug或chrome。我理解你的痛苦:)我不得不写一个测试来测试基于Extjs的datagrid。很难编辑单元格、选项卡单元格……这是一个好主意,尽管现在我意识到selenium webdriver启动的firefox实例没有firebug,并且简单地为该实例安装插件也不起作用(一旦我尝试调试测试,第二次firebug再次消失)。在我解决了这个小问题后,我会提供一个更新。我知道,通常selenium启动的firefox没有firebug,因此上面的代码片段。您的第二种方法似乎是我应该尝试的方法,尽管目前我很难确定应该是什么索引。我必须导入java.util.List;对于列表,但eclipse IDE仍然不满意indexFor查找索引,请检入firebug或chrome。我理解你的痛苦:)我不得不写一个测试来测试基于Extjs的datagrid。很难编辑单元格,制表符单元格。。。