Element doesn't exist although it has ID attribute

不打扰是莪最后的温柔 提交于 2020-03-05 06:12:41

问题


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


回答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




回答2:


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");


来源:https://stackoverflow.com/questions/57075048/element-doesnt-exist-although-it-has-id-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!