1、封装一个javascript语句点击元素操作方法(用于解决click()方法无法点击的元素)
package china; import org.testng.annotations.Test; import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor; import org.testng.annotations.BeforeMethod; import java.util.NoSuchElementException; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; public class JavaWebDriver { public WebDriver driver; String baseUrl; JavaScriptExecutor js; @Test public void elementClick() throws Exception { WebElement inputBox=driver.findElement(By.id("query")); WebElement inputButton=driver.findElement(By.id("stb")); inputBox.sendKeys("使用javaScript语句进行页面元素点击操作"); javaScriptClick(inputButton); } public void javaScriptClick(WebElement element)throws Exception{ try{if(element.isEnabled()&&element.isDisplayed()){ System.out.println("使用javascript进行页面元素点击"); ((JavascriptExecutor) driver).executeScript("arguments[0].click();",element); } else{ System.out.println("页面上的元素无法进行点击操作"); } }catch(StaleElementReferenceException e){ System.out.println("页面元素没有附加在网页中"); }catch(NoSuchElementException e){ System.out.println("在页面中没有找到要操作的元素"); }catch(Exception e){ System.out.println("无法完成单机动作"+e.getStackTrace()); } } @BeforeMethod public void beforeMethod() { baseUrl="http://www.sogou.com"; System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); driver.get(baseUrl); } @AfterMethod public void afterMethod() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); // TODO: handle exception } driver.quit(); } }
2、在Ajax方式产生的浮动框中,单机选择包含某个关键字的选项
package china; import org.testng.annotations.Test; import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor; import org.testng.annotations.BeforeMethod; import java.util.List; 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; public class Ajax { public WebDriver driver; String baseUrl; JavaScriptExecutor js; @Test public void f() { WebElement inputBox=driver.findElement(By.id("query")); inputBox.click(); // List<WebElement>suggetionOptions=driver.findElements(By.xpath("//*[@id='vl']/div/ul/li")); // for(WebElement option: suggetionOptions){ // if(option.getText().contains("火星上建迪拜塔")){ // System.out.println(option.getText()); // option.click(); // break; // } // } WebElement suggetionOption=driver.findElement(By.xpath("//*[@id='vl']/div/ul/li[3]")); System.out.println(suggetionOption.getText()); suggetionOption.click(); } @BeforeMethod public void beforeMethod() { baseUrl="http://www.sogou.com"; System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); driver.navigate().to(baseUrl); driver.manage().window().maximize(); driver.navigate().refresh(); } @AfterMethod public void afterMethod() { try{ Thread.sleep(5000); }catch(InterruptedException e){ System.out.println("多余"+e.getStackTrace()); } driver.quit(); } }
3、设置一个页面对象的属性值
package china; import org.testng.annotations.Test; //import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor; import org.testng.annotations.BeforeMethod; import java.io.File; 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.testng.annotations.AfterMethod; public class TestDemo { public WebDriver driver; JavascriptExecutor js; @Test public void f() { WebElement inputBox=driver.findElement(By.id("text")); setAttribute( driver,inputBox,"size", "50"); removeAttribute(driver, inputBox,"size"); } //增加或者修改页面元素的方法 public void setAttribute(WebDriver driver,WebElement element,String attributeName,String value){ JavascriptExecutor js=(JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element,attributeName,value); } //删除页面元素方法 public void removeAttribute(WebDriver driver,WebElement element,String attributeName){ js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])", element,attributeName); } @BeforeMethod public void beforeMethod() { File file =new File("D:\\workspace\\webdriver Api\\src\\size.html"); String filePath=file.getAbsolutePath(); System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); driver.get(filePath); } @AfterMethod public void afterMethod() { try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } driver.quit(); } }
4、日期选择器上进行日期选择
package china; import org.testng.annotations.Test; import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor; import org.testng.annotations.BeforeMethod; 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; public class date { public WebDriver driver; String baseUrl; JavaScriptExecutor js; @Test public void f() { //思路:在日期文本框中直接输入日期,可以变相模拟日期控件上进行选择 WebElement dataInputBox=driver.findElement(By.id("datepicker")); dataInputBox.sendKeys("2/19/2017"); } @BeforeMethod public void beforeMethod() { baseUrl="http://jqueryui.com/resources/demos/datepicker/other-months.html"; System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); driver.navigate().to(baseUrl); } @AfterMethod public void afterMethod() { try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } driver.quit(); } }