zoukankan      html  css  js  c++  java
  • 判断是否可以点击

    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());
            }
        }
    
    }
    
    
    


  • 相关阅读:
    python INFO: Can't locate Tcl/Tk libs and/or headers
    关于 android 中 postDelayed方法的讲解
    python两个dataframe的合并
    Android异步加载访问网络图片-解析json
    ThinkCMF----调用指定栏目的文章列表
    thinkCMF----使用自定义函数
    thinkCMF----公共模板的引入
    thinkCMF----如何写标签
    thinkCMF----列表页跳转
    thinkCMF----导航高亮显示
  • 原文地址:https://www.cnblogs.com/cmm2016/p/5595899.html
Copyright © 2011-2022 走看看