0

In selenium excel vba I am trying to learn more about how to deal with the CSS selectors And I am wondering because when inspecting an element with ID and when run the code I got a message that the element not found Here's the code till now

Private bot As New selenium.ChromeDriver

Sub Test()
Dim win, mainWin As selenium.Window, sCode As String, i As Long
Dim urlImage As String, urlPost As String

Dim sCase As String
sCase = "192160470"

Set bot = New ChromeDriver

With bot
    .Start "Chrome"

    'First Window (Main Window)
    .Get "https://www.kuwaitcourts.gov.kw/searchPages/searchCases.jsp"
    '.FindElementById("txtCaseNo").SendKeys sCase
    .FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase

    'MsgBox "Click OK After Entering Captcha", 64


    Stop
    .Quit
End With

End Sub

and here's the HTML part for that element

<td><input type="text" name="txtCaseNo" id="txtCaseNo" maxlength="9" class="inputTextBox" onkeypress="return onlyNumbers(event);"></td>

I am stuck at this line

.FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase

Thanks advanced for any help or any ideas

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

2 Answers2

1

The element is inside of an iframe with id searchCaseDiv. You have to switch to that iframe to be able to access the element.

Use .SwitchToFrame to switch frame.

For java, it would be like this,

driver.switchTo().frame("searchCaseDiv");
S Ahmed
  • 1,454
  • 1
  • 8
  • 14
  • Thanks a lot but in fact I am interested in VBA. But of course I knew the idea and apply it in VBA `.SwitchToFrame "searchCaseDiv"` – YasserKhalil Jul 17 '19 at 12:08
1

To send a character sequence to the Username field as the the desired element are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    With bot
        .Start "Chrome"
        .Get "https://www.kuwaitcourts.gov.kw/searchPages/searchCases.jsp"
        .SwitchToFrame "searchCaseDiv"
        .FindElementByCss("input[type=text][name='txtCaseNo']").SendKeys sCase
    

You can find a relevant discussion in How to send text with in the username field within an iframe using Selenium VBA


tl; dr

Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352