python selenium unittest关键字驱动_selenium--关键字驱动

package com.dn.twohomework;

import java.util.ArrayList;

import java.util.Set;

import java.util.List;

// import org.apache.bcel.generic.Select;

//import org.openqa.selenium.support.ui.Select;

//import org.apache.bcel.generic.Select;

//import com.sun.org.apache.bcel.internal.generic.Select;

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.ChromeOptions;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.support.ui.Select;

import java.util.Iterator;

import com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput;

//import com.gargoylesoftware.htmlunit.javascript.host.Iterator;

//import com.gargoylesoftware.htmlunit.javascript.host.Set;

//import com.sun.media.sound.FFT;

//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

import java.util.HashSet;;

public class keyWord1 {

private static WebDriver driver;

// 打开浏览�?

public void openBrowser(String b, String xpath) {

if ("cc".equals(b)) {

ChromeDriver chrome = new ChromeDriver("webdriver.chrome.driver", xpath);

driver = chrome.getdriver();

} else if ("ff".equals(xpath)) {

FFDriver ff = new FFDriver("webdriver.gecko.driver", xpath);

ff.getdriver();

} else if ("ie".equals(b)) {

IEDriver ie = new IEDriver("webdriver.ie.driver", xpath);

driver = ie.getdriver();

}

}

public static WebDriver getDriver() {

return driver;

}

public static void setDriver(WebDriver driver) {

keyWord1.driver = driver;

}

// 输入网址

public void geturl(String url) {

try {

driver.get(url);

//ChromeOptions options = new ChromeOptions();

//options.addArguments("--start-fullscreen");

//driver.manage().window().maximize();

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("打开浏览器失�?");

e.printStackTrace();

}

}

// 等待时间

public void sleep(String time) throws NumberFormatException, InterruptedException {

Thread.sleep(Integer.parseInt(time));

}

// 输入内容

public void input(String xpath, String word) {

try {

driver.findElement(By.xpath(xpath)).clear();

driver.findElement(By.xpath(xpath)).sendKeys(word);

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("输入字符串失�?");

e.printStackTrace();

}

}

// 点击

public void click(String xpath) {

try {

driver.findElement(By.xpath(xpath)).click();

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("点击失败");

e.printStackTrace();

}

}

// 关闭旧浏览器,切换到新窗�?

public void closeOldWin() {

List handle = new ArrayList();

Set s = driver.getWindowHandles();

// 循环获取数组里的句柄,保存到链表handle里面

for (Iterator it = s.iterator(); it.hasNext();) {

handle.add(it.next().toString());

}

driver.close();

driver.switchTo().window(handle.get(1));

}

// 关闭新浏览器,切换到旧窗�?

public void closeNewWin() {

List handle = new ArrayList();

Set s = driver.getWindowHandles();

// 循环获取数组里的句柄,保存到链表handle里面

for (Iterator it = s.iterator(); it.hasNext();) {

handle.add(it.next().toString());

}

driver.switchTo().window(handle.get(1));

driver.close();

driver.switchTo().window(handle.get(0));

}

// 进入子页�?

public void intoIframe(String xpath) {

try {

driver.switchTo().frame(driver.findElement(By.xpath(xpath)));

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("进入Iframe失败");

e.printStackTrace();

}

}

// 关闭子页�?

public void outIframe() {

driver.switchTo().defaultContent();

}

// 获取文字信息

public void getText(String xpath) {

System.out.println(driver.findElement(By.xpath(xpath)).getText());

}

// hover

public void hover(String xpath){

try {

Actions builder = new Actions(driver);

builder.moveToElement(driver.findElement(By.xpath(xpath))).build().perform();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("悬停失败");

}

}

//获取链接link

public String getLink(String xpath){

String link = "about:bliank";

try {

link = driver.findElement(By.xpath(xpath)).getAttribute("href");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("获取链接失败");

}

return link;

}

//提交表单

public void submit(String xpath){

try {

driver.findElement(By.xpath(xpath)).submit();

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("提交失败");

e.printStackTrace();

}

}

//选择下拉框

public void select(int num,String xpath){

Select sel = new Select(driver.findElement(By.xpath(xpath)));

sel.selectByIndex(num);

}

//通过js操作

public void exejs(String text){

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript(text);

}

//上传图片

public void uploadpiture(String name,String path){

try {

driver.findElement(By.name(name)).sendKeys(path);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("上传失败");

}

}

//获得元素属性的值

public String GetsTheValueOfTheElement(String xpath,String element){

try {

String getvalues =    driver.findElement(By.xpath(xpath)).getAttribute(element);

return getvalues;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("获取元素属性的值失败");

return null;

}

}

//点击alert的确认按钮

public void clickAlert(){

driver.switchTo().alert().accept();;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值