0

我遇到了隐藏元素的问题。该站点已完全加载,因此所有可用的项目,无需加载,切换页面。我尝试了使用 ExpectedConditions 的所有选项,但仍然没有等待元素。使用 Find 功能,我得到了位置,但是 x,y 坐标是:(-125, 156),因此无法单击它(在屏幕上也不可见)非常糟糕的解决方法是 while + Thread.Sleep( 1000); 和一个计数器..while x>0 and >0 我想避免..有什么想法吗? 在此处输入图像描述 代码示例:

    ChromeOptions chromeCapabilities = new ChromeOptions();
    chromeCapabilities.EnableMobileEmulation("iPhone 7");

    IWebDriver webDriver = new ChromeDriver(chromeCapabilities);
    webDriver.Manage().Window.Maximize();
    webDriver.Navigate().GoToUrl("https://m.exmaple.org");

    WebDriverWait driverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30.0));

    IWebElement menu_1;
    IWebElement switch_left;

    switch_left = webDriver.FindElement(By.Id("item_1"));
    switch_left.Click(); // ~3-5 sec while switched left because of animations


    driverWait.Until(ExpectedConditions.ElementToBeClickable(By.Id("item_1"))));
    menu_1 = webDriver.FindElement(By.Id("item_1"));
    menu_1.Click(); System.InvalidOperationException: 'unknown error: Element is not clickable at point (-125, 156)
4

1 回答 1

1

如果我正确理解您的问题,那么您正试图在浏览器框架之外单击一个元素。您需要一种方法来等待元素在单击后移入框架。没有内置方法可以执行此操作,因此您需要自定义等待。

您应该能够使用类似下面的东西。它基本上是等到元素的 X/Y 坐标(技术上是左上角)在浏览器框架内。我认为这对你有用。

public IWebElement WaitForElementToBeOnscreen(By locator)
{
    WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
    wait.Until<IWebElement>(d =>
    {
        IWebElement element = d.FindElement(locator);
        if (element.Location.X > 0 &&
            element.Location.X < Driver.Manage().Window.Size.Width &&
            element.Location.Y > 0 &&
            element.Location.Y < Driver.Manage().Window.Size.Height)
        {
            return element;
        }

        return null;
    });

    return null;
}

注意:为了使这一点更加准确,您可以考虑元素的大小。例如,确保 X 大于 0 且小于窗口宽度 - 元素的宽度……等等。

您可能会遇到的另一个问题是,如果元素永远不会移动……它会停留在浏览器框架之外。如果发生这种情况,等待将超时。我不确定在这种情况下你想做什么......你可以将它包装在 a 中try-catch并返回null或任何你决定的东西。

于 2018-03-01T20:35:27.713 回答