【问题标题】:Window Form Application in C#C# 中的窗口窗体应用程序
【发布时间】:2015-05-08 05:47:57
【问题描述】:

我是编程新手。我在 VisualStudio C# 中使用窗口窗体。 我的问题是单击窗口窗体中的第一个按钮后,它会打开浏览器并转到我要登录的 URL,然后当我单击窗口窗体上的第二个按钮时,它不会运行第二个代码块.我没有收到任何错误消息。 任何人都可以帮助我,因为我完全是一个初学者。提前非常感谢您!

public partial class Form1 : Form
{
    IWebDriver driver = null;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        driver = new FirefoxDriver();
        driver.Url = "https://accounts.google.com/ServiceLogin";    
        driver.Manage().Window.Maximize();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        driver.Url = "https://accounts.google.com/ServiceLogin";

        var email = driver.FindElement(By.Id("Email"));
        email.SendKeys("-------------");
        var password = driver.FindElement(By.Id("Passwd"));
        password.SendKeys("---------");
        password.FindElement(By.Id("signIn"));

【问题讨论】:

  • 请根据您的具体问题更改标题。 meta.stackexchange.com/questions/10647/…
  • **private void button2_Click 上的两个星号 (*) 是您的代码还是您在此问题中添加了它们?
  • 我在这里暗示,代码编译得很好,因为作者测试了他的应用程序,所以**是为了说明热点。
  • 作为旁注,您应该在您的网址前加上@。如果您想了解更多信息,请搜索verbatim string literal
  • 感谢您的帮助。为大家点赞!

标签: c# selenium-firefoxdriver


【解决方案1】:

button2.Click += button2_Click; 添加到您的表单构造函数中,紧跟在InitializeComponent(); 之后。此行将事件处理程序button2_Click 添加到button2 的事件Button.Click

通常,这种东西会由 设计师 为您完成。如果您喜欢这种方式,请转到表单的预览页面,然后转到属性管理器,单击闪电“事件”并双击您想要的事件,在本例中为 Click。完成此操作后,将生成 button2 单击事件处理程序的方法体。

【讨论】:

  • 非常感谢您的帮助和信息。它现在工作正常。我还是编程新手,每一条信息都非常有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多