因此,我有一个Selenium测试,它等待一个按钮在与其交互之前加载。
正如在我的代码中所看到的,我已经实现了它,这样驱动程序将等待14秒(14只是一个随机数),或者如果元素位于14秒之前,它就会移动。
但是,即使在等待元素加载并尝试与其交互之后(使用Click()方法),我仍然会得到这个错误,显示元素不是“可交互的”。
有趣的是,这实际上在某些时候起作用--其中的元素确实是可交互的--但其他时候就不行了。
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行是这样的一行:waitUntil.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);
}发布于 2019-04-16 19:12:59
从您的代码试用来看,您似乎试图在按钮上调用click()作为、Google、谷歌主页上的文本。
你诱导WebDriverWait的方法太完美了。但是,如果分析HTML DOM,就会发现您所适应的定位器策略标识了DOM树中的多个(两个)元素。因此,定位器不能唯一地标识所需的元素。在执行时,定位器标识另一个元素,即,不可见的。因此,您将错误视为:
ElementNotVisibleException: element not interactable解决方案
这里最简单的方法是,作为搜索框,您已经识别为:
driver.FindElement(By.Name("q")).SendKeys("Selenium");在表单中,一旦发送搜索文本,就可以使用下列解决方案之一:
Keys.Return如下:
TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));IWebElement元素=新WebDriverWait(驱动程序,WebDriverWait element.SendKeys("Selenium");element.SendKeys(Keys.Return);Keys.Enter如下:
TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));IWebElement元素=新WebDriverWait(驱动程序,WebDriverWait element.SendKeys("Selenium");element.SendKeys(Keys.Enter);发布于 2019-04-16 17:21:54
由于它有时起作用,这似乎是一个时间问题。也许元素一开始是禁用的,并且是在一些微小的延迟或事件之后启用的。尝试在.Click之前添加一个延迟。您还可以检查按钮元素的状态,以确定它是否已禁用。
发布于 2019-04-16 18:12:59
您可以使用下面的代码尝试检查页面中元素的可见性。
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;
}https://stackoverflow.com/questions/55713414
复制相似问题