selenium常用命令之操作頁面元素及獲取元素內容的事件整理


/**id <input type="text" id="phone" name="phone" class="LoginText" placeholder="手機號" >          * <button class="LoginBtn" id="btnLogin" value="baidu"> 登錄</button>*/

        WebElement byName=driver.findElement(By.name("phone"));
        WebElement byLoginButton=driver.findElement(By.id("btnLogin"));
        System.out.println(byName.getText());
        
        1、click()事件源於單擊元素操作
        byLoginButton.click();
        
        2、sendKeys()方法用於給input元素賦值
        byName.sendKeys("13600000000");
        
        3、clear()用於清空input元素的值
        byName.clear();
        
        4、Submit()用於提交表單
        byLoginButton.submit();

        5、getTitle()獲取當前網頁的標題
        String title=driver.getTitle();
        
        6、getCurrentUrl()獲取當前網頁的URL
        String url=driver.getCurrentUrl();
        
        7、getText()用於存儲元素的文本值,例如純文本、超鏈接等;
        String text=byName.getText();
        
        8、isSelected()用於存儲復選框或單選框的勾選情況,返回true(勾選)或false(未勾選)
        /**<input id="TANGRAM__PSP_8__memberPass" type="checkbox" name="memberPass" class="pass-checkbox-input pass-checkbox-memberPass" checked="checked">*/
        WebElement checkBox=driver.findElement(By.id("TANGRAM__PSP_8__memberPass"));
        boolean isSelected=checkBox.isSelected();
        
        9、getTagName()獲取元素的標記名稱
        String tagName=byName.getTagName();
        
        10、isEnabled()用於存儲input等元素的可編輯狀態,例如:文本框、復選框、單選框;返回true(可編輯)或false(不可編輯)
        boolean enabled=checkBox.isEnabled();
        
        11、getAttribute()用於獲取指定屬性的值
        String btnValue=byLoginButton.getAttribute("value");
        
        12、窗口最大化
        driver.manage().window().maximize(); 
        
        13、accept()方法是單擊彈出的對話框的確認按鈕,        driver.switchTo().alert().accept();
        
        14、dismiss()方法實現單擊彈出對話框的取消按鈕;
        driver.switchTo().alert().dismiss();
        
        15、getText()獲取彈出對話框的文本內容
        driver.switchTo().alert().getText();
        
        16、獲取當前cookie的集合
        Set<Cookie> cookie=driver.manage().getCookies();
        
        17、refresh()頁面刷新

        driver.navigate().refresh();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM