0

This code works fine in Firefox browser, but failing in Chrome browser saying not able to find element. Below is the html src code, i need to locate header .Kindly let me know how to get there ?

JAVA code:

WebElement frame = base.getWebDriver().findElement(By.xpath("//iframe[@id='cwindow']"));
base.getWebDriver().switchTo().frame(frame);
Thread.sleep(5000);
WebDriver driver = gen.getWebDriver();
WebElement form = base.getWebDriver().findElement(By.xpath("//div[@class='form input']"));

HTML code:

<iframe id="cwindow" name="cwindow" src="https://incentivesqa.ford.com/Flip/?app_context=t2&lang=en&make=Ford&model=Mustang&year=2014&zipcode=48126&paCode=03050&leadsource=FDAF-Inventory&altleadsource=SI&env=getlocaloffers&pageName=fv: si: vls: ford mustang&plan_type=MSRP">
<!DOCTYPE html>
<html>
<head>
<body>
<script src="/Scripts/Ford.Omniture.Control.js" type="text/javascript">
<script type="text/javascript">
<script type="text/javascript">
<script type="text/javascript">
<script type="text/javascript">
<div class="header">
<div class="clear"></div>
<div class="incentive">
<script type="text/javascript">
<div class="flipFilter">
<select style="display : none;" name="modelDescription">
<div>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="lcol" style="">
<div class="rcol">
<script type="text/javascript">
<div class="form thankYou">
<div class="form working">
<div class="form input">
<h2>Fill out the form below to receive special offers</h2>
<p class="error">
<table cellspacing="0" cellpadding="0" border="0">
<input id="address" type="hidden" value="" name="address">
</div>
<div class="aprLinkContainer">
</div>
<div class="clear"></div>
<div id="printContainer" style="display:none;">
<script type="text/javascript">
<script type="text/javascript">
</div>
<div id="omnitureTarget"></div>
<div id="dartTarget">
<div id="efficientFrontierTarget"></div>
</body>
</html>
</iframe>
4

3 回答 3

1

driver.switchTo().frame()有能力选择框架| iframe 基于idorname属性。假设你没有在路上使用它,让自己轻松一点,然后做,

base.getWebDriver().switchTo().frame("cwindow");

此外,您应该考虑学习 CSS。( By.cssSelector())

iframe#cwindow

比漂亮多了

//iframe[@id='cwindow']

(也快得多)

至于跨浏览器问题,@joostschouten 所说的可能是准确的,关于<head>标签没有结束,chrome 可能会抱怨这一点。

于 2013-09-29T09:25:44.560 回答
0

您的 HTML 无效。除其他外,您的<head>可能不是未封闭或空的。一些浏览器渲染它并允许您通过他们的 webdriver 查找元素的事实是因为它们很宽容并试图猜测您对 HTML 的含义。我还看到很多空<script>标签和未闭合的<div>标签,让我相信您没有发布确切的 HTML 代码。

通过W3C Markup Validator验证您的 HTML 。一旦有效,再试一次,你的代码应该给你匹配。尽管在使用 xpath 时它不会找到您的标题,//div[@class='form input']因为在我看来,这似乎是形成输入的 xpath。

于 2013-09-29T07:54:02.160 回答
0

您可能在 chrome 中遇到无法切换到框架的问题

https://code.google.com/p/chromedriver/issues/detail?id=107

解决方法在这里

https://code.google.com/p/chromedriver/issues/detail?id=107#c11

于 2013-09-29T14:13:14.863 回答