Loading...
墨滴

安康

2021/11/15  阅读:30  主题:默认主题

1. Java语言下Selenium实例代码

下载配置

  1. pom 导入依赖,地址:https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/4.0.0
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>
  1. 下载谷歌驱动,地址:http://npm.taobao.org/mirrors/chromedriver/

  2. 简单测试实例

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class demo {

    public static void main(String[] args) throws InterruptedException {
        //配置浏览器驱动地址
        System.setProperty("webdriver.chrome.driver""/Users/admin/Desktop/chromedriver");
        //打开Chrome浏览器
        WebDriver webDriver = new ChromeDriver();
        TimeUnit.SECONDS.sleep(5);
        //打开百度网站
        webDriver.get("https://www.baidu.com");
        TimeUnit.SECONDS.sleep(2);
        //输入框输入搜索关键词 selenium 中文官网
        webDriver.findElement(By.id("kw")).sendKeys("selenium 中文官网");
        TimeUnit.SECONDS.sleep(2);
        //点击百度一下按钮
        webDriver.findElement(By.id("su")).submit();
        TimeUnit.SECONDS.sleep(2);
        //查询所有搜索的结果
        List<WebElement> resultElements = webDriver.findElements(By.className("result"));
        if (!resultElements.isEmpty()) {
            //找到第一条结果的第一个链接
            List<WebElement> aTagElements = resultElements.get(0).findElements(By.tagName("a"));
            if (!aTagElements.isEmpty()) {
                //新开一个窗口打开此链接
                String href = aTagElements.get(0).getAttribute("href");
                System.out.println(href);
                ((JavascriptExecutor) webDriver).executeScript(String.format("window.open('%s')", href));
            }
        }
        TimeUnit.SECONDS.sleep(10);
        //关闭浏览器
        webDriver.quit();
    }

}

代码中展示的常用的方法如下:

  1. 定位元素

findElement(By.id("kw"))
findElement(By.tagName("a"))
findElement(By.name("kw"))
findElement(By.className("kw"))
driver.findElement(By.linkText("新闻")
driver.findElement(By.partialLinkText("新")

driver.findElement(By.xpath("//[@id='kw']"))
driver.findElement(By.xpath("//
[@name='wd']"))
driver.findElement(By.xpath("//input[@class='s_ipt']"))
driver.findElement(By.xpath("/html/body/form/span/input"))
driver.findElement(By.xpath("//span[@class='soutu-btn']/input"))
driver.findElement(By.xpath("//form[@id='form']/span/input"))
driver.findElement(By.xpath("//input[@id='kw' and @name='wd']"))

driver.findElement(By.cssSelector("#kw")
driver.findElement(By.cssSelector("[name=wd]")
driver.findElement(By.cssSelector(".s_ipt")
driver.findElement(By.cssSelector("html > body > form > span > input")
driver.findElement(By.cssSelector("span.soutu-btn> input#kw")
driver.findElement(By.cssSelector("form#form > span > input")

  1. 执行方法

    getAttribute() //获取文本信息
    sendKeys() //输入内容
    submit() //点击
    TimeUnit.SECONDS.sleep(2); //等待
    ((JavascriptExecutor) webDriver).executeScript(String.format("window.open('%s')", href));

一、使用js执行打开https://www.baidu.com功能
js=(JavascriptExecutor)driver;
js.executorScript("window.location='https://www.baidu.com';");

或者直接如下使用;只是可读性有点差
((JavascriptExecutor) driver).executeScript("window.location='https://www.baidu.com';");

注释:window.location打开的地址

参考地址:
https://blog.csdn.net/qq_22003641/article/details/79137327
https://blog.csdn.net/ABestRookie/article/details/121043640
https://blog.csdn.net/jierxiaoyao/article/details/105009045
https://www.cnblogs.com/strongmore/p/15110802.html

安康

2021/11/15  阅读:30  主题:默认主题

作者介绍

安康