如何让 Selenium WebDriver 滚动到特定元素以将其显示在屏幕上。我尝试了很多不同的选择,但没有运气。 这在 C# 绑定中不起作用吗?
我可以让它跳转到特定位置例如
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)");
但我希望能够将其发送到不同的元素,而不需要每次都给出确切的位置。
public IWebElement Example { get { return Driver.FindElement(By.Id("123456")); } }
例 1)
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView(true);", Example);
例 2)
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollBy(Example.Location.X", "Example.Location.Y - 100)");
我观看的时候,并没有向下跳转页面到该元素,异常匹配的是离屏的元素。
我在它后面添加了一个
bool ex = Example.Exists();
并检查了结果。
它确实存在(这是真的)。
它没有显示(因为它仍然在屏幕外,因为它没有移动到元素)
未选择??????
有人看到了 By.ClassName 的成功。 有谁知道在 C# 绑定中执行此 By.Id 是否有问题?
这是一个比较老的问题,但我相信有比上面建议更好的解决方案。
这是原始答案:https://stackoverflow.com/a/26461431/1221512
您应该使用 Actions 类来执行滚动到元素的操作。
var element = driver.FindElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();
这对我在 Chrome、IE8 和 IE11 中有效:
public void ScrollTo(int xPosition = 0, int yPosition = 0)
{
var js = String.Format("window.scrollTo({0}, {1})", xPosition, yPosition);
JavaScriptExecutor.ExecuteScript(js);
}
public IWebElement ScrollToView(By selector)
{
var element = WebDriver.FindElement(selector);
ScrollToView(element);
return element;
}
public void ScrollToView(IWebElement element)
{
if (element.Location.Y > 200)
{
ScrollTo(0, element.Location.Y - 100); // Make sure element is in the view but below the top navigation pane
}
}
这对我有用:
var elem = driver.FindElement(By.ClassName("something"));
driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem);
这在 C# 自动化中对我有用:
public Page scrollUp()
{
IWebElement s = driver.FindElement(By.Id("your_locator")); ;
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("arguments[0].scrollIntoView(false);", s);
return this;
}
我为 IWebDriver 创建了一个扩展:
public static IWebElement GetElementAndScrollTo(this IWebDriver driver, By by)
{
var js = (IJavaScriptExecutor)driver;
try
{
var element = driver.FindElement(by);
if (element.Location.Y > 200)
{
js.ExecuteScript($"window.scrollTo({0}, {element.Location.Y - 200 })");
}
return element;
}
catch (Exception ex)
{
return null;
}
}
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'})", PutYourElementIDHere);
为了在页面内向下滚动,我有小代码和解决方案
我的场景是直到我向下滚动页面。接受和不接受按钮未启用。我有 15 个条款和条件,我需要通过检查网页并获取最后一个条款和条件段落的 ID 来选择第 15 个条款和条件。
driver.FindElement(By.Id("para15")).Click();
<div id="para15">One way Non-Disclosure Agreement</div>
我也遇到了同样的问题。我正在处理网页,需要单击子窗口上的按钮,默认情况下,该按钮位于屏幕下方。 这是我使用的代码并且它有效。 实际上我只是模拟鼠标拖放并将窗口向上移动 250 点,以便按钮位于屏幕中。
Actions action = new Actions(driver);
action.DragAndDropToOffset(driver.FindElement(By.XPath("put an element path which **is in the screen now**, such as a label")), 0, -250);
action.Build().Perform();
如果我们放置时间的原因是加载页面的时间较长,我们放置它。就这样吧。
ChromeOptions options = new ChromeOptions();
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.w3schools.com/");
Thread.Sleep(5000);
driver.ExecuteScript("scroll(0,400)");
HtmlDocument countriesDocument = new HtmlDocument();
countriesDocument.LoadHtml(driver.PageSource);
我提供在特定元素内滚动的解决方案,例如可滚动表格。
// Driver is the Selenium IWebDriver
IJavaScriptExecutor exec = (IJavaScriptExecutor) Driver;
int horizontalScroll= direction == Direction.Right ? X : 0;
int verticalScroll = direction == Direction.Down ? Y : 0;
exec.ExecuteScript(
"arguments[0].scrollBy(arguments[1], arguments[2])"
, Self
, horizontalScroll
, verticalScroll);
Actions actions = new Actions(driver);
actions.SendKeys(Keys.PageDown).Build().Perform();
你做到了,它就像发条一样工作,简单,但并不总是方便
var e = driver.FindElement(By.XPath("//*[text()='Timesheet']"));
// JavaScript Executor to scroll to element
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", e);
C# 感谢这个在我的测试用例中为我工作
driver.FindElement(By.LinkText("Home")).SendKeys(Keys.PageDown);