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 cnList = driver.findElements(By.className("company_name"));
        for(int i = 0 ;i

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

你可能感兴趣的:(Java爬虫,selenium,xpath,java)