public static void loginYouxuePai() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\Program Files\\chromedriver.exe");
ChromeDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com");
WebElement kw = webDriver.findElement(By.id("kw"));
kw.sendKeys("测试派");
WebElement su = webDriver.findElement(By.id("su"));
String windowHandle = webDriver.getWindowHandle();
su.click();
Thread.sleep(3000);
WebElement element = webDriver.findElement(By.xpath("//em[text()=\"测试派\"]/parent::*"));
element.click();
Thread.sleep(3000);
webDriver.switchTo().defaultContent();
webDriver.manage().window().maximize();
System.out.println("页面跳转成功");
Set<String> windowHandles = webDriver.getWindowHandles();
for (String s : windowHandles){
Thread.sleep(1000);
if (webDriver.switchTo().window(s).getTitle().equals("测试派 - 软件测试工程师的心灵社区")){
windowHandle = s;
break;
}
}
webDriver.switchTo().window(windowHandle);
Thread.sleep(1000);
//element = webDriver.findElement(By.cssSelector(".fn__two-line"));
element = webDriver.findElement(By.xpath("//a[@href=\"http://testingpai.com/article/1625637760328/comment/1625650976958#comments\"]/div"));
if (element != null){
System.out.printf("找到元素了");
}
element.click();
}
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();
}
}
欢迎来到testingpai.com!
注册 关于