2013-06-24 20 views
0

我有一個應用程序,點擊一個按鈕將彈出一個窗口用於創建一個新文件夾。在這個彈出窗口中,我們需要填寫幾個用於創建文件夾的輸入字段。當我試圖通過xpath查找元素並給出(//input[@name='name'])併爲此彈出輸入字段提供輸入(使用sendKeys)時,輸入將填充到父窗口的一個字段中(主窗口位於彈出窗口)。當我嘗試使用整個XPath運行。如何將控制切換到彈出窗口並在Selenium Webdriver中輸入輸入

driver.findElement(By.xpath("//body/div[9]/div[2]/div/div/span/div/table/tbody/tr/td[2]/input")); 

我收到一個異常錯誤,說無法找到元素。

下面是一段HTML代碼

<html class="x-border-box x-quirks x-viewport"> 
<head> 
<body id="ext-gen1020" class="x-body x-gecko x-mac x-layout-fit x-container x-container-default"> 
<div id="workspace-1021" class="x-container x-fit-item x-container-default x-border-layout-ct" style="margin: 0px; width: 1680px; height: 587px;"> 
<div id="ext-quicktips-tip" class="x-tip x-layer x-tip-default" style="right: auto; left: 1605px; top: 105px; height: 26px; z-index: 19011; width: 75px; display: none;"> 
<div id="messagebox-1001" class="x-window x-message-box x-layer x-window-default x-closable x-window-closable x-window-default-closable x-hide-offsets" tabindex="-1" style="height: 114px; right: auto; left: 634px; top: 237px; z-index: 19001; width: 411px;"> 
<div id="ext-gen1055" class="x-mask" style="height: 880px; width: 1680px; z-index: 18997; right: auto; left: 0px; top: 0px;"></div> 
<div id="ext-gen1594" class="x-mask" style="z-index: 18999; width: 1371px; height: 393px; right: auto; left: 307px; top: 159px; visibility: hidden;"></div> 
<div id="loadmask-1105" class="x-mask-msg x-layer x-mask-msg-default" style="right: auto; left: 947px; top: 340px; z-index: 19001; display: none;"> 
<div id="ext-gen1056" class="x-css-shadow" role="presentation" style="z-index: 19000; right: auto; left: 684px; top: 238px; width: 312px; height: 116px; box-shadow: 0px 0px 6px rgb(136, 136, 136); display: none;"></div> 
<div id="ext-gen1048" class="x-css-shadow" role="presentation" style="z-index: 19000; right: auto; left: 584px; top: 157px; width: 512px; height: 277px; box-shadow: 0px 0px 6px rgb(136, 136, 136); display: block;"></div> 
<div id="propertieswindow-1310" class="x-window properties-window x-layer x-window-default x-closable x-window-closable x-window-default-closable x-resizable x-window-resizable x-window-default-resizable" tabindex="-1" style="height: 281px; right: auto; left: 584px; top: 153px; width: 512px; z-index: 19001;"> 
<div id="propertieswindow-1310_header" class="x-window-header x-header x-header-horizontal x-header-draggable x-docked x-unselectable x-window-header-default x-horizontal x-window-header-horizontal x-window-header-default-horizontal x-top x-window-header-top x-window-header-default-top x-docked-top x-window-header-docked-top x-window-header-default-docked-top" style="width: 512px; right: auto; left: -1px; top: -1px;"> 
<div id="propertieswindow-1310-body" class="x-window-body x-window-body-default x-layout-fit x-closable x-window-body-closable x-window-body-default-closable x-window-body-default x-window-body-default-closable x-resizable x-window-body-resizable x-window-body-default-resizable" style="background-color: white; left: 0px; top: 20px; width: 502px; height: 222px;"> 
<div id="form-1311" class="x-panel x-fit-item x-window-item x-panel-default" style="padding: 5px; width: 500px; margin: 0px; height: 220px;"> 
<div id="form-1311-body" class="x-panel-body x-panel-body-default x-panel-body-default x-docked-noborder-top x-docked-noborder-right x-docked-noborder-bottom x-docked-noborder-left" style="width: 490px; left: 0px; top: 0px; height: 210px;"> 
<span id="form-1311-outerCt" style="display: table; width: 100%; table-layout: fixed;"> 
<div id="form-1311-innerCt" class="" style="display:table-cell;height:100%;vertical-align:top;"> 
<table id="textfield-1312" class="x-field x-table-plain x-form-item x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: fixed; width: 490px;"> 
<tbody> 
<tr id="textfield-1312-inputRow"> 
<td id="textfield-1312-labelCell" class="x-field-label-cell" width="55" valign="top" halign="right" style=""> 
<td id="textfield-1312-bodyEl" class="x-form-item-body " role="presentation" colspan="2" style="width: 100%;"> 
<input id="textfield-1312-inputEl" class="x-form-field x-form-required-field x-form-text " type="text" autocomplete="off" name="name" size="1" aria-invalid="false" style="width: 100%;"> 
</td> 
<td id="textfield-1312-sideErrorCell" width="17" valign="middle" style="display: none;"> 
</tr> 
</tbody> 
</table> 
<table id="textfield-1313" class="x-field x-table-plain x-form-item x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: fixed; width: 490px;"> 
<table id="textfield-1314" class="x-field x-table-plain x-form-item x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: fixed; width: 490px;"> 
<div id="panel-1315" class="x-panel x-panel-default" style="width: 490px; height: 119px;"> 
<table id="hiddenfield-1332" class="x-field x-table-plain x-form-item-hidden x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: auto;"> 
<table id="hiddenfield-1333" class="x-field x-table-plain x-form-item-hidden x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: auto;"> 
</div> 
</span> 
</div> 
</div> 
</div> 
<div id="toolbar-1334" class="x-toolbar x-docked x-toolbar-footer x-docked-bottom x-toolbar-docked-bottom x-toolbar-footer-docked-bottom x-box-layout-ct" style="width: 502px; right: auto; left: 4px; top: 246px;"> 
<div id="propertieswindow-1310-north-handle" class="x-resizable-handle x-resizable-handle-north x-window-handle x-window-handle-north x-window-handle-north-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-south-handle" class="x-resizable-handle x-resizable-handle-south x-window-handle x-window-handle-south x-window-handle-south-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-east-handle" class="x-resizable-handle x-resizable-handle-east x-window-handle x-window-handle-east x-window-handle-east-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-west-handle" class="x-resizable-handle x-resizable-handle-west x-window-handle x-window-handle-west x-window-handle-west-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-northeast-handle" class="x-resizable-handle x-resizable-handle-northeast x-window-handle x-window-handle-northeast x-window-handle-northeast-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-northwest-handle" class="x-resizable-handle x-resizable-handle-northwest x-window-handle x-window-handle-northwest x-window-handle-northwest-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-southeast-handle" class="x-resizable-handle x-resizable-handle-southeast x-window-handle x-window-handle-southeast x-window-handle-southeast-br x-unselectable" unselectable="on"></div> 
<div id="propertieswindow-1310-southwest-handle" class="x-resizable-handle x-resizable-handle-southwest x-window-handle x-window-handle-southwest x-window-handle-southwest-br x-unselectable" unselectable="on"></div> 
</div> 
</body> 
</html> 

編輯: 鏈接到圖片:http://imageshack.us/f/811/8thi.png/

+0

Duplicate http://stackoverflow.com/questions/14098679/how-to-switch-from-main-window-to-popup-window – Amey

+0

@amey:我對此表示懷疑。也許OP只是沒有解釋清楚。對於ExtJs,iframe/div窗口很常見,不確定OP是否真的在談論那個窗口或瀏覽器窗口。 –

+0

我已將圖像鏈接添加到原始文章。請看看 – user2479333

回答

0

哦,你的情況這既不是瀏覽器窗口,也沒有iframe中彈出。

所以你只需要定位它正常。

我會建議如下定位:

driver.findElement(By.cssSelector(".x-window input[name='name']")); 

這是根據您所提供的信息,請給一些反饋,因爲它可能無法正常工作。

+0

非常感謝你......它的工作...... – user2479333

0

我謙卑地通過你使用某種谷歌Web模塊的你「X-」代碼中看到的/ (類似於polymer.js),這可能會隱藏我們的某些東西。您可能需要在Chrome網絡開發者工具中啓用shadow-DOM查看才能查看和查看這些對象中的源代碼。

但是,試圖回答你的問題,給定有限的信息,如果這只是一個簡單的彈出窗口,所有你需要做的就是使用Selenium的「driver.switchTo()」方法,然後使用JavaScriptExecutor來獲得用「window.focus()」彈出窗口焦點,然後在彈出窗口上操作並關閉它以返回到父窗口。

編輯:還應當指出的是,硒二元鉻驅動程序支持影子DOM(自2015年1月28日):http://chromedriver.storage.googleapis.com/2.14/notes.txt

相關問題