【Selenium】15执行JS,判断元素点击,添加删除元素

JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("return document.tittle");
js.executeScript("var button=document.getElementById('stb');return button.value");
判断元素存在,可点击
 
  
 
  
@Test
    public void JavaScripClick(WebElement webElement){
        //元素是否处于可点击状态以及是否显示在页面上
    try {
        if(webElement.isEnabled()&&webElement.isDisplayed()){
            //执行js语句
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].click;",webElement);
        }else {
            System.out.println("页面上的元素无法进行单机操作");
        }
        //不可点击异常
    } catch (StaleElementReferenceException e) {
        System.out.println("页面元素没有附加在网页中");
                e.printStackTrace();
        
    }catch (NoSuchElementException e){
        System.out.println("无法完成单击操作");
        e.getStackTrace();
    }
}
改变元素属性
 
  
@Test
    public void testdataPicker(){
    WebElement element = driver.findElement(By.xpath(""));
    //修改value
    setAttribute(driver,element,"value","修改了");
    //修改size
    setAttribute(driver,element,"size","10");
    //删除size属性
    removeAttribute(driver,element,"size");

}
//增加页面元素,修改页面元素
    public void setAttribute(WebDriver driver,WebElement element,String attributeName,String value){
        JavascriptExecutor js= (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute(argument[1],argument[2])",element,attributeName,value);
    }
    //删除页面元素
    public void removeAttribute(WebDriver driver,WebElement element,String attributeName){
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("argument[0].removeAttribute(argumentp[1]),argument[2]",element,attributeName);
    }


你可能感兴趣的:(selenium)