【问题标题】:How to retrieve the element text using GetAttribute method?如何使用 GetAttribute 方法检索元素文本?
【发布时间】:2020-02-25 00:45:54
【问题描述】:

我怎样才能得到这个元素的文本:

<span jsan="7.widget-pane-link,0.role" style="">Hi</span>

也就是说,我怎样才能通过使用属性GetAttribute("jsan")得到文本Hi

【问题讨论】:

  • 它的 span.innertext 或 span.innerhtml
  • 我会尝试使用javascript innerHtml

标签: c# selenium getattribute


【解决方案1】:

要从元素中检索文本 Hi,您必须为 ElementIsVisible() 诱导 WebDriverWait,您可以使用以下任一 Locator Strategies 作为解决方案:

  • 使用 CssSelectorText 属性:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("span[jsan*='widget-pane-link'][jsan$='role']"))).Text);
    
  • 使用XPathGetAttribute()方法:

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//span[contains(@jsan, 'widget-pane-link') and contains(@jsan, 'role')]"))).GetAttribute("innerHTML"));
    

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多