selenium元素定位方法
2017-05-26 14:43
197 查看
一、如何找到页面元素
1.1By ID
那么可以这样找到页面的元素:
通过id查找:
WebElement element = driver.findElement(By.id("passwd-id"));
1.2 By Name
WebElement element = driver.findElement(By.name("passwd"));
1.3 By XPATH(右键--审查元素)
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
1.4 By Class Name
Cheddar
Gouda
可以通过这样查找页面元素:
Listcheeses = driver.findElements(By.className("cheese"));
1.5 By Link Text
假设页面元素写成这样:
cheese>
那么可以通过这样查找:
WebElement cheese =driver.findElement(By.linkText("cheese"));
二、如何对页面元素进行操作
2.1 输入框(text field or textarea)
WebElement element = driver.findElement(By.id("passwd-id"));
在输入框中输入内容:
element.sendKeys(“test”);
将输入框清空:
element.clear();
获取输入框的文本内容:
element.getText();
2.2 下拉选择框(Select)
Select select = new Select(driver.findElement(By.id("select")));
选择对应的选择项:
select.selectByVisibleText(“mediaAgencyA”);
或
select.selectByValue(“MA_ID_001”);
不选择对应的选择项:
select.deselectAll();
select.deselectByValue(“MA_ID_001”);
select.deselectByVisibleText(“mediaAgencyA”);
或者获取选择项的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();
2.3 单选项(Radio Button)
WebElement bookMode =driver.findElement(By.id("BookMode"));
选择某个单选项:
bookMode.click();
清空某个单选项:
bookMode.clear();
判断某个单选项是否已经被选择:
bookMode.isSelected();
2.4 多选项(checkbox)
WebElement checkbox =driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
2.5 按钮(button)
WebElement saveButton = driver.findElement(By.id("save"));
点击按钮:
saveButton.click();
判断按钮是否enable:
saveButton.isEnabled ();
2.6 左右选择框
Select lang = new Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(“English”);
WebElement addLanguage =driver.findElement(By.id("adon"));
addLanguage.click();
2.7 弹出对话框(Popup dialogs)
alert.accept();
alert.dismiss();
alert.getText();
2.8 表单(Form)
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只适合于表单的提交
2.9 上传文件 (Upload File)
WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
2.10 Windows 和 Frames之间的切换
driver.switchTo().defaultContent();
切换到某个frame:
driver.switchTo().frame("leftFrame");
从一个frame切换到另一个frame:
driver.switchTo().frame("mainFrame");
切换到某个window:
driver.switchTo().window("windowName");
2.11拖拉(Drag andDrop)
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
2.12导航 (Navigationand History)
打开一个新的页面:
driver.navigate().to("http://www.example.com");
通过历史导航返回原页面:
driver.navigate().forward();
driver.navigate().back();
三、RemoteWebDriver
3.1 使用RemoteWebDriver
import java.net.URL;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Testing {
public void myTest()throws Exception {
WebDriver driver = newRemoteWebDriver(
new URL("http://localhost:4446/wd/hub"),
DesiredCapabilities.firefox());
driver.get("http://www.google.com");
// RemoteWebDriverdoes not implement the TakesScreenshot class
// if the driver doeshave the Capabilities to take a screenshot
// then Augmenter willadd the TakesScreenshot methods to the instance
WebDriveraugmentedDriver = new Augmenter().augment(driver);
File screenshot =((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
}
}
3.2 SeleniumServer
java -jar selenium-server-standalone-2.20.0.jar -port 4446
3.3 How to setFirefox profile using RemoteWebDriver
profile.setPreference("general.useragent.override",testData.getUserAgent());
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("firefox_profile", profile);
driver = new RemoteWebDriver(new URL(“http://localhost:4446/wd/hub”),capabilities);
driverWait = new WebDriverWait(driver,TestConstant.WAIT_ELEMENT_TO_LOAD);
driver.get("http://www.google.com");
相关文章推荐
- Selenium(一)元素定位的方法
- 自动化测试工具selenium常见的元素定位方法
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
- selenium--unittest定位元素内容为空时模拟鼠标点击的方法
- Selenium2: web元素定位方法笔记
- Selenium2(WebDriver)总结(三)---元素定位方法
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
- selenium+python 元素定位方法
- selenium webdriver的八种元素定位方法
- selenium--unittest定位元素内容为空时模拟鼠标点击的方法
- selenium webdriver 元素定位方法
- Selenium Webdriver元素定位的八种常用方法
- Selenium常见元素定位方法和操作的学习介绍
- python+selenium 定位元素异常类型及解决方法
- Selenium + Webdriver 学习(四) 元素定位方法
- selenium python (二) 元素定位方法
- Python使用Selenium测试WebApp在定位元素和使用元素属性报的莫名其妙错误的终极解决方法
- Selenium Webdriver元素定位的八种常用方法
- Selenium Webdriver元素定位八中常用方法
- Selenium2自动化测试-一种低效但有效的web元素定位方法