WebDriver API (四)模拟百度首页搜索

  1. 在输入框中清除原有的文字内容,并输入指定内容
    WebElement input = driver.findElement(By.id("xxx"));  //定位到的元素,id为xxx
    input.clear();
    
    String inputString = "Selenium";
    input.sendKeys(inputString);
    
  2. 单击按钮
    WebElement button = driver.findElement(By.id("xxx")); 
    button.click();
    
  3. 双击某个元素
    //定位到页面的输入框元素
    WebElement inputBox = driver.findElement(By.id("xxx"));
    //声明Action对象
    Actions builder = new Action(driver);
    //使用doubleClick方法在输入框元素中进行鼠标的双击操作
    builder.doubleClick(inputBox).bulid().perform();
    

完整代码:

        import org.openqa.selenium.By;
		import org.openqa.selenium.WebDriver;
		import org.openqa.selenium.WebElement;
		import org.openqa.selenium.chrome.ChromeDriver;
		import org.testng.annotations.AfterMethod;
		import org.testng.annotations.BeforeMethod;
		import org.testng.annotations.Test;
		
		public class VisitBaidu {
			WebDriver driver;
			String baseUrl = "https://www.baidu.com/";
		
			@BeforeMethod
			public void setUp() throws Exception {
				System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
				driver = new ChromeDriver();
			}
		
			@Test
			public void baidu() throws Exception {
				driver.get(baseUrl);
				Thread.sleep(1000);
		
				// 通过id值kw定位到输入框元素
				WebElement inputBox = driver.findElement(By.id("kw"));
		
				// 在输入框中输入指定内容
				inputBox.sendKeys("乘风破浪的姐姐");
				Thread.sleep(1000);
		
				// 通过id值su定位到"百度一下"按钮
				WebElement baiduButton = driver.findElement(By.id("su"));
		
				// 单击"百度一下"按钮
				baiduButton.click();
				Thread.sleep(1000);
				
				//双击输入框中的内容
		        Actions builder = new Actions(driver);
		        builder.doubleClick(inputBox).build().perform();
		        Thread.sleep(1000);

		
				// 清空输入框的内容
				inputBox.clear();
				Thread.sleep(1000);
		
			}
		
			@AfterMethod
			public void tearDown() throws Exception {
				driver.quit();
			}
		}
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer@123

不积跬步无以至千里,感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值