chromedriver的版本一定要与Chrome的版本一致,不然就不起作用。
有两个下载地址:
1、http://chromedriver.storage.googleapis.com/index.html
2、ChromeDriver Mirror
Java代码 展示了基本的 *** 作
element.getAttribute("outerHTML") //获取Html值
element.getAttribute("outerHTML");//获取html html = driver.execute_script("return document.documentElement.outerHTML") html = driver.find_element_by_xpath("//*").get_attribute("outerHTML") # 不要用 driver.page_source,那样得到的页面源码不标准 获取元素的所有属性 Object[] attr = ((JavascriptExecutor)seleniumdriver).executescript("return arguments[0].attributes);", webElement); String source=driver.findElement(By.xpath("/html/body/script[6]")).getAttribute("innerHTML");
package xg; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.security.interfaces.ECKey; public class ChromeTest { public static void main(String[] args) throws InterruptedException { //引入驱动 System.setProperty("webdriver.chrome.driver", "src\chromedriver-97.exe"); //启动谷歌 ChromeOptions option = new ChromeOptions(); option.setBinary("C:\Program Files\Google\Chrome\Application\chrome.exe"); WebDriver driver = new ChromeDriver(option); // ChromeDriver driver = new ChromeDriver(); //访问百度 driver.get("http://www.baidu.com"); //定位元素 WebElement element = null; try { //id //element=driver.findElementById("kw"); //name // element = driver.findElementByName("wd"); Thread.sleep(2000); element=driver.findElement(By.tagName("body")); element=driver.findElement(By.id("#mainBox")); System.out.println("element.getText() = " + element.getText()); System.out.println("element.getText() = " + element.getText()); //className //element=driver.findElementByClassName("s_ipt"); //css与属性 //element=driver.findElementByCssSelector("[name=wd]"); //element=driver.findElementByCssSelector("#kw"); //element=driver.findElement(By.cssSelector("[name='wd'][autocomplete='off']")); //element=driver.findElement(By.cssSelector("span>input#kw")); //element=driver.findElement(By.cssSelector("form#form>span>input#kw")); //xpath //element=driver.findElementByXPath("//*[@id="kw"]"); //输入框输入hello // element.sendKeys("hello"); // Thread.sleep(2000); //清除文本 // element.clear(); // Thread.sleep(2000); // element.sendKeys("selenium"); //回车 //element.submit(); //点击事件 // driver.findElement(By.id("su")).click(); //超链接 //element=driver.findElementBylinkText("学术"); //模糊超链接 //element=driver.findElementByPartiallinkText("术"); //element.click(); } catch (Exception e) { System.out.println("定位失败"); } Thread.sleep(2000); //窗口大小 driver.manage().window().setSize(new Dimension(500, 800)); Thread.sleep(2000); //窗口最大 driver.manage().window().maximize(); Thread.sleep(2000); //输出当前路径 System.out.println(driver.getCurrentUrl()); //浏览器后退 driver.navigate().back(); Thread.sleep(2000); //浏览器前进 driver.navigate().forward(); Thread.sleep(2000); //刷新浏览器 driver.navigate().refresh(); Thread.sleep(5000); //休眠后后关闭浏览器 driver.close(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)