在selenuim中运行代码时遇到错误

时间:2018-06-18 19:38:15

标签: webdriver selenium-chromedriver assert

  public void f() throws IOException, InterruptedException {
     System.setProperty("webdriver.chrome.driver","D:\\Selenium\\chromedriver_win32\\chromedriver.exe"); 
     driver = new ChromeDriver();
     driver.get("http://toolsqa.com/selenium-webdriver/install-testng/");
     driver.manage().window().maximize();
     Thread.sleep(5000);
     List<WebElement> wb = driver.findElements(By.tagName("a"));
     ListIterator<WebElement> lt = wb.listIterator();
     System.out.println(wb.size());
     for(WebElement x:wb) {  
Assert.assertEquals(x.getText(), "HOME" );

x.click();


Error: java.lang.AssertionError: expected [HOME] but found []

虽然&#34; HOME存在,但我无法使用上面的代码点击该元素

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它将为您工作。

public void f() throws IOException, InterruptedException {  
    System.setProperty("webdriver.chrome.driver","D:\\Selenium\\chromedriver_win32\\chromedriver.exe"); 
    driver = new ChromeDriver();
    driver.get("http://toolsqa.com/selenium-webdriver/install-testng/");
    driver.manage().window().maximize();
    Thread.sleep(5000);
    List<WebElement> wb = driver.findElements(By.tagName("a"));
    ListIterator<WebElement> lt = wb.listIterator();
    System.out.println(wb.size());
    Thread.sleep(5000);
    for(WebElement x:wb) {  
        String linkName= x.getText();
        System.out.println(linkName);
        if(linkName.contains("HOME")) {
            Thread.sleep(5000);
            x.click();
            break;
        }else {
            System.out.println("Link does not contain HOME");
        }
    }