2013-09-28 45 views
0

此代碼在Firefox瀏覽器中正常工作,但在Chrome瀏覽器中未能找到元素時失敗。下面是html的src代碼,我需要找到標題。請讓我知道如何到達那裏?只能在Chrome瀏覽器中找到Iframe標籤下的元素(html)

Java代碼:

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代碼:

<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> 
+0

沒有與CWindow類的ID的iframe在那個html源代碼中,或者iframe的開始標籤。 – Richard

+0

在html標籤上方添加了開放iframe標籤。 – Tan0308

回答

0

你的HTML是無效的。其中,您的<head>可能未被封閉或爲空。事實上,一些瀏覽器渲染它,並允許你通過他們的webdriver查找元素,因爲它們是寬鬆的,並試圖猜測你對HTML的含義。我也看到很多空的<script>標籤和未封閉的<div>標籤讓我相信你沒有發佈確切的HTML代碼。

通過W3C Markup Validator驗證您的HTML。一旦有效,再試一次,你的代碼應該給你匹配。雖然在使用xpath //div[@class='form input']時它不會找到您的標題,因爲對我來說,這似乎是一個xpath來形成輸入。

+0

不知道你是否對此正確.. .. iframe包括該頁面內的任何內容。我認爲他向你展示了iframe的來源。 – sircapsalot

1

driver.switchTo().frame()有能力選擇框架| iframe基於idname屬性。假設你不使用它的道路,可以很容易對自己和剛做,

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

此外,你應該考慮學習CSS。 (By.cssSelector()

iframe#cwindow 

//iframe[@id='cwindow'] 

漂亮多了(太多更快)

至於跨瀏覽器的問題,它可能是什麼@joostschouten說是準確的,大約<head>標記沒有結束,並且chrome可能會對此抱怨。

相關問題