如何通过文本框设置GetElementById值 [英] How to set GetElementById value via a textbox

查看:153
本文介绍了如何通过文本框设置GetElementById值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清楚如何通过我的Windows窗体上的两个文本框自动填充位于我窗体中嵌入的网站上的两个文本框。其中一个文本框的ID是电子邮件。另一个文本框的ID是密码。我知道我可以使用下面的代码在我的Windows窗体中的嵌入式浏览器中找到文本框的元素ID。我将首先发布定义。



I'm trying to figure out how to auto fill two text box's located on a website embedded in my windows form via two text box's also on my windows form. The Id of one of the text box's is Email. The other textbox's Id is Password. I know I can find the element Id of the text box's on the embed browser in my windows form by using this code below. I will post the definition first.

public void ExecuteScriptAsync(string script);





这是用于获取Id的代码。





This is the code used to get the Id.

browser.ExecuteScriptAsync("document.getElementById('Email')")




browser.ExecuteScriptAsync("document.getElementById('Password')")





我可以使用此代码设置所述文本框的值/文本。下面的代码很有用,并且会设置两个文本框的值/文本。





Which in turn I can set the value/text of said text box's by using this code. The below code works great and will set the value/text of both text box's.

browser.ExecuteScriptAsync("document.getElementById('Email').value= 'USER EMAIL STRING'");




browser.ExecuteScriptAsync("document.getElementById('Password').value= 'USER PASSWORD STRING'");





我的Windows窗体上有两个文本框,名为txtEmail和txtPassword,我想使用这样用户就可以输入自己的登录信息。我尝试通过在下面的代码中传递txtEmail.Text和txtPassword.Text来实现此目的。





I have two text boxes on my windows form named txtEmail and txtPassword that I would like to use so the user can put his or her own login information. I tried to achieve this among other ways by passing the txtEmail.Text and txtPassword.Text in the code below which doesn't work.

string Email = @"""document.getElementById('Email').value= " + @"'" + txtEmail.Text + @"'""";







string Password = @"""document.getElementById('Password').value= " + @"'" + txtPassword.Text + @"'""";







browser.ExecuteScriptAsync(Email);
browser.ExecuteScriptAsync(Password);









我在开发人员IDE中没有任何错误。它不会在嵌入式浏览器中设置文本框的文本。有人可以解释如何使用我的窗体上的文本框传递所需的字符串吗?



编辑:更详细地重新编写问题。希望它更能帮助解释我的问题。





I don't get any errors inside the developer IDE. It just won't set the text box's text in the embedded browser. Can someone explain how to use a text box on my windows form to pass along the needed strings?

Reworded the question in better detail. Hope it better helps explain my question.

推荐答案

我自己想出来了。然而,这里是将文本框的数据提供给字符串所需的代码。因此,如果表单有两个文本框分别带有ID的电子邮件和密码,您可以将它与CefSharp中的ExecuteScriptAsync脚本结合使用。







I figured it out by myself. None the less here is the code needed to supply a text box's data into the string. So you can use it in conjunction with the script for ExecuteScriptAsync in CefSharp if the form has two text box's respectively with the ID's Email and Password.



browser.ExecuteScriptAsync("document.getElementById('Email').value=" + '\'' + txtEmail.Text + '\'');
                    browser.ExecuteScriptAsync("document.getElementById('Passwd').value=" + '\'' + txtPassword.Text + '\'');


private void browserView1_FinishLoadingFrameEvent (object sender, FinishLoadingEventArgs e)

{

if (e.IsMainFrame)

  {

   DOMDocument document = e.Browser.GetDocument();
   DOMInputElement userName = (DOMInputElement)document.GetElementByName("j_username");
   DOMInputElement pass = (DOMInputElement)document.GetElementByName("j_password");                    
   userName.Value = "HackStar";
   pass.Value = "HackStar";  

  }
}


这篇关于如何通过文本框设置GetElementById值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆