selenium webdriver 定位元素 第一部分
static final WebDriver driver = ExplorerBase.IESetting(); // 实例化一个浏览器对象 @Test //@Ignore public void findBaiDuElements() { try { Thread.sleep(500); } catch (InterruptedException e1) { e1.printStackTrace(); } driver.manage().window().maximize();// 最大化 driver.get("http://www.baidu.com/"); WebElement element=driver.findElement(By.id("su"));//id String str=element.getAttribute("value"); System.out.println("**********id:"+str+"**********"); Assert.assertEquals("通过id查找元素失败", "百度一下",str ); element=driver.findElement(By.className("btn"));//class str=element.getAttribute("value"); System.out.println("**********class:"+str+"**********"); Assert.assertEquals("通过class查找元素失败", "百度一下",str ); element=driver.findElement(By.name("wd"));//name element.sendKeys("selenium test"); str=element.getAttribute("value"); System.out.println("**********name:"+str+"**********"); Assert.assertEquals("通过name查找元素失败", "selenium test",str ); element=driver.findElement(By.cssSelector("#su"));//css id选择器 str=element.getAttribute("value"); System.out.println("**********id选择器:"+str+"**********"); Assert.assertEquals("通过css查找元素失败", "百度一下",str ); element=driver.findElement(By.linkText("贴吧")); element.click(); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String title= driver.getTitle(); System.out.println(title); }