0
package testPackage;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
public class AllLinkVerificationInAPage {
WebDriver driver;
@BeforeTest
public void OpenApp()
{
System.setProperty("webdriver.chrome.driver", "E:/Selenium/Webdriver /Softwares/chromedriver.exe");
driver = new ChromeDriver();
driver.navigate().to("http://ndtv.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement popUp = driver.findElement(By.xpath("//*[@id='__cricketsubscribe']/div[2]/div[2]/a[1]"));
popUp.click();
}
@Test
public void clickLinks() throws InterruptedException
{
//extract the list of WenElements and its count
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
int count = linkElements.size();
System.out.println("Total number of links = " + count);
//test each link
for(WebElement currentElement : linkElements)
{
String link = currentElement.getText();
System.out.println(link);
if(link !="")
{
currentElement.click();
System.out.println("Working Fine");
}
driver.navigate().back();
Thread.sleep(3000);
}
}
}
當我運行這段代碼我得到以下錯誤網頁上的所有鏈接: -無法點擊使用Java和硒的webdriver
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
我試圖隱等待爲好,但得到同樣的問題。
非常感謝Guy,它完美的工作。 – Deepak