如果包含" - "如何获取HTMLElement ID

时间:2017-01-27 15:48:22

标签: c# html variables naming-conventions

我正在尝试加载Google并获取搜索框的ID。框的ID是" lst-ib"。当程序进行调试时,它需要一个分号。

有没有办法获得元素ID?到目前为止,我有:

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        public void Main(string[] args)
        {

            Process.Start("www.google.com");

            HtmlElement lst-ib = WebBrowser1.Document.All["foo"];
            //expects a semi colon on the line above after the element id

            if (lst-ib != null)
            {
                lst-ib.InnerText = "test";
            }

            Console.ReadKey();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

即C#代码,-在标识符中无效。您可以根据需要随意命名变量 - 它与元素的ID无关。

答案 1 :(得分:1)

- 运算符 ,您不能这样使用!

在这里您可以找到有关运营商的更多信息: https://msdn.microsoft.com/en-us/library/6a71f45d.aspx

我建议你重命名 - (追踪)到_(下划线)或任何你想要的

= d