java - 仅在 Chrome 浏览器中无法在 Iframe 标记下找到元素 (html)

标签 java selenium selenium-webdriver

此代码在 Firefox 浏览器中运行良好,但在 Chrome 浏览器中运行失败,提示无法找到元素。下面是html src代码,我需要找到标题。请告诉我如何到达那里?

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>

最佳答案

driver.switchTo().frame()能够选择框架 |基于iframeidname属性。假设您以后不会使用它,请让自己轻松一点,然后就这样做,

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

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

iframe#cwindow

漂亮多了
//iframe[@id='cwindow']

(也快得多)

就跨浏览器问题而言,@joostschouten 所说的关于 <head> 的内容可能是准确的。标签没有结束,chrome 可能会提示这一点。

关于java - 仅在 Chrome 浏览器中无法在 Iframe 标记下找到元素 (html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19070671/

相关文章:

java - 尽管我有权访问该文件,但 Files.isExecutable(...) 给出 false

java - 为什么 Java 在 While 循环中跳过我的 If 语句?

java - 无法在 Firefox 上打开默认配置文件

Java Selenium 。通过文本查找特定 xpath 的元素

selenium - 带复选框的下拉列表,需要选择和取消选择具有选择功能的复选框并使用 Selenium 获取关联值

java - 为什么我的页面在 Android 中无法转到下一个页面

python - 如何修复这些错误以便我最终可以使用 'webdriver.Chrome()' ?

java - Selenium/Java - 使用 PLUpload 上传文件

python - Selenium 打开 Web 浏览器,但不打开目标 url (InvalidArgumentException : Message: invalid argument)

Java:大量的MouseListeners与像素/网格划分相比如何?