【问题标题】:OpenQA.Selenium.NoSuchElementException when the Element exists on Developer ToolsOpenQA.Selenium.NoSuchElementException 当元素存在于开发者工具上时
【发布时间】:2019-09-05 15:17:27
【问题描述】:

我正在尝试使用 Selenium 填写表单,但当该元素明显存在于 Firefox 上的开发人员工具中时,我收到了 Unable to locate element: #PaymentMethod_FullName。我已经测试了 iframe,并且没有 iframe。

我正在努力:

driver.FindElement(By.Id("PaymentMethod_FullName")).SendKeys(name);

但我已经尝试过使用 css 选择器 #PaymentMethod_FullName 和 XPath //*[@id='PaymentMethod_FullName'],所以我不知道为什么它不起作用。此外,该表单上的所有输入都存在此错误。提前致谢。

【问题讨论】:

  • 再次检查 IFRAME。您是否尝试过添加等待?
  • 在控制台窗口中输入:$('#PaymentMethod_FullName') 并按回车键。它找到你的项目了吗?
  • @Aman B 不,它不会返回

标签: c# selenium geckodriver


【解决方案1】:

所需的元素是动态元素,因此您必须为所需的 ElementToBeClickable 诱导 WebDriverWait,您可以使用以下任一Locator Strategies

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.tooltip#PaymentMethod_FullName"))).SendKeys(name);
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='tooltip' and @id='PaymentMethod_FullName']"))).SendKeys(name);
    

您可以在Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome找到详细讨论

【讨论】:

    【解决方案2】:

    诱导WebDriverWait并等待ElementToBeClickable。希望这会奏效

     WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); 
     wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("PaymentMethod_FullName"))).SendKeys(name);
    

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); 
    wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Xpath("//fieldset[@class='payment-methods required-form']//ol[@class='form credit label-positioned']//li//input[@id='PaymentMethod_FullName']"))).SendKeys(name);
    

    【讨论】:

      【解决方案3】:

      编辑:这是一个 iframe,我只是愚蠢

      只需执行driver.SwitchTo().Frame(driver.FindElement(By.Id("ID OF THE IFRAME HERE")));,然后就可以了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-12
        • 1970-01-01
        • 2015-01-22
        • 1970-01-01
        • 2014-01-26
        • 2022-01-12
        • 2013-12-26
        • 2018-07-06
        相关资源
        最近更新 更多