selenium元素定位方法

一、如何找到页面元素

Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。

1.1By ID

假设页面写成这样:input type="text" name="passwd"id="passwd-id"

那么可以这样找到页面的元素:

通过id查找:

WebElement element = driver.findElement(By.id("passwd-id"));

1.2 By Name

或通过name查找:

WebElement element = driver.findElement(By.name("passwd"));

1.3 By XPATH(右键--审查元素)

或通过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 alert = driver.switchTo().alert();

alert.accept();

alert.dismiss();

alert.getText();

2.8 表单(Form)

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 element =driver.findElement(By.name("source"));

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

当本机上没有浏览器,需要远程调用浏览器进行自动化测试时,需要用到RemoteWebDirver.

3.1 使用RemoteWebDriver

import java.io.File;

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

在使用RemoteDriver时,必须在远程服务器启动一个SeleniumServer:

java -jar selenium-server-standalone-2.20.0.jar -port 4446

3.3 How to setFirefox profile using RemoteWebDriver

profile = new FirefoxProfile();

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");

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值