How to extract all the links in selenium?
package wdProgram;
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.BeforeTest;
import org.testng.annotations.Test;
public class ExtractAllLinks {
WebDriver driver;
@BeforeTest
public void openBrowser() {
System.setProperty("webdriver.chrome.driver",
"D:\\Software\\Testing-Tools\\Selenium\\WebDriver\\Drivers\\IEChromeFirefox\\19092018\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testExtractAllLinks() {
driver.get("http://localhost/uth/gadgetsgallery/catalog");
WebElement catBox = driver.findElement(By.xpath("//*[@id=\"left_sidebar\"]/div[1]/div[2]"));
List<WebElement> allLinks = catBox.findElements(By.tagName("a"));
System.out.println(allLinks.size());
for(int count=0;count<allLinks.size();count++) {
System.out.println(allLinks.get(count).getText());
allLinks.get(count).click();
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
catBox = driver.findElement(By.xpath("//*[@id=\"left_sidebar\"]/div[1]/div[2]"));
allLinks = catBox.findElements(By.tagName("a"));
}
}
}
Comments
Post a Comment