百度搜索框输入值后自动弹出的下拉框的测试(可应用到很多场景)

本文探讨了如何测试百度搜索框在输入内容后自动弹出的相关数据下拉框,该功能展示了10条匹配数据。测试重点涉及输入值后的匹配准确性和下拉框展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

百度搜索框输入内容后,会匹配相关数据,弹出下拉框(共10条数据),看一下:


看一下如何测试:

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/*
 * 
 * 测试百度搜索框输入某个数据之后自动弹出的下拉列表
 * author:Kiven
 * 部分信息参考于网络
 */
public class BaiduTable {
	private WebDriver driver; 
	
	BaiduTable(WebDriver driver){
		this.driver = driver;
	}
	
	public static void main(String args[]) throws InterruptedException{
		WebDriver driver;
		String DriverPath = "D:\\软件测试\\软件测试资料\\selenium\\chromedriver.exe";
		System.setProperty("webdriver.chrome.driver",DriverPath);
		driver = new ChromeDriver();
		
		driver.get("http://www.baidu.com");
		
		//搜索框
		driver.findElement(By.id("kw")).sendKeys("test");
		//等待元素出现
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		BaiduTable table = new BaiduTable(driver);
		//table的ID
		By by = By.id("st");
		//String address = "2.0";	//取第三行第一列的值(该table只有一列)
		
		//取出全部10行的数据
		String address[] = {"0.0","1.0","2.0","3.0","4.0","5.0","6.0","7.0","8.0","9.0"};
		for(int i=0;i<address.length;i++){
			System.out.println(table.getCellText(by, address[i]));
		}
		driver.quit();
	}
	
	/** 从一个table的单元格中得到文本值. 参数tableCellAddress的格式为row.column, 行列从0开始. 
    @param by  用于得到table对象 
    @param tableCellAddress 一个单元格地址, 如. "1.4"  行 列
    @return 从一个table的单元格中得到文本值
    */
    public String getCellText(By by,String tableCellAddress){
        //得到table元素对象
        WebElement table = driver.findElement(by);
        
        //对所要查找的单元格位置字符串进行分解,得到其对应行、列。
        int index = tableCellAddress.trim().indexOf('.');
        int row =  Integer.parseInt(tableCellAddress.substring(0, index));
        int cell = Integer.parseInt(tableCellAddress.substring(index+1));
       
        //得到table表中所有行对象,并得到所要查询的行对象
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        WebElement theRow = rows.get(row);
        
        //调用getCell方法得到对应的列对象,然后得到要查询的文本。
        String text = getCell(theRow, cell).getText(); 
        return text;
    }
    
    private WebElement getCell(WebElement Row,int cell){
        List<WebElement> cells;
        WebElement target = null;
        //列里面可能有"<th>"、"<td>"两种标签,所以分开处理。
        if(Row.findElements(By.tagName("th")).size()>0){
           cells = Row.findElements(By.tagName("th"));
           target = cells.get(cell);
        }
        if(Row.findElements(By.tagName("td")).size()>0){
           cells = Row.findElements(By.tagName("td"));
           target = cells.get(cell);
        }
        return target;
    }
}

经验证,测试通过,可看一些我的截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值