Selenium的基本操作(2)(phantomjs,组元素)

package com.main;

import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.google.common.io.Files;

public class Assert1 {
//小米登陆
	public void fireFox() {

		try {
			System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");

			WebDriver driver = new FirefoxDriver();

			driver.get("https://www.mi.com/index.html");

			System.out.println(driver.getCurrentUrl());
			
			driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/a[1]")).click();
			
			Thread.sleep(5000);
			
			driver.findElement(By.cssSelector("html body div#J_agreeModal.modal.fade.modal-agree.in.modal-hide div.modal-ft div.actions button.btn.btn-primary.J_sure")).click();;
			
			Thread.sleep(5000);
			
			driver.findElement(By.id("username")).sendKeys("xxxx");
			
			driver.findElement(By.id("pwd")).sendKeys("xxxx");
			
			Thread.sleep(1000);
			
			driver.findElement(By.id("login-button")).click();
			
			Thread.sleep(1000);
			
			// 获取截屏
			File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
			// 用io复制到指定路径
			Files.copy(scrFile, new File("d:\\screenfile.png"));

			Thread.sleep(2000);
			//获取前端网站源码
		    driver.getPageSource();
			
			driver.close();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("出错了");
		}

	}
	//稍微操作一组元素
	public void fireFoxElements() {

		try {
			System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");

			WebDriver driver = new FirefoxDriver();
			
			driver.get("https://www.baidu.com/");
			
			driver.findElement(By.cssSelector("#kw")).sendKeys("海瑞");

			driver.findElement(By.xpath(".//*[@id='su']")).click();
			
			Thread.sleep(500);
			//获取一个元素集合
			List<WebElement> findElements = driver.findElements(By.cssSelector("div>div[id]>h3>a"));
			
			int size = findElements.size();
			
			System.err.println(size);
			
			for (WebElement webElement : findElements) {
				//获取内容文本
				System.out.println(webElement.getText());
				//获取标签里的内容
				System.out.println(webElement.getAttribute("href"));
				
			}
			
			driver.quit();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("出错了");
		}

	}

	public void phantomjs() {

		System.setProperty("phantomjs.binary.path", "d:\\BS\\phantomjs.exe");
		
		WebDriver driver = new PhantomJSDriver();

		driver.get("https://mail.126.com/");

		System.out.println(driver.getCurrentUrl());
		// 获取截屏
		File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);

		try {
			// 用io复制到指定路径
			Files.copy(scrFile, new File("d:\\screenfile.png"));

			/*
			 * WebElement findElement =
			 * driver.findElement(By.linkText("邮箱帐号登录"));
			 * 
			 * Actions actionOpenLinkInNewTab = new Actions(driver);
			 * 
			 * actionOpenLinkInNewTab.moveToElement(findElement).perform();
			 */

			Thread.sleep(2000);

			driver.close();

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("出错了");
		}

	}

	public static void main(String[] args) {

		 //new Assert1().fireFox(); 
		/*new Assert1().phantomjs();*/
		 
		 new Assert1().fireFoxElements();
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值