Search code examples
vbaseleniumxpathcss-selectorspopupwindow

Click OK in popup using Selenium VBA


Trying to automate an on-line order process. Once the order specification is completed you press the Save button. If the item specification (height) is >1000mm a popup is displayed. I am having problems automating Clicking it. It is not in an iframe.

On the second line of code below: bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click

'Clicking 'causes' the popup warning to display if the >1000mm condition is TRUE. It has only one button - 'OK' (coded as nbsp;OKnbsp; ). Clicking manually allows automatic progress to go to the next page but I need help to get the coding to do this automatically. At the moment the code appears to be hung displaying the popup. Debug indicates that the next instruction cannot be executed - obviously because that findElementById....Click cannot be found because the process has got stuck.

I have tried several Alternatives - please see the code for the most likely that I tried.

If ThisWorkbook.Sheets("SquareSpace").Cells(r, "F").Value > 1000 Then ' The item is more than 1000mm high

bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click 
                            
'Alternative 1
bot.FindElementByXPath("//input[@ID='popup_ok' and text()=' OK ']").Click

'Alternative 2
bot.SwitchToAlert.Accept
bot.Wait 1000

Alternative 3
bot.FindElementById("popup_ok").Click

Else ' for when the item is not more than 1000mm high

       bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click
       bot.Wait 500

End If

'Then the next instruction of the form bot.FindElementById ......Click

The HTML with some stuff removed for clarity (text and styling):

<div id="popup_container" class="ui-draggable" style="position: absolute; z-index: 99999; padding: 0px; margin: 0px; min-width: 604px; max-width: 604px; top: 0px; left: 647.5px;"><h1>Warning</h1>
    
    <div id="popup_content" class="alert">

        <div id="popup_message"><br>WARNING TEXT</div>

        <div id="popup_panel"><input type="button" value="&nbsp;OK&nbsp;" id="popup_ok"></div>

    </div>
</div>

Any suggestions are appreciated. Thanks.


Solution

  • To click on the element with value as OK you can use either of the following Locator Strategies:

    • Using FindElementByCss():

      bot.Wait 5000
      bot.FindElementByCss("div#popup_container div#popup_panel > input#popup_ok").Click
      
    • Using FindElementByXPath():

      bot.Wait 5000
      bot.FindElementByXPath("//div[@id='popup_container']//div[@id='popup_panel']/input[@id='popup_ok']").Click