Selenium WebDriver没有通过XPATH找到元素 [英] Selenium WebDriver does not find element by XPATH

查看:485
本文介绍了Selenium WebDriver没有通过XPATH找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写Selenium Testcase,并在Firefox中使用Selenium IDE。
测试在那里工作得很好。现在我想用Selenium Webdriver自动化测试并导出相应的JAVA类。
到目前为止,一切都设置正常(Internet Explorer窗口打开,显示相应的页面)。

I'm trying to write a Selenium Testcase and started with the Selenium IDE in Firefox. The test is working quite fine there. Now I want to automate that test with Selenium Webdriver and exported the according JAVA class. Everything is setup and working fine so far (the Internet Explorer window is opening, the according page is displayed).

但是:Selenium Webdriver没有找到该元素。
我想获得以下元素:

BUT: Selenium Webdriver does not find the element. I want to get the following element:

<div class="x-grid3-cell-inner x-grid3-col-instance/status" unselectable="on">Open</div>

我有以下代码:

    WebElement openWorkItem = driver.findElement(By.xpath("//div[contains(text(),'Open')]"));
System.out.println(openWorkItem.getText());

在使用Selenium IDE的测试用例中,语句基本相同,并且正在运行:

In the testcase with Selenium IDE the statement is pretty much the same, and it is working:

<td>//div[contains(text(),'Open')]</td>

唯一的区别是,Selenium IDE仅适用于Firefox,但最终我想要使用Selenium WebDriver在IE中执行测试。

The only difference is, that the Selenium IDE is only available in Firefox, but in the end I want to have the test executed in IE with Selenium WebDriver.

非常感谢任何帮助。

==更新==
我执行了 driver.getPageSource(),现在看到我要查找的HTML部分不可用,因为它似乎位于iFrame中。任何想法?

==Update== I executed driver.getPageSource() and now see that the HTML part I am looking for is not available because it seems to be located in an iFrame. Any ideas?

推荐答案

您可以使用findElement获取帧webelement并将其与switchto()方法一起使用。

You can use findElement to get the frame webelement and have it used with the switchto() method.

driver.switchTo().frame(driver.findElement(By.xpath("iframe[contains(@name=pngNewC‌​ase1143241142570_IFrame)]")));

如果您有其他属性,如src,您可以尝试以下。

If you have some other attribute like src, you can try the below.

WebElement frame=driver.findElement(By.xpath("//iframe[@src='showNewClaimForm.action']");
driver.switchTo().frame(frame);

这篇关于Selenium WebDriver没有通过XPATH找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆