Unable to get all links from the webpage - Selenium Unable to get all links from the webpage by using below mentioned code. Code below:
package config;
import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ActionKeywords {
// WebDriver driver = new FirefoxDriver();
WebDriver driver;
@BeforeTest
public void setup()
{
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.16.1-win64\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
}
@Test
public void openBrowser(){
driver.get("https://www.google.com/");
}
/*
@Test
public void verify_Menus(){
WebElement mainMenu = driver.findElement(By.xpath("//ul[@id='menu-main']/li/a"));
System.out.println(mainMenu.getText());
WebElement subMenu = driver.findElement(By.xpath("//a[contains(text(),'Impegno Per La Natura')]"));
Actions action = new Actions (driver);
action.moveToElement(mainMenu).perform();
System.out.println(subMenu.getText());
action.click(subMenu).perform();
} */
@Test
public void all_Links(){
try{
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println("Count of all links: " +allLinks.size());
//Loop
for (WebElement link : allLinks)
System.out.println(link.getText());
}catch (Exception e){
System.out.println("Element not found by tagName");
}
}
@AfterTest
public void close_Browser(){
driver.quit();
}
}
After run this program, result displayed as 'Count of all links: 0' Please advise!
Thanks, Sudhir
You will get all links using containing the attributes href / src like shown in following code:
@Test
public void alllinks()
{
System.setProperty("webdriver.chrome.driver", "D:/Selenium/Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
List<WebElement> list=driver.findElements(By.xpath("//*[@href or @src]"));
for(WebElement e : list){
String link = e.getAttribute("href");
if(null==link)
link=e.getAttribute("src");
System.out.println(e.getTagName() + "=" + link);
}
}
Hope this code will help you.
Thanks
You are using the correct code But executing in wrong order all_Links() method is executing first before openBrowser(). Please put the priority in your @test annotation because @test annotation run by default alphabetical order.
Hope this will helpful to you!!!
It would be better if you can change driver.get("https://www.google.com/");
from OpenBrowser()
to SetUp()
. OpenBrowser()
is not supposed to be a test and it can be interfering with the order of execution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.