【问题标题】:scrolling to an element failed in Selenium Java在 Selenium Java 中滚动到元素失败
【发布时间】:2020-07-31 19:47:48
【问题描述】:

我正在 Firstcry .com 网站上进行自动化工作。在搜索框中搜索鞋子后,我需要向下滚动到页面底部以单击“查看所有产品”链接。但是滚动没有发生..应该做什么...附上我的代码和屏幕截图以供参考..

[public void f(String s) {
        String ExpecTitle = "Kids Footwear - Buy Baby Booties, Boys Shoes, Girls Sandals Online India";
        Actions builder = new Actions(Driver);
        Driver.get("https://www.firstcry.com/");
        String Viewall = "/html/body/div\[6\]/div\[2\]/div\[2\]/div\[2\]/div\[8\]/div\[2\]/span/a";
        String MainTitle = Driver.getTitle();
         System.out.println("Main title is " + MainTitle);
        
        WebElement SearchBox =  Driver.findElement(By.id("search_box"));
        SearchBox.clear();
        WebElement SearchBox2 =  Driver.findElement(By.id("search_box"));
        SearchBox2.sendKeys(s);
      //    SearchBox.sendKeys(Keys.ENTER);
        //wait.until(ExpectedConditions.stalenessOf(Driver.findElement(By.cssSelector(".search-button"))));
        Driver.findElement(By.cssSelector(".search-button")).click();
        
        
        String ActTitle = Driver.getTitle();
         System.out.println("The page title is " + ActTitle);
         Driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
         if(ActTitle.contains("Kids Footwear")){
                      
             System.out.println("Inside the if condition");  
             js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
             WebElement viewALL = Driver.findElement(By.xpath(Viewall));
            // js.executeScript("arguments\[0\].scrollIntoView();", viewALL);
            
             Driver.findElement(By.xpath(Viewall)).click();
             
             System.out.println("View");  
             // WebElement viewAll = Driver.findElement(By.xpath("/html/body/div\[6\]/div\[2\]/div\[2\]/div\[2\]/div\[8\]/div\[2\]/span/a"));
             // js.executeScript("arguments\[0\].scrollIntoView(true);", viewAll);
             // wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a\[contains(text(),'View All Products')\]")));
             // viewAll.click();
          }
          
              WebElement element = Driver.findElement(By.cssSelector(".sort-select-content"));
              element.click();
              builder.moveToElement(element).perform();
            
            {
              WebElement elem = Driver.findElement(By.linkText("Price"));
           elem.click();
            //  builder.moveToElement(elem).perform();
            }
            //Driver.findElement(By.linkText("Price")).click();
  }][1]

【问题讨论】:

    标签: java selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    在搜索框中搜索鞋子并选择第一个建议后,向下滚动到页面底部点击文本为查看所有产品的元素需要诱导@987654321 @ 为elementToBeClickable(),您可以使用以下 基于Locator Strategies:

    driver.get("https://www.firstcry.com/");
    WebElement searchBox = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search_box']")));
    searchBox.clear();
    searchBox.sendKeys("Shoes");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='searchlist']/ul/li/span"))).click();
    ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='View All Products']"))));
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='View All Products']"))).click();
    

    【讨论】:

    • 谢谢..它可以工作,但我不能添加额外的点击来进行排序..它是基于指令的执行......
    • @JeyanthiRanjit 这似乎是一个完全不同的用例。你能根据你的新要求提出一个新问题吗?
    【解决方案2】:

    以下代码对我有用:

        WebDriver Driver = new ChromeDriver();
        Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        Driver.manage().window().maximize();
        String url = "https://www.firstcry.com/";
        Driver.get(url);
        WebElement searchbox=Driver.findElement(By.id("search_box"));
        searchbox.clear();
        searchbox.sendKeys("shoes");
        Driver.findElement(By.xpath("/html/body/div[1]/div[5]/div/div[2]/form/span")).click();
        WebElement Element=Driver.findElement(By.partialLinkText("View All Products"));
        JavascriptExecutor js = (JavascriptExecutor) Driver;
        js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
        Element.click();
    

    【讨论】:

    • 谢谢它有效.. 我刚刚删除了第 9 行的额外点击...
    【解决方案3】:

    // 跟随代码滚动直到元素可见并点击该元素

        JavascriptExecutor js = (JavascriptExecutor) driver;
    
    
        WebElement viewALL = Driver.findElement(By.xpath(Viewall));
    
                
        js.executeScript("arguments[0].scrollIntoView();", viewALL);
         viewALL.click();
    }
    

    }

    【讨论】:

    • 抱歉,由于某种原因,它没有以这种方式滚动到元素...我试过了,但感谢您花费的时间
    猜你喜欢
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2018-08-09
    相关资源
    最近更新 更多