[英]Scroll until element is in DOM
嗨,我正在使用此代码尝试滚动页面,直到元素位于DOM中。 但是,该页面不会滚动,它只会循环播放。 我的IJavaScriptExecutor
错误吗?
public static void ScrollUntilElementinDom(this IWebDriver driver, By by)
{
bool isPresent = false;
while (isPresent == false)
{
try
{
isPresent = driver.FindElement(by).Displayed;
}
catch (Exception)
{
}
if (isPresent == true)
{
break;
}
else
{
((IJavaScriptExecutor) driver).ExecuteScript("window.scrollBy(100,0);");
}
}
您需要等待内容加载 。
您根本没有在等。 考虑使用WebDriverWait
尝试使用Actions
滚动
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(by)).Build().Perform();
并查找是否显示该元素,可以使用显式等待
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
try
{
wait.Until(ExpectedConditions.ElementIsVisible(by));
isPresent = true;
}
catch (Exception) { }
这将等待15秒钟才能显示该元素。
您应该使用Actions类来执行到元素的滚动。
WebElement element = driver.findElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.