Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';ElementNotVisibleException:元素不可交互';即使在Google主页上等待了元素,但定位Google搜索按钮时出错_C#_Selenium_Selenium Webdriver_Webdriver_Webdriverwait - Fatal编程技术网

C# ';ElementNotVisibleException:元素不可交互';即使在Google主页上等待了元素,但定位Google搜索按钮时出错

C# ';ElementNotVisibleException:元素不可交互';即使在Google主页上等待了元素,但定位Google搜索按钮时出错,c#,selenium,selenium-webdriver,webdriver,webdriverwait,C#,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,所以我有一个Selenium测试,它在按钮与它交互之前等待加载 正如在我的代码中看到的,我已经实现了它,这样驱动程序将等待14秒(14只是一个随机数),或者如果元素在14秒之前定位,它将继续移动 然而,即使在我等待加载元素并尝试与之交互(使用Click()方法)之后,我仍然会收到这个错误,表明该元素不“可交互” 有趣的是,这在某些时候确实有效——元素确实是可交互的——但在其他时候不行 public void TestChromeDriverMinimalWaitTime() {

所以我有一个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行是这样写的: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);
    

谢谢@Thoryn,我确实想到了这一点,但我想知道在测试中增加大量睡眠延迟是否有益。这是一种良好的做法,还是我们必须等待元素加载?检查状态通常比静态延迟好。对于您的解决方案,第一行不会编译,因为Click()方法返回void,并且不允许我们转换为IWebElement类型。这是我的错误,请更正。让我知道你的状态。谢谢你,效果很好(至少现在没有更多有趣的事情了!)。实际上,我以前曾尝试过使用“ExpectedConditions”方法,但没有找到。我刚刚发现我必须添加一个Nuget包'DotNetSeleniumExtras.WaitHelpers'才能使该方法工作。关于您的解决方案,您是说我以前的代码不是一直唯一标识特定元素的吗?非常感谢-现在都排序好了