Selenium 1.x 或 2.x 中是否有任何方法可以滚动浏览器窗口,以便浏览器可以看到由 XPath 标识的特定元素? Selenium 中有一个 focus 方法,但它似乎并没有在 FireFox 中物理滚动视图。有人对如何做到这一点有任何建议吗?
我需要这个的原因是我正在测试页面上元素的点击。不幸的是,除非元素可见,否则该事件似乎不起作用。我无法控制单击元素时触发的代码,因此我无法对其进行调试或修改,因此,最简单的解决方案是将项目滚动到视图中。
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,250)", "”);
Actions action = new Actions(driver); action.moveToElement(element).perform();
WebElement element = driver.findElement(By.<locator>)); element.sendKeys(Keys.DOWN);
在滚动方面尝试了很多事情,但下面的代码提供了更好的结果。
这将滚动直到元素出现在视图中:
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
//do anything you want with the element
您可以使用 org.openqa.selenium.interactions.Actions
类移动到元素。
爪哇:
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
Python:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.sl.find_element_by_id('my-id')).perform()
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
你可能想试试这个。
js.ExecuteScript("arguments[0].scrollIntoView(true);" + "window.scrollBy(0,-100);", e);
用于 IE 和 Firefox,而将 js.ExecuteScript("arguments[0].scrollIntoViewIfNeeded(true);", e);
用于 Chrome。简单的。
((JavascriptExecutor) driver).executeScript("javascript:window.scrollBy(0,250)");
这在 Chrome 中有效。唯一对我有用的解决方案。与第一个评论者的情况相同,我在底部有一个固定元素,它一直遮挡我需要单击的元素。
如果您想使用 Selenium webdriver 在 Firefox 窗口上滚动,其中一种方法是在 Java 代码中使用 JavaScript。向下滚动(到网页底部)的 JavaScript 代码如下:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.clientHeight));");
定位任何元素和 sending down keys(或上/左/右)似乎也有效。我知道这有点小题大做,但我也不太喜欢使用 JavaScript 来解决滚动问题。
例如:
WebElement.sendKeys(Keys.DOWN);
var elem = driver.FindElement(By.Id("element_id")); elem.SendKeys(Keys.PageDown);
在 Selenium 中,我们需要借助 JavaScript 执行器来滚动到元素或滚动页面:
je.executeScript("arguments[0].scrollIntoView(true);", element);
在上面的语句中,element
是我们需要滚动的确切元素。我尝试了上面的代码,它对我有用。
我有一个完整的帖子和视频:
http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/
webElement = driver.findElement(By.xpath("bla-bla-bla"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", webElement);
如需更多示例,请参阅 go here。全部都是俄语,但 Java 代码是跨文化的 :)
您可以使用此代码段滚动:
C#
var element = Driver.FindElement(By.Id("element-id"));
Actions actions = new Actions(Driver);
actions.MoveToElement(element).Perform();
你有它
这对我有用:
IWebElement element = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId))[0];
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);
我发现我的元素的边界矩形不正确,导致浏览器滚动到屏幕外。但是,以下代码对我来说效果很好:
private void scrollToElement(WebElement webElement) throws Exception {
((JavascriptExecutor)webDriver).executeScript("arguments[0].scrollIntoViewIfNeeded()", webElement);
Thread.sleep(500);
}
使用驱动程序发送诸如 pagedown 或 downarrow 键之类的键以将元素显示在视图中。我知道这是一个过于简单的解决方案,可能并不适用于所有情况。
element
并发送 PageDown element.SendKeys(Keys.PageDown);
对我有用。
根据我的经验,当页面上有多个可滚动部分(这很常见)时,Selenium Webdriver 不会在单击时自动滚动到元素。
我正在使用 Ruby,对于我的 AUT,我必须对 click 方法进行猴子修补,如下所示;
class Element
#
# Alias the original click method to use later on
#
alias_method :base_click, :click
# Override the base click method to scroll into view if the element is not visible
# and then retry click
#
def click
begin
base_click
rescue Selenium::WebDriver::Error::ElementNotVisibleError
location_once_scrolled_into_view
base_click
end
end
'location_once_scrolled_into_view' 方法是 WebElement 类的现有方法。
我感谢您可能没有使用 Ruby,但它应该会给您一些想法。
module Capybara :: module Node
中声明了上述内容,我需要调用 native.location_once_scrolled_into_view
而不仅仅是 location_once_scrolled_into_view
。我还拯救了 ::Selenium::WebDriver::Error::UnknownError
,这是我在 Firefox 44.0.2 中经常遇到的。但我发现这个解决方案不够灵活,最终将 <Element>.native.location_once_scrolled_into_view
调用放入验收测试本身,然后在需要时立即调用 page.execute_script('window.scrollByPages(-1)')
。
location_once_scrolled_into_view
的方法 Element#scroll_into_view
。但两者都与 Selenium 的默认行为没有什么不同,因此溢出元素仍然会阻碍我们尝试单击的任何内容。
用于将元素滚动到视图中的 Ruby 脚本如下所示。
$driver.execute_script("arguments[0].scrollIntoView(true);", element)
sleep(3)
element.click
有时我也遇到了使用 Selenium 滚动的问题。所以我使用 javaScriptExecuter 来实现这一点。
向下滚动:
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0, 250)", "");
或者,也
js.executeScript("scroll(0, 250);");
向上滚动:
js.executeScript("window.scrollBy(0,-250)", "");
或者,
js.executeScript("scroll(0, -250);");
这是使用 JavaScript 的重复解决方案,但添加了等待元素。
否则,如果正在对元素执行某些操作,则可能会出现 ElementNotVisibleException
。
this.executeJavaScriptFunction("arguments[0].scrollIntoView(true);", elementToBeViewable);
WebDriverWait wait = new WebDriverWait(getDriver(), 5);
wait.until(ExpectedConditions.visibilityOf(elementToBeViewable));
Selenium 2 尝试滚动到该元素,然后单击它。这是因为 Selenium 2 不会与元素交互,除非它认为它是可见的。
滚动到元素是隐式发生的,因此您只需要找到该项目然后使用它。
WebElement.click()
的规范,需要元素可见才能点击它。它不会代表您滚动。
我用这种方式滚动元素并单击:
List<WebElement> image = state.getDriver().findElements(By.xpath("//*[contains(@src,'image/plus_btn.jpg')]"));
for (WebElement clickimg : image)
{
((JavascriptExecutor) state.getDriver()).executeScript("arguments[0].scrollIntoView(false);", clickimg);
clickimg.click();
}
def scrollToElement(element: WebElement) = {
val location = element.getLocation
driver.asInstanceOf[JavascriptExecutor].executeScript(s"window.scrollTo(${location.getX},${location.getY});")
}
对我有用的是在浏览器窗口底部的元素上使用 Browser.MoveMouseToElement 方法。它奇迹般地在 Internet Explorer、Firefox 和 Chrome 中运行。
我选择它而不是 JavaScript 注入技术只是因为它感觉不那么 hacky。
您可能想要访问页面 Scroll Web elements and Web page- Selenium WebDriver using Javascript:
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FirefoxDriver ff = new FirefoxDriver();
ff.get("http://toolsqa.com");
Thread.sleep(5000);
ff.executeScript("document.getElementById('text-8').scrollIntoView(true);");
}
如果您认为其他答案太 hacky,这个答案也是,但不涉及 JavaScript 注入。
当按钮离开屏幕时,它会中断并滚动到它,所以重试它...¯\_(ツ)_/¯
try
{
element.Click();
}
catch {
element.Click();
}
在大多数情况下滚动此代码都可以使用。
WebElement element = driver.findElement(By.xpath("xpath_Of_Element"));
js.executeScript("arguments[0].click();",element);
JAVA
尝试使用 x y
位置滚动到元素,并将 JavascriptExecutor
与此参数一起使用:"window.scrollBy(x, y)"
。
导入后:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;
首先,您需要获取元素的 x y
位置。
//initialize element
WebElement element = driver.findElement(By.id("..."));
//get position
int x = element.getLocation().getX();
int y = element.getLocation().getY();
//scroll to x y
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(" +x +", " +y +")");
我不确定这个问题是否仍然相关,但在参考 https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView 中的 scrollIntoView 文档之后。
最简单的解决方案是
executor.executeScript("arguments[0].scrollIntoView({block: \"center\",inline: \"center\",behavior: \"smooth\"});",element);
这会将元素滚动到页面的中心。
Javascript
解决方法很简单:
const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await driver.sleep(500);
Selenium 的默认行为是滚动,因此元素几乎不会出现在视口顶部的视图中。此外,并非所有浏览器都具有完全相同的行为。这是非常不满意的。如果您像我一样录制浏览器测试的视频,您想要的是让元素滚动到视图中并垂直居中。
这是我的 Java 解决方案:
public List<String> getBoundedRectangleOfElement(WebElement we)
{
JavascriptExecutor je = (JavascriptExecutor) driver;
List<String> bounds = (ArrayList<String>) je.executeScript(
"var rect = arguments[0].getBoundingClientRect();" +
"return [ '' + parseInt(rect.left), '' + parseInt(rect.top), '' + parseInt(rect.width), '' + parseInt(rect.height) ]", we);
System.out.println("top: " + bounds.get(1));
return bounds;
}
然后,滚动,你这样称呼它:
public void scrollToElementAndCenterVertically(WebElement we)
{
List<String> bounds = getBoundedRectangleOfElement(we);
Long totalInnerPageHeight = getViewPortHeight(driver);
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("window.scrollTo(0, " + (Integer.parseInt(bounds.get(1)) - (totalInnerPageHeight/2)) + ");");
je.executeScript("arguments[0].style.outline = \"thick solid #0000FF\";", we);
}
我一直在使用 ADF 组件进行测试,如果使用延迟加载,您必须有一个单独的滚动命令。如果对象未加载并且您尝试使用 Selenium 找到它,Selenium 将引发 element-not-found 异常。
如果没有任何效果,请在单击之前尝试此操作:
public void mouseHoverJScript(WebElement HoverElement) {
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript, HoverElement);
}
在 Java 中,我们可以使用 JavaScript 进行滚动,如以下代码所示:
driver.getEval("var elm = window.document.getElementById('scrollDiv'); if (elm.scrollHeight > elm.clientHeight){elm.scrollTop = elm.scrollHeight;}");
您可以为“elm.scrollTop”变量分配所需的值。
一个解决方案是:
public void javascriptclick(String element)
{
WebElement webElement = driver.findElement(By.xpath(element));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", webElement);
System.out.println("javascriptclick" + " " + element);
}
不定期副业成功案例分享
position: fixed
的元素并且它掩盖了 Selenium 想要点击的元素时,这是一个很好的解决方案。这些固定元素通常位于底部,因此设置scrollIntoView(true)
会将其很好地移动到视口的顶部。true
传递给scrollIntoView
,如果您正在滚动到的对象在您当前所在的位置上方,则传递false
。我很难弄清楚这一点。