2016-10-19 3 views
0

Я пытаюсь нажать кнопку OK в диалоговом окне. Как я могу найти xpath, чтобы найти кнопку и нажать, чтобы закрыть окно. Это не в любом Iframe. Вот мой код:Как я могу обрабатывать диалоговое окно, чтобы нажимать кнопку в selenium webdriver

`<div id="YesNoDialogBox" class="dialogbox errorDialog firepath- 
 
matching-node" style="display: block;"> 
 
<div class="errWarnMsg"> 
 
<div class="popupHeaderContainer"> 
 
<div class="imgicon"><img src="/web/images/error.png" class="vMiddle" 
 
tabindex=""></div> 
 
\t <h1>Warning message</h1> 
 
\t <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> 
 
\t <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> 
 
\t </a> 
 
\t <div class="clear"></div> \t 
 
\t <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> 
 
\t \t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t Please Confirm to change warehouse 
 
\t \t \t \t <br><br> 
 
    \t \t \t \t \t Do you want to continue? 
 
\t \t \t \t </span> 
 
\t </div> 
 
\t <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> 
 
\t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t Please Confirm to change Customer 
 
\t \t \t <br><br> 
 
    \t \t \t \t Do you want to continue? 
 
\t \t </span> \t \t 
 
\t </div> 
 
\t <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> 
 
\t \t <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> 
 
\t \t 
 
\t \t <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> 
 
\t </div> 
 
</div> 
 
</div> 
 
</div>`<div id="YesNoDialogBox" class="dialogbox errorDialog firepath- 
 
    matching-node" style="display: block;"> 
 
    <div class="errWarnMsg"> 
 
    <div class="popupHeaderContainer"> 
 
     <div class="imgicon"><img src="/web/images/error.png" class="vMiddle" tabindex=""></div> 
 
\t \t <h1>Warning message</h1> 
 
\t \t <!-- Start: Warehouse change confirmation popup --><a href="#" id="close" class="closeClass warehouseCancel" tabindex=""> 
 
\t \t <img src="/web/images/close.png" class="closeIcon errWarnIcoClose" tabindex=""> 
 
\t \t </a> 
 
\t \t <div class="clear"></div> \t 
 
\t \t <div class="msgBody paddingTopBtm2 textLeft padLeft10 warehouseText" style=""> 
 
\t \t \t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t \t Please Confirm to change warehouse 
 
\t \t \t \t \t <br><br> 
 
    \t \t \t \t \t Do you want to continue? 
 
\t \t \t \t \t </span> 
 
\t \t </div> 
 
\t \t <div class="msgBody paddingTopBtm2 textLeft padLeft10 customerText" style="display:none"> 
 
\t \t \t <span class="warningText" id="errorTextWarning"> 
 
\t \t \t \t Please Confirm to change Customer 
 
\t \t \t \t <br><br> 
 
    \t \t \t \t Do you want to continue? 
 
\t \t \t </span> \t \t 
 
\t \t </div> 
 
\t \t <div id="dialogboxNav" class="padding10 textLeft padLeft10 marginleft40"> 
 
\t \t \t <button class="enterSubmit btnInactive" name="btn_delete" id="warehouseOk" tabindex="">Yes</button> 
 
    \t \t 
 
    \t \t <a href="javascript:;" id="cancelDialog" class="formblue_link closeClass warehouseCancel" tabindex="">No</a> 
 
\t \t </div> 
 
    </div> 
 
</div> 
 
</div>

+0

Есть несколько кнопок «да/нет», которые присутствуют в предоставленном HTML, который вы хотите найти и нажмите? –

ответ

1

Я думаю, что вы вставляете одно и то же диалоговое окно HTML два раза. Предполагая, что есть только одно диалоговое окно.

Вы должны попробовать использовать WebDriverWait подождать, пока диалоговое окно видимым и позволяет щелкнуть нужный элемент, как показано ниже: -

WebDriverWait wait = new WebDriverWait(driver, 10); 
  • Если вы хотите нажать на Yes кнопку попробовать, как: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("warehouseOk"))).click(); 
    
  • Если вы хотите нажать на кнопку No, попробуйте ввести: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("cancelDialog"))).click(); 
    
  • Если вы хотите нажать на Close кнопку попробовать, как: -

    wait.until(ExpectedConditions.elementToBeClickable(By.id("close"))).click(); 
    

Примечание: - Там нет необходимости использовать xpath локатор, искомый элемент может быть найти легко, используя By.id() локатор.

1

Вы должны быть в состоянии соответствовать по ID:

//button[@id="warehouseOk"] 

This article приводит некоторые полезные примеры XPath запросов HTML.