1

I had to start using Selenium recently as a website that I crawled had updated to javascript, after seeking some advise here Selenium was recommended as the program of choice in this situation.

Using Selenium I am now able to crawl the website and "roughly" get what I want but I would like some guidance on how to select the different elements that I gathered when I crawled the Table. For example, When I use J-Soup to gather the data I get the Whole table like so:

docVTS = Jsoup.connect("http://********************").timeout(10000).get(); 
                Elements table = docVTS.select("table.dynlist");

Then I can gather the different parts of that table like this:

                Elements number = table.select("td:eq(0)");
                  vtsInt = number.size();
                    for (int i = 0; i < vtsInt; i++) {

                   ships = table.select("td:eq(1)").get(i).text().replace("&nbsp","");
                       arr_ships.add(ships);

                   dwt = table.select("td:eq(3)").get(i).text().replace("&nbsp","");
                       arr_dwt.add(dwt); 

Is it possible to do the same with Selenium?

I currently have:

String text = driver.findElement(By.xpath("//div[@id='cphBody_Report_grid']")).getText();

This gets the table but I am unsure as to how I could select the different parts of the table like I do in j-soup. I would welcome any advice. Thank you.

Edit: I found this CookBook for Selenium and selectors in general and found it rather helpful when starting with selenium: https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/

1
  • Why not use Jsoup to parse, etc. after you navigate to the page with Selenium (if you're already familiar with Jsoup?) Commented Nov 3, 2014 at 0:16

1 Answer 1

1

To answer the question. Yes this is possible.

Get table row tds

List<WebElement> rowData = findElements(By.cssSelector("table tr td"));

For (WebElement we : rowData) {
    //do something with the tds
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, I have in the mean time been able to select the table data cell by cell, but do you know how to select the individual contents of each cell? I have updated my question, Thanks. Hold that, I will ask a new question.
Calling .text() on the element returns the string as text in the td
A sample implemantation is here github.com/alicakil/SeleniumCrawler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.