无法使用Web驱动程序切换到框架

时间:2017-01-12 16:27:59

标签: java eclipse selenium automated-tests

我无法切换到框架,因为它没有名称或ID。我需要能够在输入字段中输入值。

网页:http://phptravels.net/flightsw

框架:

    <!DOCTYPE html>
<html class=" js flexbox canvas canvastext webgl no-touch geolocation postmessage no-websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow cssanimations csscolumns cssgradients no-cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths" lang="en">
<head>
<body id="flights" class="home">
<a href="http://www.phptravels.net">
<link href="https://dl.dropbox.com/s/drqq1bid1y9x7qu/wego.css?dl=0" rel="stylesheet" media="screen">
<link rel="shortcut icon" href="http://phptravels.net/uploads/global/favicon.png">
<header>
<div></div>
<div id="content">
<div id="wan-flights-form" style="display: block;">
<div class="wan-wrap">
<form id="wg_flight_search" accept-charset="UTF-8" action="" method="post">
<div style="margin:0;padding:0;display:inline">
<h3>
<fieldset>
<span class="form-triptype">
<span class="form-from">
<span class="form-to">
<span class="form-date">
<span class="form-date">
<span class="form-cabin-class">
<span class="form-extrafields">
<span class="form-button">
</fieldset>
</form>
<div class="clearfix"></div>
</div>
<div class="expand-container"></div>
</div>
<div id="wan-top-flights-destinations">
<script id="template-flight-destination" type="text/html">
<div id="wan-flights-results">
<script id="template-out-of" type="text/mustache">
<script id="template-found" type="text/mustache">
<script id="template-flight-card" type="text/mustache">
<script id="template-airline-card" type="text/mustache">
<script id="template-flight-details" type="text/mustache">
</div>
<div></div>
<footer>
<div class="flights-info-bar">
<script src="/assets/shared/theme-af487a9f719ca343821ae5a49209ad72.js" type="text/javascript">
<script src="/assets/generate/javascripts/customisations.js" type="text/javascript">
<div id="lightboxOverlay" class="lightboxOverlay" style="display: none;"></div>
<div id="lightbox" class="lightbox" style="display: none;">
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem" style="z-index: 1; top: 0px; left: 0px; display: none;"></ul>
<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>
</body>
</html>
</iframe>

我尝试了以下操作(但由于ID不正确而无法正常工作:

driver.switchTo().frame(driver.findElement(By.id("wg_flight_search"))); 

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Could not find element: {"using":"id","value":"flights_search_to_location_name"} (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds

下面是我想在切换发生后执行的内容。

driver.findElement(By.id("flights_search_to_location_name")).sendKeys("Miami");

1 个答案:

答案 0 :(得分:0)

有几种方法可以切换到一个框架。其中一种方法是使用帧索引。因此,应用此案例可能会对您有所帮助。要切换到您的框架,请使用此代码段 -

driver.switchTo().frame(0); // I assume you are selecting first frame

现在,做你的东西,然后切换回你的默认框架。

请在此处查看已接受的答案 -

How to switch between frames in Selenium WebDriver using Java