Dan*_*and 185 selenium scroll automated-tests
在Selenium 1.x或2.x中是否有任何方法可以滚动浏览器窗口,以便XPath识别的特定元素可以在浏览器中查看?Selenium中有一个焦点方法,但它似乎没有在FireFox中物理滚动视图.有没有人对如何做到这一点有任何建议?
我需要这个的原因是我正在测试页面上元素的点击.不幸的是,除非元素可见,否则事件似乎不起作用.我无法控制单击该元素时触发的代码,因此我无法对其进行调试或修改,因此,最简单的解决方案是将项目滚动到视图中.
Ami*_*ith 189
已经尝试过很多关于滚动的东西,但是下面的代码提供了更好的结果.
这将滚动直到元素在视图中:
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
Run Code Online (Sandbox Code Playgroud)
小智 141
您可以使用org.openqa.selenium.interactions.Actions
该类移动到元素.
Java的:
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.sl.find_element_by_id('my-id')).perform()
Run Code Online (Sandbox Code Playgroud)
小智 28
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
Run Code Online (Sandbox Code Playgroud)
你可能想试试这个.
小智 15
如果要使用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));");
Run Code Online (Sandbox Code Playgroud)
小智 8
webElement = driver.findElement(By.xpath("bla-bla-bla"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", webElement);
Run Code Online (Sandbox Code Playgroud)
有关更多示例,请转到此处.全部用俄语,但Java代码是跨文化的:)
我发现我的元素的边界矩形不正确,导致浏览器滚动到屏幕之外。然而,下面的代码对我来说效果很好:
private void scrollToElement(WebElement webElement) throws Exception {
((JavascriptExecutor)webDriver).executeScript("arguments[0].scrollIntoViewIfNeeded()", webElement);
Thread.sleep(500);
}
Run Code Online (Sandbox Code Playgroud)
Selenium 2尝试滚动到元素,然后单击它.这是因为Selenium 2不会与元素交互,除非它认为它是可见的.
滚动到元素是隐式发生的,因此您只需要找到该项目然后使用它.
使用驱动程序发送键,例如pagedown或downarrow键,以使该元素可见。我知道这太简单了,可能无法在所有情况下都适用。
小智 5
这为我工作:
IWebElement element = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId))[0];
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);
Run Code Online (Sandbox Code Playgroud)
小智 5
在Selenium中,我们需要借助JavaScript执行程序来滚动到某个元素或滚动页面:
je.executeScript("arguments[0].scrollIntoView(true);", element);
Run Code Online (Sandbox Code Playgroud)
在上面的语句中,element
是我们需要滚动的确切元素。
我尝试了上面的代码,它为我工作。
我对此有完整的帖子和视频:
http://learn-automation.com/how-to-scroll-into-view-in-selenium-webdriver/
您可以使用以下代码片段进行滚动:
C#
var element = Driver.FindElement(By.Id("element-id"));
Actions actions = new Actions(Driver);
actions.MoveToElement(element).Perform();
Run Code Online (Sandbox Code Playgroud)
你有它
归档时间: |
|
查看次数: |
382756 次 |
最近记录: |