直播平台开发,WebDriver API模拟首页搜索

发布时间 2023-09-05 14:14:09作者: 云豹科技-苏凌霄

直播平台开发,WebDriver API模拟首页搜索

在输入框中清除原有的文字内容,并输入指定内容

 

WebElement input = driver.findElement(By.id("xxx"));  //定位到的元素,id为xxx
input.clear();
String inputString = "Selenium";
input.sendKeys(inputString);
 

单击按钮

 

WebElement button = driver.findElement(By.id("xxx")); 
button.click();
 

 

双击某个元素

 

//定位到页面的输入框元素
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();
}
}

 

以上就是直播平台开发,WebDriver API模拟首页搜索, 更多内容欢迎关注之后的文章