代码之家  ›  专栏  ›  技术社区  ›  djangofan

使用c watin,如何获取输入标记html元素的值?

  •  3
  • djangofan  · 技术社区  · 15 年前

    使用c_watin,如何获取页面上第二次出现的输入标记HTML元素的值?

    我尝试了很多事情,但都没有成功。调试器没有提供任何明显的方法来获取返回的4个元素中的元素[1]。

    Console.WriteLine(ie.Element(Find.ByClass("myclass")).GetAttributeValue("value")  ) ;  
    // works but prints the value of the 1st of 4 input elements on the page
    
    Console.WriteLine(ie.ElementsWithTag("input", "text").Length.ToString()  ); 
    // returns the number 4
    
    Console.WriteLine( ie.Element(Find.ByName("login")).GetAttributeValue("value")  );
    // works but its not safe since its possible there might be two elements with the name login
    

    基本上,我希望能够通过 数组索引 .

    4 回复  |  直到 15 年前
        1
  •  3
  •   alonp    15 年前

    这可以帮助:

            ElementCollection elementCol = ie.Elements;
            elementCol = elementCol.Filter(Find.ByClass("myclass"));
            string value = elemntCol[1].GetAttributeValue("value");
    
        2
  •  0
  •   prostynick    15 年前

    尝试

    ie.Element(Find.ByIndex(1))
    
        3
  •  0
  •   sth    14 年前

    我相信我可能偶然遇到了一个更好的答案 index constraint class ,这是:

    ie.Link(new IndexConstraint(1) && Find.ByName("linkname"))
    

    我需要研究一下。我稍后再讨论这个问题。

        4
  •  0
  •   sth    14 年前

    没有人再使用循环了吗?我认为循环是整洁的(而且更容易理解为什么watin表现出这种行为/偷了我的午餐;-):

    TextFieldCollection textFields = browser.TextFields;  
    foreach (var field in textFields)  
    {  
        if (field.Id == "humbuggery")...  
    }