c# - 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']
,所以我不知道为什么它不起作用。此外,该表单上的所有输入都存在此错误。提前致谢。
解决方案
所需的元素是动态元素,因此您必须为所需的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”中找到详细讨论
推荐阅读
- android - 安卓 11 | 地图导航的包可见性更改
- python - sklearn逻辑回归和seaborn逻辑回归有什么区别?
- python - 使用列表和 for 循环按键对字典的键进行排序
- python - 无法获得嵌套数组初始化工作?
- python - 从输入中取一个日期,看看它是从今天开始的天数
- python - 如何获得小于n的自然数互质子集的最大和?
- python - 正则表达式将(所有匹配项或无匹配项)最后修复为一个
- javascript - DirtyForms 不适用于输入隐藏字段
- sql - 如何在从一个表插入另一个表之前修改选择查询的数据
- python-3.x - Python 会自动跳出循环吗?