zoukankan      html  css  js  c++  java
  • 章节十四、4-如何获取窗口的大小、如何把元素滚动到页面可见位置

    一、如何用JavascriptExecutor获取浏览器窗口的大小

     1 package basicweb;
     2 
     3 import static org.junit.jupiter.api.Assertions.*;
     4 
     5 import java.util.concurrent.TimeUnit;
     6 
     7 import org.junit.jupiter.api.AfterEach;
     8 import org.junit.jupiter.api.BeforeEach;
     9 import org.junit.jupiter.api.Test;
    10 import org.openqa.selenium.JavascriptExecutor;
    11 import org.openqa.selenium.WebDriver;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 
    14 class WindowSize {
    15     
    16     WebDriver driver;
    17 //    声明一个私有的JavascriptExecutor变量
    18     private JavascriptExecutor js;
    19 
    20     @BeforeEach
    21     void setUp() throws Exception {
    22         driver = new ChromeDriver();
    23 //        把driver强制转换成JavascriptExecutor类型
    24         js = (JavascriptExecutor)driver;
    25         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    26         driver.manage().window().maximize();
    27     }
    28 
    29     @Test
    30     void test() throws InterruptedException {
    31 //        用Javascript的方式打开百度
    32         js.executeScript("window.location = 'https://www.baidu.com';");
    33 //        用Long类型的变量来接收窗口的长和宽
    34 //
    35         Long height = (Long)js.executeScript("return window.innerHeight;");
    36 //
    37         Long width = (Long)js.executeScript("return window.innerWidth;");
    38         System.out.println("窗口的高度为:"+height);
    39         System.out.println("窗口的宽度为:"+width);
    40 
    41     }
    42     
    43     @AfterEach
    44     void tearDown() throws Exception {
    45         Thread.sleep(2000);
    46         driver.quit();
    47     }
    48 
    49 
    50 }

    运行结果为:

    二、如何把元素滚动到页面可见位置

    1、当我们打开页面时,默认是在页面的最顶部,其余的页面部分是被遮盖的,我们通过滑动鼠标齿轮才能够看到网页剩余的部分。

    2、如果页面是被遮盖的,那么这个时候如果需要操作被遮盖的元素,就有可能会失败。(可能会失败)因此这种情况下我们就需要滚动页面元素到可见位置。

     1 package basicweb;
     2 
     3 import java.util.concurrent.TimeUnit;
     4 
     5 import org.junit.jupiter.api.AfterEach;
     6 import org.junit.jupiter.api.BeforeEach;
     7 import org.junit.jupiter.api.Test;
     8 import org.openqa.selenium.By;
     9 import org.openqa.selenium.JavascriptExecutor;
    10 import org.openqa.selenium.WebDriver;
    11 import org.openqa.selenium.WebElement;
    12 import org.openqa.selenium.chrome.ChromeDriver;
    13 
    14 class ScrollingElementIntoView {
    15     
    16     WebDriver driver;
    17 //    声明一个私有的JavascriptExecutor变量
    18     private JavascriptExecutor js;
    19 
    20     @BeforeEach
    21     void setUp() throws Exception {
    22         driver = new ChromeDriver();
    23 //        把driver强制转换成JavascriptExecutor类型
    24         js = (JavascriptExecutor)driver;
    25         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    26         driver.manage().window().maximize();
    27     }
    28 
    29     @Test
    30     void test() throws InterruptedException {
    31         js.executeScript("window.location = 'https://p2p.migang.com/';");
    32         Thread.sleep(2000);
    33 
    34 //        1、滚动到页面最底部
    35 //        第一个数字代表水平位置,因为水平位置不变,因此设置为0
    36 //        第一个数字代表垂直位置,因为需要滚动到页面最底部,所以垂直设置的数值要大于等于窗口垂直的实际数值
    37         js.executeScript("window.scrollBy(0, 1900);");
    38         Thread.sleep(2000);
    39 
    40 //        2、滚回先前的位置(还未执行滚动到页面最底部的位置,也就是页面最顶部)
    41         js.executeScript("window.scrollBy(0,-1900);");
    42         Thread.sleep(2000);
    43         
    44 //        3、滚动到要操作的元素位置
    45         WebElement element = driver.findElement(By.xpath("//h2/span[contains(text(),'媒体报道')]"));
    46 //        arguments[0]不是数组,是一个arguments对象,arguments[0]返回的是第一个参数的实际值,0会被argument代替,argument就是上面element这个元素
    47 //        我们要把这个元素提供给JavascriptExecutor
    48         js.executeScript("arguments[0].scrollIntoView(true);",element);
    49         Thread.sleep(2000);
    50         js.executeScript("window.scrollBy(0,-190);");
    51         Thread.sleep(2000);
    52         element.click();
    53     }
    54     
    55     @AfterEach
    56     void tearDown() throws Exception {
    57         Thread.sleep(2000);
    58         driver.quit();
    59     }
    60 }

    运行结果:

    滚动到最底部---》最顶部---》滚动到“媒体报道”模块(但被导航条遮盖)---》滚动显示出“媒体报道”模块

    在很多网站中,当我们刚进入一个网站页面,进行滑动时,导航条会固定在顶部显示,因此我们在代码中滚动到“媒体报道”模块后还需要向上滚动一点,这是因为滑动后顶部固定的导航条遮住了我们需要定位的元素。

    如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。

  • 相关阅读:
    如何理解css3 -webkit-animation-fill-mode属性值为both时的使用方法
    关于对canvas.beginPath()的理解
    [cf10E]Greedy Change
    [atAGC055B]ABC Supremacy
    [loj6734]图上的游戏
    [gym102412D]The Jump from Height of Self-importance to Height of IQ Level
    [Aizu1410]Draw in Straight Lines
    [Aizu2993]Invariant Tree
    [zoj3990]Tree Equation
    [hdu6326]Monster Hunter
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/10918194.html
Copyright © 2011-2022 走看看