Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 滚动直到元素位于DOM中_Javascript_C#_Selenium - Fatal编程技术网

Javascript 滚动直到元素位于DOM中

Javascript 滚动直到元素位于DOM中,javascript,c#,selenium,Javascript,C#,Selenium,您好,我正在使用这段代码尝试滚动页面,直到元素位于DOM中。然而,这一页并没有滚动,它只是一个接一个地循环。我的IJavaScriptExecutor错误吗 public static void ScrollUntilElementinDom(this IWebDriver driver, By by) { bool isPresent = false; while (isPresent == false) { try {

您好,我正在使用这段代码尝试滚动页面,直到元素位于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);");

        }

    }

尝试使用
操作滚动

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 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秒,使元素可见。

  • 您正在滚动窗口以触发加载更多内容
  • 您希望继续滚动窗口,直到您查找的内容加载完毕
您需要等待加载内容

你决不会等。考虑<代码> WebDriverWait <代码> < /P> < UL>
  • 您正在滚动窗口以触发加载更多内容
  • 您希望继续滚动窗口,直到您查找的内容加载完毕
  • 您需要等待加载内容


    你决不会等。考虑<代码> WebDriverWait <代码>

    ,您应该使用Actudio类来执行元素的滚动。
    WebElement element = driver.findElement(By.id("element-id"));
    Actions actions = new Actions(driver);
    actions.moveToElement(element);
    actions.perform();
    

    您应该使用Actions类执行滚动到元素的操作

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

    注意:元素不在dom中,滚动将触发元素。注意:元素不在dom中,滚动将触发元素。正确。但是我不知道webdriverwait有多有用,一个while循环应该足够了吧?好的,我将插入一个暂停。但是我的Javascript不会滚动页面,你知道为什么吗?正确。但是我不知道webdriverwait有多有用,一个while循环应该足够了吧?好的,我将插入一个暂停。但是,我的Javascript不会滚动页面,您知道为什么吗?使用操作(在本例中)的唯一警告是,这是一个“本机浏览器事件”。使用非本机JavascriptExecutor可能会获得更快/不同/更好的结果。使用操作(在本例中)的唯一警告是,这是一个“本机浏览器事件”。使用非本机JavascriptExecutor可能会得到更快/不同/更好的结果。