• selenium操作(浏览器)


    浏览器相关API

    1.浏览器中加载URLget()      --首先要启动浏览器

    实例:driver.get("http://www.taobao.com")

    2.浏览器最大化:window().maximize()   实例:driver.manage().window().maximize();

    3.刷新:refresh()    实例:driver.navigate().refresh();

    4.返回上一页:back()               实例:driver.navigate().back();

    5. 向前进一页:forward()        实例:driver.navigate().forward();

    6.截图:getScreenshotAs()

    实例:

    File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);  

            FileUtils.copyFile(screenShotFile, new File("D:/test.png"));

    7.获取当前页的URLgetCurrentUrl()    实例:driver.getCurrentUrl();

    8.关闭当前tab页面:close()            实例:driver.close();

    9.退出当前driverquit()                 实例:driver.quit();

    10.获取当前页的title:  getTitle()    实例:driver.getTitle(); 

    7种识别元素的方法:

    我们以百度主页搜索框为例:=

    <input autocomplete="off" maxlength="255" value="" class="s_ipt" name="wd" id="kw">

    1.通过id定位元素

    findElement(By.id("id_vaule"))

    driver.findElement(By.id("kw")).sendKeys("123456");

    WebElement bu = driver.findElement(By.id("su"));

    bu.click();

    2.通过name定位元素:

    findElement(By.name("name_vaule"))

    driver.findElement(By.name("wd")).sendKeys("123456");

    3.通过className定位元素(样式名):

    findElement(By.className("class_name"))

    driver.findElement(By.className("s_ipt")).sendKeys("123456");

    4.通过tag_name定位元素(标签名):

    findElement(By.tagName("tag_name"))

    driver.findElement(By.tagName("input")).sendKeys("123456");

    5.通过link定位:

    findElement(By.linkText("text_vaule"))

    或:findElement(By.partialLinkText("text_vaule"))

    driver.findElement(By.linkText("糯米")).click();

    driver.findElement(By.partialLinkText("")).click();

    driver.findElement(By.partialLinkText("")).click();

    6.通过css定位元素:

    findElement(By.cssSelector())

    7.通过xpath定位元素:

    findElement(By.xpath())

  • 相关阅读:
    C++ 沉思录——Chap6:句柄2
    C++ 沉思录——Chap5:代理类
    C++ 沉思录——Chap4:设计类的核查表
    Linux 网卡驱动相关——01
    FCoE的提出
    想成为嵌入式程序员应知道的0x10个基本问题
    C++ 沉思录——Chap6:句柄
    C++ 沉思录——Chap8:一个面向对象程序范例
    数据库调优积累系列(1):索引
    QTP使用问题集锦
  • 原文地址:https://www.cnblogs.com/Abby123/p/6554225.html
Copyright © 2020-2023  润新知