java selenium通过WebElement获取页面元素

package com.selenium.demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

/**
 * 获取页面element
 */
public class ElementDemo {
    private static final String URL = "https://www.csdn.net/";

    private static WebDriver driver = null;

    /**
     * 初始化driver
     */
    private static void initDriver(){
        System.setProperty("webdriver.chrome.driver","C:\\Users\\szpc-01\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    private static void quitDriver(){
        if(driver != null){
            //关闭webDriver
            driver.quit();
        }
    }

    private static void test() {
        //打开url
        driver.get(URL);
        //根据页面元素 id获取 相当于js document.getElementById('')方法
        WebElement feedlistUl = driver.findElement(By.id("feedlist_id"));
        System.out.println("feedlistUl Text:" + feedlistUl.getText());
        //根据 class获取 今日推荐
        List<WebElement> cnList = driver.findElements(By.className("company_name"));
        for(int i = 0 ;i<cnList.size(); i ++){
            System.out.println("今日推荐【"+(i+1)+"】:" + cnList.get(i).getText());
        }
        //页面存在多个 返回的不是数组则会返回第一个 findElement
        WebElement cn = driver.findElement(By.className("company_name"));
        System.out.println("今日推荐:" + cn.getText());
        //根据 name 获取 ,多个用返回数组的
        WebElement description = driver.findElement(By.name("description"));
        // getAttribute 可以获取到元素上的信息 比如 $(this).attr('content')
        System.out.println("description Text :" + description.getAttribute("content"));
        //根据元素标签获取
        WebElement inputElement = driver.findElement(By.tagName("input"));
        System.out.println("inputElement Text:" + inputElement.getAttribute("placeholder"));
        //根据a 标签的文本获取元素
        WebElement linkText = driver.findElement(By.linkText("程序人生"));
        System.out.println("linkText href:" + linkText.getAttribute("href"));
        //通过部分a标签文本获取元素
        WebElement partialLinkText = driver.findElement(By.partialLinkText("程序"));
        System.out.println("partialLinkText href:" + partialLinkText.getAttribute("href"));
        // 通过xpath获取  //input 获取页面中的input元素
        WebElement xpathElement = driver.findElement(By.xpath("//input"));
        System.out.println("xpathElement Text :" + xpathElement.getAttribute("placeholder"));
        //通过cssSelector获取 input#toolber-keyword  获取id为toolber-keyword 的input
        WebElement cssSelector = driver.findElement(By.cssSelector("input#toolber-keyword"));
        System.out.println("cssSelector Text:" + cssSelector.getAttribute("placeholder"));
    }


    public static void main(String[] args) {
        try {
            initDriver();
            test();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            quitDriver();
        }
    }
}

通过xpath,cssSelector获取详细介绍地址

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值