0

I wonder why VBA can't find element by id. I want to input my ID and PW in homepage text box, but VBA can't find element. (the error message is, "Runtime error: No SuchElementError")

My code is:

Dim Sel As New Selenium.WebDriver
Dim i As Object

Sel.Start "chrome"
Sel.Get "https://world.taobao.com/markets/all/login"
Sel.Wait 2000

Sel.FindElementById("fm-login-id").SendKeys "test my id"

Sel.Quit
Set Sel = Nothing

enter image description here

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

2 Answers2

0

The element you're looking for is within an iframe. Take a look at this answer: https://stackoverflow.com/a/50882564/1387701 which talks about using SwitchToFrame:

Sub HandleIframe()
    With New ChromeDriver
        .get "http://hcad.org/quick-search/"
        .SwitchToFrame .FindElementByTag("iframe", timeout:=10000)
        .FindElementById("acct").SendKeys "8227"
        .FindElementByCss("input[value='Search']").Click
    End With
End Sub
DMart
  • 2,401
  • 1
  • 14
  • 19
0

To send a character sequence to the Username field as the the desired element is within an <iframe> so youhave to:

  • First SwitchToFrame.

  • Then locate the element.

  • You can use either of the following Locator Strategies:

  • Using FindElementById:

    Dim Sel As New Selenium.WebDriver
    
    Sel.Start "chrome"
    Sel.Get "https://world.taobao.com/markets/all/login"
    Sel.Wait 2000
    Sel.SwitchToFrame "login-iframe"
    Sel.Wait 2000
    Sel.FindElementById("fm-login-id").SendKeys "test my id"
    
  • Using FindElementByCss:

    Dim Sel As New Selenium.WebDriver
    
    Sel.Start "chrome"
    Sel.Get "https://world.taobao.com/markets/all/login"
    Sel.Wait 2000
    Sel.SwitchToFrame "login-iframe"
    Sel.Wait 2000
    Sel.FindElementByCss("input#fm-login-id").SendKeys "test my id"
    
  • Using FindElementByXPath:

    Dim Sel As New Selenium.WebDriver
    
    Sel.Start "chrome"
    Sel.Get "https://world.taobao.com/markets/all/login"
    Sel.Wait 2000            
    bot.SwitchToFrame "login-iframe"
    Sel.Wait 2000
    Sel.FindElementByXPath("//input[@id='fm-login-id']").SendKeys "test my id"
    

References

You can find a couple of relevant detailed discussions in:


tl; dr

Ways to deal with #document under iframe

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