1

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.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
JonR
  • 67
  • 6

2 Answers2

1

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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I tried using both Css and XPath approaches and also tried: `bot.FindElementByXPath("//div[@id='popup_container']//div[@id='popup_content']//div[@id='popup_panel']/input[@id='popup_ok']").Click` Not sure if the syntax is right -but it did not work. The error occurs when it tires to execute the next instruction so I conclude that it is not leaving the page. Thanks for any more ideas – JonR Jul 16 '20 at 20:31
  • 1
    Thank you. This solution worked once I had sorted out another issue – JonR Jul 17 '20 at 12:17
0

Try using javascript to click the button:

[edit - updated identifier]

element=bot.FindElementByXPath("//input[@id='popup_ok']")
x =bot.ExcuteScript("arguments[0].click();", element)

[code above updated - see comments] [new below] Had a bit of fun getting this to work as expected.

This is my little test function to give you an idea of what i used to check things:

Function selenium()
    Dim driver As New WebDriver
    Dim element As WebElement
    
    driver.Start "chrome"
    driver.Get "https://www.google.co.uk"
    driver.FindElementByXPath("//input[@name='q']").SendKeys "hello world"
    Set element = driver.FindElementByXPath("//input[@value='Google Search']")
    
    x = driver.ExecuteScript("arguments[0].click();", element)
End Function

Not sure why i had to assign a response back from the execute script but it wouldn't work without it.

RichEdwards
  • 2,205
  • 2
  • 2
  • 15
  • Thanks, Sorry, I could not convert to Selenium VBA syntax – JonR Jul 16 '20 at 20:33
  • No worries buddy. I'll have a look again when I get online :-) – RichEdwards Jul 17 '20 at 06:38
  • Hi Rich, thanks. I replaced this line `bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click ` with these `Set element = bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave")` `X = bot.ExecuteScript("arguments[0].click();", element)`. What sort of variable is 'X'? The routine stalls when it tries to find an element that does not exist - so I guess it did not close the popup – JonR Jul 17 '20 at 10:15
  • I can't tell you what x is - it just wouldn't run for me without assigning a return variable. If it's stalling on finding the object try anther identifier? - try a generic xpath such as //input[@id='popup_ok'] – RichEdwards Jul 17 '20 at 10:31
  • I see now i borrowed your existing findby id which isn't in your html source for the button! Sorry about that - You need to click the input/button for this to work... try that new xpath and let me know if you need steps on how you can verify if xpaths exist in chrome devtools - i'll update the above with the new xpath too – RichEdwards Jul 17 '20 at 10:32