使用Selenium访问不可见的文本输入元素

本文关键字:文本 输入 元素 Selenium 访问 使用 | 更新日期: 2023-09-27 18:37:08

我正在尝试访问输入元素,但是当我尝试单击或发送密钥时,VS 会给我以下异常:

An unhandled exception of type 'OpenQA.Selenium.ElementNotVisibleException' occurred in WebDriver.dll
Additional information: element not visible

谷歌搜索后,我发现这可能是一个不可见的输入元素,我需要使用 JavaScript 驱动程序来完成。我不太擅长 C#,而且我以前从未使用过 Selenium,因此我无法自己实现这部分,所以我希望你能帮助我。

这是我在检查元素时在表单源代码中看到的内容:

<span class="origin-ux-textbox-control origin-ux-control">
<span>
<input id="ebi_email-input" maxlength="256" type="text" name="" value="peterparker@web.de" autocorrect="off" autocapitalize="off" autocomplete="off"></span>
</span>

这是我尝试过的:

driver.FindElement(By.Id("ebi_email-input")).Click();
driver.FindElement(By.Id("ebi_email-input")).Clear();
driver.FindElement(By.Id("ebi_email-input")).SendKeys(email);

异常在第一行引发。

使用Selenium访问不可见的文本输入元素

我怀疑正在使用一些重复的ids。 尝试查找FindElements方法返回了多少个元素,如果有多个,那么我想你可以循环并找到可见的元素,然后发送密钥。或者,如果你必须使用javascript,下面也应该可以工作。

IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
IWebElement element = driver.FindElement(By.Id("ebi_email-input"));
js.ExecuteScript("arguments[0].setAttribute('value', 'youremailaddress')", element);