selenuim的基本操作
作者:互联网
1 模拟项目进行操作
package com.bw; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import java.io.*; import java.nio.file.Files; public class BDtest { public static void main(String[] args) throws InterruptedException, IOException { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://localhost:1080/webtours/"); //登录 driver.switchTo().defaultContent(); driver.switchTo().frame("body"); driver.switchTo().frame("navbar"); driver.findElement(By.name("username")).sendKeys("zhangyunxing"); Thread.sleep(1000); driver.findElement(By.name("password")).sendKeys("123456"); driver.findElement(By.name("login")).click(); // 延时 Thread.sleep(1000); // 搜票 driver.switchTo().defaultContent(); driver.switchTo().frame("body"); driver.switchTo().frame("navbar"); driver.findElement(By.xpath("//img[@alt=\'Search Flights Button\']")).click(); Thread.sleep(1000); // 选票 Thread.sleep(1000); driver.switchTo().defaultContent(); driver.switchTo().frame("body"); driver.switchTo().frame("info"); String depart=""; String arrive=""; File file = new File("D:\\zyx.txt"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String row=""; while ((row=bufferedReader.readLine())!=null){ String[] split=row.split(","); depart=split[0]; arrive=split[1]; } new Select(driver.findElement(By.name("depart"))) .selectByVisibleText(depart); Thread.sleep(1000); new Select(driver.findElement(By.name("arrive"))) .selectByVisibleText(arrive); Thread.sleep(1000); driver.findElements(By.name("seatPref")).get(1).click(); Thread.sleep(1000); // 点击下一步 Thread.sleep(1000); driver.findElement(By.name("findFlights")).click(); // findFlights // 点击下一步 Thread.sleep(1000); driver.findElement(By.name("reserveFlights")).click(); // 点击下一步 Thread.sleep(1000); driver.findElement(By.name("buyFlights")).click(); // 退出 Thread.sleep(1000); driver.switchTo().defaultContent(); driver.switchTo().frame("body"); driver.switchTo().frame("navbar"); driver.findElement(By.xpath("//img[@alt=\'SignOff Button\']")).click(); Thread.sleep(3000); driver.close();
2 下拉框以及八大定位非常重要
//单选 // driver.findElements(By.name("xb")).get(1).click(); //多选 // driver.findElements(By.name("xsah")).get(0).click(); // driver.findElements(By.name("xsah")).get(2).click(); //下拉框 // new Select(driver.findElement(By.name("jg"))).selectByIndex(2); // new Select(driver.findElement(By.name("jg"))).selectByValue("sh"); // new Select(driver.findElement(By.name("jg"))).selectByVisibleText("天津"); //定位1 id // driver.findElement(By.id("uname")).sendKeys("zhangyunxing"); //定位2 name // driver.findElement(By.name("mm")).sendKeys("123456"); //定位3 tagname // WebElement input = driver.findElement(By.tagName("input")); // System.out.println(); //定位4 超链接 // WebElement top = driver.findElement(By.linkText("top")); // top.click(); //定位5 超级链接 // driver.findElement(By.partialLinkText("左")).click(); //定位6 xpath 相对路径和绝对路劲 // WebElement element = driver.findElement(By.xpath("//input[@id='uname']")); // element.sendKeys("zhangsan"); //定位7 className // driver.findElement(By.className("text")).sendKeys("lis"); //定位8 css // driver.findElement(By.cssSelector("form>input")).sendKeys("zhouighfhgf");
标签:name,Thread,selenuim,driver,findElement,基本操作,click,sleep 来源: https://www.cnblogs.com/zyx0623/p/14204981.html