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 []
虽然"HOME 存在,但我无法使用上面的代码单击该元素
尝试以下代码。它会为你工作。
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");
}
}