简体   繁体   English

如何将 VB.net 代码中定义的字符串变量的值传递到 CefSharp 浏览器中,并使用 Javascript 按名称调用它们?

[英]How can I pass the values of string variables defined in VB.net code into a CefSharp browser and call them by their name using Javascript?

First time user of CefSharp.第一次使用 CefSharp。 I'm trying to use it with vb.net to automate some tasks in a website.我正在尝试将它与 vb.net 一起使用来自动化网站中的一些任务。

I created a ChromiumWebBrowser and pointed it to a website with 2 input fields Username and Password.我创建了一个 ChromiumWebBrowser 并将其指向一个具有 2 个输入字段用户名和密码的网站。 I managed to use javascript for filling the 2 input fileds with the 2 corresponding strings as follows:我设法使用 javascript 用 2 个相应的字符串填充 2 个输入文件,如下所示:

document.getElementsByTagName('input')[0].value = 'MyUsername';
document.getElementsByTagName('input')[1].value = 'MyPassword';

and IT WORKS.它工作正常。 I'm seeing the 2 fileds filled with the 2 strings.我看到 2 个文件充满了 2 个字符串。 (The fileds have no Id. That's why the getElementsByTagName) (文件没有 ID。这就是 getElementsByTagName 的原因)

Now, I want, instead of the actual strings inside the javascript code, to pass the strings from variables inside my VB.net code.现在,我想要从 VB.net 代码中的变量传递字符串,而不是 javascript 代码中的实际字符串。 Something like:就像是:

VB.net code VB.net代码

Dim USER as String = 'MyUsername'
Dim PASS as String = 'MyPassword'

and the previous JS code to something like:和之前的 JS 代码类似:

document.getElementsByTagName('input')[0].value = USER;
document.getElementsByTagName('input')[1].value = PASS;

but obviously not that!但显然不是这样!

I found some posts about putting in my VB code something like this我发现了一些关于在我的 VB 代码中放入类似这样的帖子

browser.JavascriptObjectRepository.Register("boundAsync", New BoundObject())

and a Public Class BoundObject和一个公共 Class BoundObject

and pass it to JS code like并将其传递给 JS 代码,例如

await CefSharp.BindObjectAsync("boundAsync", "bound");

but I'm confused on how to do it with the 2 strings I mentioned before.但我对如何使用我之前提到的 2 个字符串进行操作感到困惑。 How can I register the USER and PASS variables in VB.net and then call their values in JS to fill the input fields?如何在 VB.net 中注册 USER 和 PASS 变量,然后在 JS 中调用它们的值来填充输入字段?

TIA TIA

Yes I was doing that, just now!是的,我刚才正在这样做!

in VB.net code在 VB.net 代码中

Dim USER as String = 'MyUsername'

and in JS code并在 JS 代码中

document.getElementsByTagName('input')[0].value = '" + USER + "';

the trick is in the quotes.诀窍在于引号。 Instead of writing = USER you put it like this '" + USER + "'而不是写 = USER 你这样写'" + USER + "'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM