駿域網(wǎng)站建設(shè)專家/seo排名資源
一、環(huán)境配置
二、瀏覽器適配
//1.設(shè)置瀏覽器的位置,google瀏覽器位置是默認(rèn)且固定在電腦里的//2.設(shè)置瀏覽器驅(qū)動(dòng)的位置,C:\Users\27743\AppData\Local\Google\Chrome\ApplicationSystem.setProperty("webdriver.chrome.driver", "C:\\Users\\27743\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");//3.加載瀏覽器
// ChromeDriver chromeDriver = new ChromeDriver();WebDriver driver = new ChromeDriver();//4.通過(guò)chromeDriver打開(kāi)瀏覽器
// chromeDriver.get("https://www.baidu.com/");driver.get("https://www.baidu.com/");
這里配置selenium環(huán)境,推薦這篇文章全國(guó)大學(xué)生軟件測(cè)試大賽Web應(yīng)用測(cè)試(二)Selenium功能測(cè)試環(huán)境配置_慕測(cè)平臺(tái)的eclipse插件-CSDN博客
通過(guò)這篇文章來(lái)學(xué)習(xí)?
02_瀏覽器適配_嗶哩嗶哩_bilibili?根據(jù)這個(gè)視頻操作
設(shè)置自動(dòng)補(bǔ)全功能?
三、瀏覽器基本操作
package selenium;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.chrome.ChromeDriver;public class demo1 {public static void main(String[] args) {try {// TODO Auto-generated method stub//1.設(shè)置瀏覽器的位置,google瀏覽器位置是默認(rèn)且固定在電腦里的//2.設(shè)置瀏覽器驅(qū)動(dòng)的位置,C:\Users\27743\AppData\Local\Google\Chrome\ApplicationSystem.setProperty("webdriver.chrome.driver", "C:\\Users\\27743\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");//3.加載瀏覽器
// ChromeDriver chromeDriver = new ChromeDriver();WebDriver driver = new ChromeDriver();//4.通過(guò)chromeDriver打開(kāi)瀏覽器
// chromeDriver.get("https://www.baidu.com/");driver.get("https://www.baidu.com/");Thread.sleep(1500);//瀏覽器的操作//1.瀏覽器最大化driver.manage().window().maximize();
// Options manage = driver.manage();
// manage.window().maximize();Thread.sleep(1500);//2.獲取導(dǎo)航類
// Navigation nat = driver.navigate();
// nat.to("https://www.baidu.com/s?wd=%E6%B8%85%E6%BE%88%E7%9A%84%E7%88%B1%E5%8F%AA%E4%B8%BA%E4%B8%AD%E5%9B%BD&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1");driver.navigate().to("https://www.baidu.com/s?wd=%E6%B8%85%E6%BE%88%E7%9A%84%E7%88%B1%E5%8F%AA%E4%B8%BA%E4%B8%AD%E5%9B%BD&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1");Thread.sleep(1500);//2.1瀏覽器后退
// nat.back();driver.navigate().back();Thread.sleep(1500);//2.2瀏覽器前進(jìn)
// nat.forward();driver.navigate().forward();Thread.sleep(1500);//2.3瀏覽器的刷新driver.navigate().refresh();Thread.sleep(1500);//3.獲取當(dāng)前標(biāo)題和urlString title = driver.getTitle();System.out.println("title:"+ title);System.out.println("url:"+driver.getCurrentUrl());//3.1重新打開(kāi)瀏覽器,我們看當(dāng)前的標(biāo)題和url地址driver.get("https://www.baidu.com/");driver.navigate().to("https://yiyan.baidu.com/");System.out.println("title:"+ driver.getTitle());System.out.println("url:"+driver.getCurrentUrl());//last:關(guān)閉瀏覽器driver.quit();//last:關(guān)閉標(biāo)簽
// driver.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}
四、元素的基本操作
package selenium;import java.util.List;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;public class demo2 {public static void main(String[] args) {// TODO Auto-generated method stubtry {//1.設(shè)置瀏覽器驅(qū)動(dòng)的位置System.setProperty("webdriver.chrome.driver", "C:\\Users\\27743\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");//2.加載瀏覽器WebDriver driver = new ChromeDriver();//3.打開(kāi)瀏覽器driver.get("file:///D:/RuanJian/selenium/code/demo1/index.html");//4.元素的基本操作,先定位后操作//定位WebElement input_name = driver.findElement(By.xpath("//*[@id=\"user2\"]"));System.out.println(input_name.getAttribute("value"));//測(cè)試賬號(hào)//清空input_name.clear();//輸入input_name.sendKeys("李聰聰");Thread.sleep(1000);//獲取value屬性的內(nèi)容String value = input_name.getAttribute("value");//李聰聰System.out.println(value);//輸入密碼driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("123456");Thread.sleep(1000);//單選框選擇女driver.findElement(By.xpath("/html/body/table/tbody/tr[4]/td[2]/input[2]")).click();Thread.sleep(1000);//復(fù)選框選擇唱歌driver.findElement(By.xpath("/html/body/table/tbody/tr[5]/td[2]/input[3]")).click();Thread.sleep(1000);//下拉表單選擇陜西,這個(gè)比較特殊,先獲取這個(gè)元素,然后加載成select,最后進(jìn)行操作WebElement select_table = driver.findElement(By.xpath("/html/body/table/tbody/tr[6]/td[2]/select"));Select select = new Select(select_table);//下拉表單,獲取其中的值List<WebElement> options = select.getOptions();//法一for (WebElement webElement : options) {String text = webElement.getText();System.out.println(text);}//法二for (int i = 0; i < options.size(); i++) {WebElement webElement = options.get(i);String text = webElement.getText();System.out.println("index"+text);}//通過(guò)順序選擇 0開(kāi)始select.selectByIndex(4);//陜西Thread.sleep(1000);//通過(guò)文字選擇select.selectByVisibleText("北京");Thread.sleep(1000);//點(diǎn)擊超鏈接driver.findElement(By.xpath("/html/body/table/tbody/tr[8]/td[2]/a")).click();//要確定到超鏈接那里Thread.sleep(1000);driver.navigate().back();Thread.sleep(1000);//多行文本輸入driver.findElement(By.xpath("/html/body/table/tbody/tr[9]/td[2]/textarea")).sendKeys("666666");Thread.sleep(1000);//5.關(guān)閉瀏覽器driver.quit();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}
?