日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用java自动化访问百度测试_java+eclipse+selenium+百度搜索设置自动化测试

發布時間:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用java自动化访问百度测试_java+eclipse+selenium+百度搜索设置自动化测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在eclipse中新建一個項目baidutest;再在src文件夾中新建一個包com.baidutest.homework;再在包中新建一個java類baidusetting,并設置為靜態類;最后在項目中新建一個lib文件夾,將selenium的所有架包拷貝到lib文件夾中,并設置選中項目baidutest,右鍵選擇Bulid Path->Configure Bulid Path,將lib中的所有架包添加到Libraries中。至此selenium包配置完畢。

有什么疑惑可以參照百度經驗:eclipse+java添加外部架包

現在要做的是:打開百度頁面,點擊設置中的搜索設置,修改搜索設置中的選項。代碼如下:

package com.baidutest.homework;

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

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.By.ById;

import java.util.*;//保證List使用正確

public class baidusetting {

public static void main(String[] args) throws InterruptedException {

// TODO Auto-generated method stub

//告訴計算機瀏覽器驅動在哪里

System.setProperty("webdriver.chrome.driver",

"C:\\Users\\Dell\\Downloads\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

//訪問百度網址

driver.get("https://www.baidu.com/");

driver.manage().window().maximize();//窗口最大化

//模擬將鼠標放置在搜索上

Actions action=new Actions(driver);

action.moveToElement(driver.findElement(By.xpath("//*[@id=\"u1\"]/a[8]"))).perform();

Thread.sleep(2000);

driver.findElement(By.linkText("搜索設置")).click();

Thread.sleep(2000);

//此時有兩個頁面,一個是百度頁面,一個是搜索設置頁面

String handle_baidu=driver.getWindowHandle();

System.out.println("當前頁面的標題為:"+driver.getTitle());//打印目前頁面的標題

//如果當前的窗口句柄數不為1,則執行切換窗口

if (driver.getWindowHandles().size()!=1) {

//循環遍歷,找到登錄頁面的句柄,并切換到登錄頁面

for (String handles:driver.getWindowHandles())

{

if (handles.equals(handle_baidu)) {

continue;

}

driver.switchTo().window(handles);//切換到登錄頁面

}

System.out.println("切換之后頁面的標題為:"+driver.getTitle());//打印目前頁面的標題

}

//搜索框提示

if (driver.findElement(By.id("s1_2")).isSelected()==false) {

driver.findElement(By.id("s1_2")).click();

}

Thread.sleep(1000);

//搜索語言范圍

List radios=driver.findElements(By.name("SL"));

if (radios.get(1).isSelected()==false) {

radios.get(1).click();

}

Thread.sleep(1000);

//搜索結果顯示條數

Select select=new Select(driver.findElement(ById.cssSelector("select[name='NR']")));

select.selectByIndex(1);//選擇每頁顯示20條

Thread.sleep(2000);

//實時預測功能

Select select_yuce=new Select(driver.findElement(ById.cssSelector("select[name='issw1']")));

select_yuce.selectByVisibleText("關閉");//選擇關閉實時預測功能

Thread.sleep(1000);

//搜索歷史記錄

if (driver.findElement(By.id("sh_1")).isSelected()==false) {

driver.findElement(By.id("sh_1")).click();

}

Thread.sleep(1000);

//通欄瀏覽模式

if (driver.findElement(By.id("stb_2")).isSelected()==false) {

driver.findElement(By.id("stb_2")).click();

}

Thread.sleep(1000);

driver.findElement(By.xpath("//*[@id=\"gxszButton\"]/a[1]")).click();

Thread.sleep(2000);//停頓2秒

//處理彈出框

Alert alert=driver.switchTo().alert();

alert.accept();//. accept() 相當于點擊它的"確認";. dismiss() 相當于點擊"取消"或者叉掉對話框

Thread.sleep(3000);//停頓3秒

driver.quit();

}

}

總結

以上是生活随笔為你收集整理的用java自动化访问百度测试_java+eclipse+selenium+百度搜索设置自动化测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。