【问题标题】:How get value from this code by using Selenium?如何使用 Selenium 从这段代码中获取价值?
【发布时间】:2016-07-21 06:12:28
【问题描述】:

我希望这个值 201607000044 来自这个代码 -

<SPAN id=ctl00_ctl00_ctl00_ctl00_ContentPlaceHolderCenter_ContentPlaceHolderBody_ContentPlaceHolderBody_ContentPlaceHolderBody_txtNumber class=inputBold style="WIDTH: 200px; DISPLAY: inline-block">201607000044</SPAN>

因为这不起作用 -

IWebElement notificationNumberElement = FindElementById("ctl00_ctl00_ctl00_ctl00_ContentPlaceHolderCenter_ContentPlaceHolderBody_ContentPlaceHolderBody_ContentPlaceHolderBody_txtNumber");
        string notificationNumber = notificationNumberElement.GetAttribute("value");

【问题讨论】:

  • 什么是FindElementById? “不工作”是什么意思?
  • FindElementById 是我的功能。等于 driver.FindElement(By.Id("ctl00_ctl00_ctl00_ctl00_ContentPlaceHolderCenter_ContentPlaceHolderBody_ContentPlaceHolderBody_ContentPlaceHolderBody_txtNumber"));不工作意味着我没有将此值转换为字符串。
  • 你有什么错误吗?
  • 字符串为空异常

标签: c# asp.net selenium testing


【解决方案1】:

值是文本。您可以通过Text 会员获得它

string notificationNumber = notificationNumberElement.Text;

【讨论】:

    【解决方案2】:

    使用.Text 如下:-

    IWebElement notificationNumberElement = driver.FindElement(By.Id("ctl00_ctl00_ctl00_ctl00_ContentPlaceHolderCenter_Conte‌​ntPlaceHolderBody_ContentPlaceHolderBody_ContentPlaceHolderBody_txtNumber"));
    string notificationNumber = notificationNumberElement.Text;
    

    如果您的id 是动态生成的,请尝试使用CssSelector,如下所示:-

    IWebElement notificationNumberElement = driver.FindElement(By.CssSelector("span.inputBold"));
    string notificationNumber = notificationNumberElement.Text;
    

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2014-09-22
      • 2022-08-18
      相关资源
      最近更新 更多