package cn.com.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.chrome.ChromeDriver;
public class findElement {
public static void main(String[] args) {
ChromeDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.findElementByXPath("//input[@id='kw' and @name='wd']").sendKeys("测试派");
driver.findElementByCssSelector("#su").click();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
driver.findElementByXPath("//em[text()=\"测试派\"]/parent::a").click();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Set<String> winHandels=driver.getWindowHandles();// 得到当前窗口的set集合
List<String> it = new ArrayList<String>(winHandels); // 将set集合存入list对象
driver.switchTo().window(it.get(1));// 切换到弹出的新窗口
driver.findElementById("navLogin").click();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 登录
driver.findElementById("nameOrEmail").sendKeys("输入账号");
driver.findElementById("loginPassword").sendKeys("输入密码");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 登录
driver.findElementById("loginBtn").click();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 点击 ‘领域’标签 /html/body/header/nav/a[2]
driver.findElementByXPath("/html/body/header/nav/a[2]").click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.quit();
}
}
package com.demo;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class demo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
driver.findElementById("kw").sendKeys("测试派");
driver.findElementById("su").click();
Thread.sleep(1000);
driver.findElementByPartialLinkText("软件测试工程师的心灵社区").click();
Thread.sleep(1000);
String search_handle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handle.equals(search_handle)) {
driver.switchTo().window(handle);
Thread.sleep(1000);
driver.findElementById("navLogin").click();
Thread.sleep(1000);
driver.findElementById("nameOrEmail").sendKeys("ke2beck");
driver.findElementById("loginPassword").sendKeys("********");
driver.findElementById("loginBtn").click();
Thread.sleep(1000);
driver.findElementByXPath("/html/body/header/nav/a[2]").click();
Thread.sleep(5000);
driver.close();
}
}
driver.quit();
}
}
```
```
#coding:utf-8
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.baidu.com')
browser.implicitly_wait(5)
browser.find_element_by_xpath('//input[@id="kw"]').send_keys(u'测试派')
browser.find_element_by_xpath('//input[@id="su"]').click()
browser.find_element_by_xpath('//*[@id="3"]/h3/a').click()
handles = browser.window_handles
browser.switch_to.window(handles[1])
browser.find_element_by_xpath('//a[text()="领域"]').click()
sleep(5)
browser.quit()
欢迎来到testingpai.com!
注册 关于