首页 > 解决方案 > OpenQA.Selenium.NoSuchElementException 当元素存在于开发者工具上时

问题描述

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

我正在尝试做:

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

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

标签: c#seleniumgeckodriver

解决方案


所需的元素是动态元素,因此您必须为所需的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);
    

使用 Chrome 时,您可以在 Selenium “selenium.common.exceptions.NoSuchElementException”中找到详细讨论


推荐阅读