selenium 通过id,name,class,css,xpath,tag,linktext,partial link 定位web 页面元素
文本框交互:可以通过SendKeys 方法把值转换成文本框内
单选按钮:可以使用click 方法选中按钮
复选框交互:可以使用click 方法选中按钮
下拉框交互:可以使用"selectByVisibleText"或"selectByIndex"或"selectByValue"的方法选择一个选项
拖放:
将Disabled Node 拖到Parent Node 下面
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;
public class webdriverdemo
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
//Puts a Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.navigate().to("http://www.keenthemes.com/preview/metronic/templates/admin/ui_tree.htmll");
driver.manage().window().maximize();
WebElement From = driver.findElement(By.xpath(".//*[@id='j3_7']/a"));
WebElement To = driver.findElement(By.xpath(".//*[@id='j3_1']/a"));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From)
.moveToElement(To)
.release(To)
.build();
dragAndDrop.perform();
driver.close();
}
}
键盘操作:有时需要一些组合键例如:按Ctrl键或Shift键
sendkeys:发送键,在浏览器的键盘表示。特殊键都没有文字,表示按键都未字符,或单独序列的一部分。
pressKey:按键盘上不是文字的按键
releaseKey:执行按键事件后松开键盘的一个键。
鼠标操作:
有时会执行一些复杂的鼠标事件,如双击,右键单击,将鼠标悬停在等下面的一些操作:
扫描二维码关注公众号,回复:
4140120 查看本文章
Click-进行点击,
contentClick:执行上下文点击/右键单击一个元素或基于坐标
doubleClick:执行双击元素或基于坐标
mouseDown:执行一个元素上按下鼠标操作或基于坐标
mouseMove:执行元素上的鼠标移动或基于坐标
mouseUp:释放鼠标
多选择操作:
有时我们会在一个情况来选择列表框文本域或多个项目,我们要从下列表中选择3个项目:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;
public class webdriverdemo
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to("http://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx");
//driver.manage().window().maximize();
driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();
driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();
Thread.sleep(5000);
// Perform Multiple Select
Actions builder = new Actions(driver);
WebElement select = driver.findElement(By.id("ContentHolder_lbFeatures_LBT"));
List<WebElement> options = select.findElements(By.tagName("td"));
System.out.println(options.size());
Action multipleSelect = builder.keyDown(Keys.CONTROL)
.click(options.get(2))
.click(options.get(4))
.click(options.get(6))
.build();
multipleSelect.perform();
driver.close();
}
}