C# ';ElementNotVisibleException:元素不可交互';即使在Google主页上等待了元素,但定位Google搜索按钮时出错
所以我有一个Selenium测试,它在按钮与它交互之前等待加载 正如在我的代码中看到的,我已经实现了它,这样驱动程序将等待14秒(14只是一个随机数),或者如果元素在14秒之前定位,它将继续移动 然而,即使在我等待加载元素并尝试与之交互(使用Click()方法)之后,我仍然会收到这个错误,表明该元素不“可交互” 有趣的是,这在某些时候确实有效——元素确实是可交互的——但在其他时候不行C# ';ElementNotVisibleException:元素不可交互';即使在Google主页上等待了元素,但定位Google搜索按钮时出错,c#,selenium,selenium-webdriver,webdriver,webdriverwait,C#,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,所以我有一个Selenium测试,它在按钮与它交互之前等待加载 正如在我的代码中看到的,我已经实现了它,这样驱动程序将等待14秒(14只是一个随机数),或者如果元素在14秒之前定位,它将继续移动 然而,即使在我等待加载元素并尝试与之交互(使用Click()方法)之后,我仍然会收到这个错误,表明该元素不“可交互” 有趣的是,这在某些时候确实有效——元素确实是可交互的——但在其他时候不行 public void TestChromeDriverMinimalWaitTime() {
public void TestChromeDriverMinimalWaitTime()
{
driver.Navigate().GoToUrl("http://www.google.com");
//find search bar and enter text
driver.FindElement(By.Name("q")).SendKeys("Selenium");
//wait 14 seconds max..
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(14));
//...unless button element is found
IWebElement waitUntil = wait.Until(x => x.FindElement(By.Name("btnK")));
//once found, click the button
waitUntil.Click();
//wait 4 secs till this test method ends
Thread.Sleep(2000);
}
这是我得到的错误:
第53行是这样写的:waitill.Click()强>
根据@DebanjanB的回答修订了工作代码:
public void TestChromeDriverMinimalWaitTime()
{
driver.Navigate().GoToUrl("http://www.google.com");
//find search bar and enter text
driver.FindElement(By.Name("q")).SendKeys("Selenium");
//wait 14 seconds max..unless button element is found
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(14)).Until(ExpectedConditions.ElementToBeClickable(By.Name("btnK")));
//click enter
element.SendKeys(Keys.Return);
Thread.Sleep(2000);
}
有时它会起作用,这似乎是一个时间问题。可能该元素一开始是禁用的,在某个微小的延迟或事件后被启用。尝试在之前添加延迟。单击。您还可以检查按钮元素的状态,查看它是否已禁用。您可以尝试使用以下代码检查页面中某个元素的可见性
public void TestChromeDriverMinimalWaitTime()
{
driver.Navigate().GoToUrl("http://www.google.com");
//find search bar and enter text
driver.FindElement(By.Name("q")).SendKeys("Selenium");
//...unless button element is found
while(!IsElementVisible(driver.FindElement(By.Name("btnK"))){
Thread.Sleep(1000);
}
//once found, click the button
waitUntil.Click();
//wait 4 secs till this test method ends
Thread.Sleep(2000);
}
public bool IsElementVisible(IWebElement element)
{
return element.Displayed && element.Enabled;
}
从您的代码试用中,您似乎试图调用按钮上的
click()
,按钮上的文本为Google Search
你的方法非常完美。但是,如果您分析,您将发现您已调整的元素在中标识了多(两)个元素。因此定位器不能唯一地标识所需的元素。执行时,定位器标识另一个不可见的元素。因此,您将错误视为:
ElementNotVisibleException: element not interactable
解决方案 这里最简单的方法是,将搜索框标识为:
driver.FindElement(By.Name("q")).SendKeys("Selenium");
在表单中,发送搜索文本后,可以使用以下任一解决方案:
- 发送
键。返回
如下:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))); element.SendKeys("Selenium"); element.SendKeys(Keys.Return);
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))); element.SendKeys("Selenium"); element.SendKeys(Keys.Enter);
- 发送
键。按如下方式输入
:
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))); element.SendKeys("Selenium"); element.SendKeys(Keys.Return);
IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))); element.SendKeys("Selenium"); element.SendKeys(Keys.Enter);