判断是否可以点击

package PublicModel;

import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import Page.element;
import org.openqa.selenium.JavascriptExecutor;
public class LoginPub {
// ok
    public static void okclick(WebDriver driver) {
        if (element.isExistWebElement(driver, "//*[@id='okId']/span")) {
            element.ok_Button(driver).click();
        }
        // try {
        // element.ok_Button(driver).click();
        // } catch (Exception e) {
        // // TODO Auto-generated catch block
        // //e.printStackTrace();
        // }
    }
}




判断某个元素是否处于可单击状态并处于显示状态
package test;

import java.sql.Driver;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

public class TestDemo {
    WebDriver driver;
    String baseUrl;
    JavascriptExecutor js;

    @BeforeMethod
    public void beforeMethod() {
        baseUrl = "http://www.baidu.com";
        System.setProperty("webdriver.chrome.driver", "H:\\Program Files\\Java\\chromedriver\\chromedriver_x64.exe");
        driver = new ChromeDriver();
        driver.get(baseUrl);
    }

    @AfterMethod
    public void afterMethod() {
        // driver.quit();
    }

    @Test
    public void testHandleiFrame() throws Exception {
        WebElement searchBox = driver.findElement(By.id("kw"));
        WebElement searchBtn = driver.findElement(By.id("su"));
        searchBox.sendKeys("javaScript进行单击");
        JavaScriptClick(searchBtn);
    }

    public void JavaScriptClick(WebElement element) throws Exception {
        try {
            if (element.isEnabled() && element.isDisplayed()) {
                System.out.println("使用JavaScript单击");
                ((JavascriptExecutor) driver).executeScript("arguments[0].clik();", element);
            } else {
                System.out.println("页面元素无法单击");
            }
        } catch (StaleElementReferenceException e) {
            // TODO: handle exception
            System.out.println("页面元素没有在网页中" + e.getStackTrace());
        } catch (NoSuchElementException e) {
            // TODO: handle exception
            System.out.println("页面中没有找到要操作的元素" + e.getStackTrace());

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("无法完成单击操作" + e.getStackTrace());
        }
    }

}

 



 

posted @ 2016-06-18 11:20  好奇的小明  阅读(416)  评论(0编辑  收藏  举报