当元素不在可见屏幕中时无法处理该元素

Unable to handle the element when it is not in the Visible Screen

当元素不在可见屏幕中时,Selenium 会抛出“找不到元素”异常。仅当我使用 Autoit 或手动向下滚动页面时,它才有效。

尽管目前我正在使用 AutoIt 向下滚动页面并定位元素,但我不这么认为,它适用于所有类型的分辨率屏幕。

硒版本:2.41.0 Chrome版本:47.0.2526.80

注意:我的应用程序仅适用于 Chrome 浏览器

请建议。

看,由于 Selenium Webdriver 试图模拟真实用户,它无法与 invisible/hidden 的元素交互。首先,您可以使用 Actions 移动到元素,然后简单地检查元素的可见性,如下所示:

WebElement element = driver.findElement(By by);
Actions action = new Action(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element)).WhateverActionYouWantToDoOnWebElement;

然后你可以执行任何你想执行的操作。

试试这个:

WebElement element = driver.findElement(By.id("id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

您可以尝试将您的元素放在页面的开头,例如第一个可见的 div,使用 jQuery,如图 hereprepend 方法

$( ".div_class" ).prepend( $( "h2" ) ); 
//put the h2 element at the beginning of the div with class "div_class"

你可以了解更多here