您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium如何高亮某元素和操作隱藏的內(nèi)容
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/4/25 13:26:07 ] 推薦標(biāo)簽:功能測(cè)試 軟件測(cè)試工具

  高亮元素的思路是:
  1.找到要高亮的元素
  2.對(duì)該元素執(zhí)行js,更改style達(dá)到高亮效果。
  操作隱藏的內(nèi)容思路:
  1.可以用Actions的moveToElement,使鼠標(biāo)懸停在觸發(fā)隱藏內(nèi)容的元素上。
  2.也可以用js點(diǎn)擊觸發(fā)隱藏內(nèi)容的元素
  下面是具體的代碼:
package com.test;
import java.sql.Driver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import bsh.commands.dir;
public class TestHighLight {
static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.zhihu.com/#signin");
driver.findElement(By.name("account")).sendKeys("XXX@163.com");
driver.findElement(By.name("password")).sendKeys("XXX");
WebElement login = driver.findElement(By.xpath("//*//button[text()='登錄']"));
highl(driver, login);
login.click();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver dr) {
// TODO Auto-generated method stub
return dr.findElement(By.xpath("//*//span[text()='喬葉葉']"));
}});
/*
* 使用js實(shí)現(xiàn)點(diǎn)擊目標(biāo),出現(xiàn)隱藏元素
String js = "document.getElementsByClassName("name")[0].click()";
((JavascriptExecutor)driver).executeScript(js);
*/
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebElement sixinElement = driver.findElement(By.id(":2"));//*[@id=":2"]
highl(driver, sixinElement);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sixinElement.click();
driver.quit();
}
static void highl(WebDriver dr, WebElement we) {
((JavascriptExecutor)dr).executeScript("arguments[0].style.border="5px solid red"", we);
}
}
  可以看下效果,把登錄按鈕高亮顯示,因?yàn)橹跫虞d主頁(yè)太慢,后面的操作隱藏元素的效果沒(méi)有截圖。


 

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd