发布新日志

  • selenium webdriver登录163邮箱发送邮件的实例

    bob123654 发布于 2012-12-21 10:24:11

    package com.test;

    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.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.interactions.Actions;

    public class test163 {
        public static void main(String[] args) throws InterruptedException {
            // 启动浏览器,进入163邮箱首页
            //WebDriver driver = new InternetExplorerDriver();
            //WebDriver driver = new ChromeDriver();
            WebDriver driver = new FirefoxDriver();
            driver.get("http://mail.163.com/");
           
            Thread.sleep(5000);
            // 输入用户名密码,登录邮箱
            WebElement youxiangzhanghao_element = driver.findElement(By.id("idInput"));
            youxiangzhanghao_element.clear();
      
            //邮箱用户名           youxiangzhanghao_element.sendKeys("selenium");
     
    //邮箱密码      
            WebElement mima_element = driver.findElement(By.id("pwdInput"));
            mima_element.sendKeys("123654");
           
           
            WebElement denglu_element = driver.findElement(By.id("loginBtn"));
            denglu_element.click();
           
            Thread.sleep(10000);
            //  写信
            //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
            WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'写 信')]"));
            xiexin_element.click();
           
            WebElement shoujianren_element = driver.findElement(By.className("nui-editableAddr-ipt"));
            shoujianren_element.sendKeys("rob123654@163.com");
           
            WebElement zhuti_element = driver.findElement(By.id("objComposeSubject"));
            zhuti_element.sendKeys("test1");
           
            WebElement youjianneirong_element = driver.switchTo().frame(driver.findElement(By.className("APP-editor-iframe"))).findElement(By.className("nui-scroll"));
            youjianneirong_element.sendKeys("123456");
           
            driver.switchTo().defaultContent();
           
            // 发送邮件
            WebElement fasong_element = driver.findElement(By.xpath("//span[contains(.,'发 送')]"));
            fasong_element.click();
           
            Thread.sleep(5000);
           
            WebElement tuichu_element = driver.findElement(By.linkText("退出"));
            tuichu_element.click();
          
            driver.close();
        }

    }

  • selenium webdriver定位不到元素的五种原因及解决办法

    bob123654 发布于 2012-12-21 10:42:53

    1.动态id定位不到元素
    for example:
            //WebElement xiexin_element = driver.findElement(By.id("_mail_component_82_82"));
            WebElement xiexin_element = driver.findElement(By.xpath("//span[contains(.,'写 信')]"));
            xiexin_element.click();

       上面一段代码注释掉的部分为通过id定位element的,但是此id“_mail_component_82_82”后面的数字会随着你每次登陆而变化,此时就无法通过id准确定位到element。
       所以推荐使用xpath的相对路径方法查找到该元素。

    2.iframe原因定位不到元素

        由于需要定位的元素在某一个frame里边,所以有时通过单独的id/name/xpath还是定位不到此元素
    比如以下一段xml源文件:
    <iframe id="left_frame" scrolling="auto" frameborder="0" src="index.php?m=Index&a=Menu" name="left_frame" noresize="noresize" style="height: 100%;visibility: inherit; width: 100%;z-index: 1">

    4. xpath描述错误
    这个是因为在描述路径的时候没有按照xpath的规则来写 造成找不到元素的情况出现

    5.点击速度过快 页面没有加载出来就需要点击页面上的元素
    这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来实现
    例如:
           //用WebDriverWait和until实现显示等待 等待欢迎页的图片出现再进行其他操作
           WebDriverWait wait = (new WebDriverWait(driver,10));
           wait.until(new ExpectedCondition<Boolean>(){
               public Boolean apply(WebDriver d){
                   boolean loadcomplete = d.switchTo().frame("right_frame").findElement(By.xpath("//center/div[@class='welco']/img")).isDisplayed();
                   return loadcomplete;
               }
           });
    也可以自己预估时间通过Thread.sleep(5000);//等待5秒 这个是强制线程休息

    6.firefox安全性强,不允许跨域调用出现报错
    错误描述:uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location:

    解决办法:
    这是因为firefox安全性强,不允许跨域调用。
    Firefox 要取消XMLHttpRequest的跨域限制的话,第一
    是从 about:config 里设置 signed.applets.codebase_principal_support = true; (地址栏输入about:config 即可进行firefox设置)
    第二就是在open的代码函数前加入类似如下的代码: try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch (e) { alert("Permission UniversalBrowserRead denied."); }
Open Toolbar