【问题标题】:What is the C# equivalent of document.getElementByClassName("mat-form-field-infix")[0]document.getElementByClassName("mat-form-field-infix")[0] 的 C# 等价物是什么
【发布时间】:2021-01-13 20:51:01
【问题描述】:

大家好,这里是事情,我需要确认一个元素是否在屏幕上可见,我当前使用的代码是:

WaitVisible(By.ClassName("mat-form-field-infix"));

但该元素出现了 3 次,我需要确保我指向正确的元素,我发现使用:

document.getElementByClassName("mat-form-field-infix") 

添加 [0] 将我引向特定元素,但我不知道如何将其转换为 Selenium,有什么想法吗?

【问题讨论】:

  • I need to be sure that I'm pointing at the right one: 什么是“正确的”?如果你有mat-form-field-infix 类的三个元素,你想要哪一个?
  • 我想一次指向一个特定的(任何),而不仅仅是让 selenium 自动指向第一个。

标签: javascript c# jquery selenium selenium-webdriver


【解决方案1】:

您可以使用类似下面的内容,但没有发布您的 html,我有点猜测。该类是来自 div、input、span 的吗?这允许您使用 xpath 在呼叫中设置自定义等待时间。通话时间设置为 30 秒。

呼叫:

WaitForElementDisplayed_byXPathCustomTime("//div[@class='mat-form-field-infix'][0]", 30);

方法:

   public static void WaitForElementDisplayed_byXPathCustomTime(string value, Int32 time)
    {
        try
        {
            var wait = new WebDriverWait(Driver, new TimeSpan(0, 0, time));
            wait.Until(webDriver => Driver.FindElement(By.XPath(value)).Displayed);
        }
        catch (Exception) { throw; }
    }

【讨论】:

    【解决方案2】:

    大概是getElementsByClassName(),而不是getElementByClassName()

    要使用 JavaScript

    var x = document.getElementsByClassName("mat-form-field-infix")[0]
    

    作为 命令,您可以使用:

    var element = ((IJavaScriptExecutor)driver).ExecuteScript("return document.getElementsByClassName('mat-form-field-infix')[0]");
    

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2010-12-07
      • 2010-12-01
      • 1970-01-01
      相关资源
      最近更新 更多