查找页面中的文字元素

package com.test.utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Search
{
 public static boolean isContentAppeared(WebDriver driver, String content){
  boolean status = false;
  try{
  
   driver.findElement(By.xpath("//*[contains(.,'" + content + "')]"));
   System.out.println(content+ "已找到");
   status = true;
  }catch (Exception e){
   System.out.println(content+ "没有找到");
  }
  return status;
 }
}

 @BeforeClass
 public void beforeClass() {
   System.setProperty("webdriver.ie.driver", "D:\\WebDriver\\IEDriverServer.exe");
   DesiredCapabilities ieCapability = new DesiredCapabilities();
   ieCapability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
   driver = new InternetExplorerDriver(ieCapability);
   //driver.get("http://localhost:8080/day04/demo.html");
   driver.get("http://demo.opensourcecms.com/wordpress/wp-login.php");
 }

 public void login() throws InterruptedException{
  driver.findElement(By.id("user_login")).sendKeys("admin");
  driver.findElement(By.id("user_pass")).sendKeys("demo123");
  driver.findElement(By.id("wp-submit")).click();
  Thread.sleep(3000);
  //WebElement element = driver.findElement(By.xpath("//ul[@id='adminmenu']/li[3]/div[3]/div/ul/li[2]/a[.='Add New']"));
  WebElement element = driver.findElement(By.xpath("//ul[@id='adminmenu']/li[3]/div[3]/div/ul/li[2]/a"));
  //WebElement element = driver.findElement(By.xpath("//ul[@id='adminmenu']/li[3]/div[3]/div/ul/a[@contains(text(),'Add New')]"));
  Thread.sleep(3000);
  Assert.assertTrue(Search.isContentAppeared(driver, "Add New Post"));
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值