0

Trying to print table element sequentially in console. Code given below please help:

    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import io.github.bonigarcia.wdm.WebDriverManager;
    
    public class Softwaretestingo {
    public static void main(String[] args) throws Exception {
    
        //Webdrivermanager to open chromebrowser
    
                WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.get("https://softwaretestingo.blogspot.com/2020/09/static-table.html");
        driver.findElements(By.xpath("//Table"));
        List<WebElement> rows=driver.findElements(By.xpath("//Table//tr"));
        System.out.println(rows.size());
        List<WebElement> cols=driver.findElements(By.xpath("//Table//th"));
        System.out.println(cols.size());
        List<WebElement> rd = driver.findElements(By.xpath("//Table//td"));
        System.out.println(rd.size());
        
                //itrate throgh the table elements 
    
                for(WebElement e : cols)
        {
            System.out.println(e.getText());
        }
        for(int i = 1 ; i <= cols.size() ; i++)
        {
            List<WebElement> thiteration = driver.findElements(By.xpath("//Table//th"));
            for(WebElement e2 : thiteration)
            {
                System.out.println(e2.getText());
            }
            List<WebElement> tditeration = driver.findElements(By.xpath("//Table//td"));
            for(WebElement e1 : tditeration)
            {
                System.out.println(e1.getText());
            }
            Thread.sleep(3000);
           }
         }
       }

Output as per the above code:

http://localhost:19529 for channel [id: 0xb0a056df, L:/127.0.0.1:64075 - R:localhost/127.0.0.1:19529]
Learn Selenium
19:06:07.566 [Forwarding getElementText on session b8964f2247db71d7a0f851f3441d3342 to remote] DEBUG org.asynchttpclient.netty.request.NettyRequestSender - Using pooled Channel '[id: 0xb0a056df, L:/127.0.0.1:64075 - R:localhost/127.0.0.1:19529]' for 'GET' to 'http://localhost:19529/session/b8964f2247db71d7a0f851f3441d3342/element/8ED067903D940894D08DF7C9EE6DC3EC_element_14/text'
19:06:07.566 [Forwarding getElementText on session b8964f2247db71d7a0f851f3441d3342 to remote] DEBUG org.asynchttpclient.netty.request.NettyRequestSender - Using open Channel [id: 0xb0a056df, L:/127.0.0.1:64075 - R:localhost/127.0.0.1:19529] for GET '/session/b8964f2247db71d7a0f851f3441d3342/element/8ED067903D940894D08DF7C9EE6DC3EC_element_14/text'
19:06:07.585 [AsyncHttpClient-1-2] DEBUG org.asynchttpclient.netty.handler.HttpHandler - 

Request DefaultFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: EmptyByteBufBE)
GET /session/b8964f2247db71d7a0f851f3441d3342/element/8ED067903D940894D08DF7C9EE6DC3EC_element_14/text HTTP/1.1
User-Agent: selenium/4.9.1 (java windows)
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
host: localhost:19529
accept: */*

Response DefaultHttpResponse(decodeResult: success, version: HTTP/1.1)
HTTP/1.1 200 OK
Content-Length: 16
Content-Type: application/json; charset=utf-8
cache-control: no-cache

19:06:07.586 [AsyncHttpClient-1-2] DEBUG org.asynchttpclient.netty.channel.ChannelManager - Adding key: http://localhost:19529 for channel [id: 0xb0a056df, L:/127.0.0.1:64075 - R:localhost/127.0.0.1:19529]
Amit
Shawn
  • 4,064
  • 2
  • 11
  • 23

2 Answers2

0

Refer the simplified code below:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public static void main(String[] args) throws InterruptedException {

        WebDriver driver = new ChromeDriver();
        driver.get("https://softwaretestingo.blogspot.com/2020/09/static-table.html");

        // Get all the table rows from the table
        List<WebElement> allRows = driver.findElements(By.xpath("//table[@name='BookTable']//tr"));

        // And iterate over them and get all the cells
        for (WebElement row : allRows) {
            List<WebElement> cells = row.findElements(By.tagName("td"));

            // Print the contents of each cell
            for (WebElement cell : cells) {
                System.out.println(cell.getText());
            }
        }
    }

Console output:

Learn Selenium
Amit
Selenium
300
Learn Java
Mukesh
Java
500
Learn JS
Animesh
Javascript
300
Master In Selenium
Mukesh
Selenium
3000
Master In Java
Amod
JAVA
2000
Master In JS
Amit
Javascript
1000
Shawn
  • 4,064
  • 2
  • 11
  • 23
0

You need to make 2 significant adjustments as follows:

  • You need to upgrade Selenium version from 4.9.1 to 4.10.0 so you won't explicitly need WebDriverManager as Selenium Manager being fully integrated is invoked transparently by the Selenium bindings when no browser driver is detected on the PATH or no third party driver manager is being used.

  • You can use Stream interface to achieve your task easily as follows:

  • Code block:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://softwaretestingo.blogspot.com/2020/09/static-table.html");
    System.out.println(driver.findElements(By.cssSelector("table[name=BookTable] td")).stream().map(element->element.getText()).collect(Collectors.toList()));
    
  • Console Output:

    [Learn Selenium, Amit, Selenium, 300, Learn Java, Mukesh, Java, 500, Learn JS, Animesh, Javascript, 300, Master In Selenium, Mukesh, Selenium, 3000, Master In Java, Amod, JAVA, 2000, Master In JS, Amit, Javascript, 1000]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352