0

I am trying to Debug.Print a value in the immediate window of an input text field so when found that this line can be executed in the console

document.querySelector("#txtCaptcha").value

I thought it may be useful for me if I could get that value into the immediate window I have tried such line but returned nothing in the immediate window (no error occurred but nothing in the immediate window)

Debug.Print .ExecuteScript("document.querySelector('#txtCaptcha').value;")

Simply I am searching for a way to be able to get the value from javascript command. I have no idea about javascript so I am stuck

2
  • Do you have any text field defined in your page with id txtCaptcha ? Commented Jul 18, 2019 at 5:43
  • Yes there is a field with this id txtCaptcha Commented Jul 18, 2019 at 6:33

2 Answers 2

2

You are mixing HTMLDocument and Selenium examples in the above.

You cannot return text in either scenario from ExecuteScript (or execScript for IE) direct. Use your script to write the value to an existing node (or create a new one) and then read from that via DOM parser.

Dim s As String
s = "captcha = document.querySelector('#txtCaptcha').value;" & _
    "document.title = captcha;"

.ExecuteScript s
.FindElementByTag("Title").Text

But if FindElementById("txtCaptcha").Attribute("value") doesn't return the value I would be surprised if using javascript will. Though testing with javascript in browser using url from a prior question of yours does return the value.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you very much for great efforts. I encountered error at the last line so I changed to Debug.Print .FindElementByTag("Title").Text > but I got empty in the immediate window.
I am not surprised. But the process in general above is correct for how to return from ExecuteScript.
Side note: Have you checked btw your element is not inside an iframe? Because from your prior url it is.
Yes in the code I recognize it is in frame so I used .SwitchToFrame "searchCaseDiv"
Use the iframe url direct. It works with IE pastebin.com/DkaT1LJr
|
1

Just add return in front of your JS script.

Debug.Print .ExecuteScript("return document.querySelector('#txtCaptcha').value;")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.