美文网首页
selenium---好例子

selenium---好例子

作者: 小胖5920 | 来源:发表于2019-07-04 09:33 被阅读0次

    public class chrometest {
    public static void main(String[] args) throws InterruptedException {

        String chromepath="path";
       
        System.setProperty("webdriver.chrome.driver", chromepath);
    
        //初始化一个chrome浏览器实例,实例名称叫driver 
        WebDriver driver = new ChromeDriver();
         
        //最大化窗口
        driver.manage().window().maximize();
         
        //get()打开一个站点 
        driver.get("https://music.163.com/");
         
        //切换到第二个tab-li[2]
        WebElement tagele1 = driver.findElement(By.xpath("//*[@id=\"g_nav2\"]/div/ul/li[2]/a"));
         
        tagele1.click();
        //切换iframe
        WebElement frame = driver.findElement(By.xpath("//*[@id=\"g_iframe\"]"));
         
        driver.switchTo().frame(frame);
         
        //点击播放按钮
        WebElement play_button = driver.findElement(By.xpath("//*[@id=\"toplist\"]/div[2]/div/div[1]/div/div[2]/div/div[3]/a[1]"));
         
        play_button.click();
         
        //打印当前url
        System.out.println("当前.....打开页面是: "+ driver.getCurrentUrl());
         
        //切换
        //----------------------
        driver.switchTo().defaultContent();
        //----------------------
         
        Thread.sleep(3000);
         
        WebElement tagele0 = driver.findElement(By.xpath("//*[@id=\"g_nav2\"]/div/ul/li[1]/a"));
         
        tagele0.click();
         
        System.out.println("当前.....打开页面是: "+ driver.getCurrentUrl());
    
        Thread.sleep(3000);
         
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//*[@class=\"link s-fc3\"]"))).perform();
        Thread.sleep(3000);
        WebElement ele2 = driver.findElement(By.xpath("//*[em=\"QQ登录\"]"));
        ele2.click();
        Thread.sleep(3000);
        //----------------------
        String title = "安全登录";
        String s = null;
        
        Set<String> handles = driver.getWindowHandles();
         
        for (String t : handles) {
         
            System.out.println("switchWindow: "+t);
            if (driver.switchTo().window(t).getTitle().contains(title)) {
                s = t;
            }
        }
        driver.switchTo().window(s);
        System.out.println("target_window_handler:"+s);
         
      
        WebElement qframe = driver.findElement(By.xpath("//*[@id=\"ptlogin_iframe\"]"));
         
        driver.switchTo().frame(qframe);
         
        //点击意见反馈
        WebElement qplay_button = driver.findElement(By.xpath("//*[@id=\"feedback_qlogin\"]"));
         
        qplay_button.click();
    
        Thread.sleep(8000);
        System.out.println("当前.....打开页面是: "+ driver.getCurrentUrl());
         
        driver.quit();
    }
    

    }

    相关文章

      网友评论

          本文标题:selenium---好例子

          本文链接:https://www.haomeiwen.com/subject/uqsyqctx.html