selenium WebDriver简单使用

本文介绍了一个使用Selenium2进行Web自动化测试的示例代码,演示了如何使用Firefox浏览器打开网页、点击链接、填写表单等操作。

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

最近学习selenium 2 ,以下代码用于实现的基本的功能,其中navigation.to("http://www.baidu.com");在我练习的时候经常更换地址,完成一个小功能我就对代码进行注释,不能直接运行。

基于Eclipse Java EE IDE for Web Developers.
Version: Mars.2 Release (4.5.2)

package ST;


import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.*;

public class test2{
    public static void main(String[] args)
    {
        
        WebDriver driver = new FirefoxDriver();
        Navigation navigation = driver.navigate();
        navigation.to("http://www.baidu.com");
        /*navigation.to("http://www.google.com.hk");
        try{
            Thread.sleep(3000);
        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
        //页面后退
        navigation.back();
        
        try{
            Thread.sleep(3000);
        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
        //页面前进
        navigation.forward();
        //页面刷新
        navigation.refresh();*/
        //定位至百度文本框,在文本框输入find it
        /*WebElement baiduTextBox = driver.findElement(By.id("kw"));
        baiduTextBox.sendKeys("find it");*/
        //清除文本框中的find it
        /*try{
            Thread.sleep(3000);
        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
        baiduTextBox.clear();*/
        
        //精确查询
        WebElement baiduLogin = driver.findElement(By.linkText("登录"));
        baiduLogin.click();
        //模糊查询
        /*WebElement baiduLogin = driver.findElement(By.partialLinkText("录"));
        baiduLogin.click();*/
        
        //使用class定位
        /*try{
            Thread.sleep(3000);
        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
        WebElement tieba = driver.findElement(By.className("senior-search-link"));
        tieba.click();
        */
        //selenium 2中没有Check/Uncheck方法,用click事件来勾选或者取消选中
        /*WebElement remember = driver.findElement(By.id("TANGRAM__PSP_3__memberPass"));
        remember.click();*/
        //以百度贴吧->高级搜素->下拉框为例,选中按相关性排序
        /*WebElement select = driver.findElement(By.name("sm"));
        String targetText = "按相关性排序";
        java.util.List<WebElement> options = select.findElements(By.tagName("option"));
        for (int i = 0; i < options.size(); i++){
            if(options.get(i).getText().equals(targetText))
            {
                options.get(i).click();
            }
        }*/    
        //以百度的登录为例
        /*WebElement username = driver.findElement(By.id("TANGRAM__PSP_8__userName"));
        username.sendKeys(" your username");
        WebElement password = driver.findElement(By.id("TANGRAM__PSP_8__password"));
        password.sendKeys(" your password");
        //可以采取上诉的找到提交按钮单击登录,也可以对html文件中的form的任意元素进行提交也可以登录成功
        password.submit();*/
        
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值