0

I am trying to extract only all the links title of a website and I did the following code. But getting only empty string.

String url = "https://www.linkedin.com/learning/learning-azure-sql-querying-18952522?trk=learning-serp_learning-search-card_search-card&upsellOrderOrigin=default_guest_learning";
List<String> titles = new ArrayList<String>();
String tempTitle = "";

WebDriverManager.chromedriver().setup(); // Chrome
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));


List<WebElement> elements = driver.findElements(By.className("table-of-contents__item-title"));
System.out.println(elements.size());

for (WebElement element : elements) {
    String title = element.getText().trim();
    titles.add(title);  
}

System.out.println(titles);

If i run tha program getting the below output:

27
[, , , , , , , , , , , , , , , , , , , , , , , , , , ]

But i am trying to get only the titles from the page. Anybody can point out what I am missing here?

2 Answers2

0

Using your code, I just needed to replace element.getText() with element.getAttribute("innerHTML") instead and it worked.

0

To print the element texts ideally you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following solution:

  • Code block:

    List<String> titles = new ArrayList<String>();
    List<WebElement> elements = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className("table-of-contents__item-title")));
    for(WebElement element : elements)
    {
      String title = element.getText().trim();
      titles.add(title);
    }
    System.out.println(titles);
    

Using Java8's stream() and map() you can use the following solution:

  • Line of code:

    System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className("table-of-contents__item-title"))).stream().map(element->element.getText()).collect(Collectors.toList()));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352