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