-创建Java工程
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SearchSelenium {
public static void main(String[] args){
// 指定浏览器驱动路径(Selenium3新特性)
System.setProperty ( "webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe" );
// 创建一个ChromeDriver实例
WebDriver driver = new ChromeDriver();
// 使用driver实例打开百度的首页
driver.get("http://www.baidu.com");
// 通过输入框的id属性找到百度搜索输入框
WebElement element = driver.findElement(By.id("kw"));
// 在搜索输入框中输入“Selenium”关键字
element.sendKeys("Selenium");
// 进行搜索
element.submit();
// 关闭浏览器
driver.quit();
}
}
<element id="value">
WebElement element = driver.findElement(By.id("query"));
<element name="q">
WebElement element = driver.findElement(By.name("q"));
元素Class Name
<element class="s-inpt">
WebElement element = driver.findElement(By.class("s-inpt"));
<element href="value">LinkTextelement>
WebElement element = driver.findElement(By.linkText("有道词典"));
<element href="value">PartiaLinkTextelement>
WebElement element = driver.findElement(By.partialLinkText("词典"));
List<WebElement> element = driver.findElements(By.partialLinkText("有道"));
<tag class="value">tag>
WebElement element = driver.findElement(By.tagName("input"));
如果页面中有多个input元素,则只会返回自上而下第一个元素
WebDriver driver = new FireFoxDriver();
driver.get("http://photo.163.com");
打开网址(get)-经验总结
Webelement element = driver.findElement(By.name("username"));
element.sendKeys("[email protected]");
element.sendKeys("auto","test1","@163.com");
输入内容(sendKeys)-经验总结
WebElement element = driver.findElement(By.className("js-cur"));
element.click();
点击元素(click)-经验总结
WebElement element = driver.findElement(By.name("passsword"));
element.clear();
清空内容(clear)-经验总结
WebDriver driver = new FirefoxDriver();
driver.get("http://photo.163.com");
System.out.println(driver.getCurrentUrl);
获取网址(getCurrentUrl)-经验总结
System.out.println(driver.getTitle());
获取页面标题(getTitle)-经验总结
WebElement element = driver.findElement(By.className("album"));
System.out.println(”element.getText()“);
获取文案(getText)-经验总结
driver.quit();
关闭浏览器(quit)-经验总结
XPath语法:指定顺序【?】
XPath语法:星号 *
XPath语法:元素属性 @
XPath语法:Text
推荐第二种
XPath语法: start-with()与contains()
String currentWindow = driver.getWindowHandle();
driver.switchTo().window(currentWindow);
driver.switchTo().frame(driver.findElement(By.xpath("//div[@class=‘j-main’]//iframe")));
driver.findElement(By.xpath("//Body")).sendKeys("发表评论");
driver.switchTo().defaultContent();
Alert alert = driver.switchTo().alert();
Alert alert = driver.switchTo().alert();
alert.accept();
注意事项
关键操作:
Alert alert = driver.switchTo().alert();
alert.accept() ;
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
Action act = new Actions(driver);
act.dragAndDrop(element, target).perform();
WebElement element = driver.findElement(By.xpath("//div[@title='A']"));
WebElement target = driver.findElement(By.xpath("//div[@title='C']"));
Actions act = new Actions(driver);
act.dragAndDrop(element, target).perform();
Action builder = new Actions(driver);
Action act = builder.clickAndHold(someElement).moveToElement(otherElement).release(otherElement).build();
act.perform();
注意事项
关键操作
WebElement element = driver.findElement(By.xpath("//div[@class='ln ln0'][1]"));
Actions builder = new Actions(driver);
Action hover = builder.moveToElement(element).build();
hover.perform();
// 设置元素出现的等待时间是10s,元素默认的等待时间是0s
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// 将页面默认的加载时间从60s改为10s
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
// 设置脚本的执行时间为10s,默认是0s
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
方法二:显式等待
WebDriverWait wait = new WebDriverWait(driver, time period);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id")));