简体   繁体   English

Selenium WebDriver没有通过XPATH找到元素

[英]Selenium WebDriver does not find element by XPATH

I'm trying to write a Selenium Testcase and started with the Selenium IDE in Firefox. 我正在尝试编写Selenium Testcase,并在Firefox中使用Selenium IDE。 The test is working quite fine there. 那里的测试工作得非常好。 Now I want to automate that test with Selenium Webdriver and exported the according JAVA class. 现在我想用Selenium Webdriver自动化测试并导出相应的JAVA类。 Everything is setup and working fine so far (the Internet Explorer window is opening, the according page is displayed). 到目前为止,一切都设置正常(Internet Explorer窗口打开,显示相应的页面)。

BUT: Selenium Webdriver does not find the element. 但是:Selenium Webdriver没有找到该元素。 I want to get the following element: 我想获得以下元素:

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

and I have the following code: 我有以下代码:

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

In the testcase with Selenium IDE the statement is pretty much the same, and it is working: 在使用Selenium IDE的测试用例中,语句几乎相同,并且它正在工作:

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

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. 唯一的区别是,Selenium IDE仅在Firefox中可用,但最后我希望在IE中使用Selenium WebDriver执行测试。

Any help is very much appreciated. 很感谢任何形式的帮助。

==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. ==更新==我执行了driver.getPageSource() ,现在看到我正在寻找的HTML部分不可用,因为它似乎位于iFrame中。 Any ideas? 有任何想法吗?

You can use findElement to get the frame webelement and have it used with the switchto() method. 您可以使用findElement获取帧webelement并将其与switchto()方法一起使用。

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

If you have some other attribute like src, you can try the below. 如果你有一些像src这样的其他属性,你可以试试下面的内容。

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

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

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