ruby, How to get data from a column in a table using CSS ?

64 views
Skip to first unread message

Abdel Latif

unread,
Feb 1, 2018, 11:09:10 AM2/1/18
to Selenium Users
Let say I have a table that has an id or a class, How to print out data text  for example from column number 3 of all the table.
Thanks

Abdel Latif

unread,
Feb 1, 2018, 12:02:40 PM2/1/18
to Selenium Users
I tried this :
browser.find_elements(:xpath => "//table[@id='booktable']/tbody/tr/td:nth-child(0)")

I got :

Given xpath expression "//table[@id='booktable']/tbody/tr/td:nth-child(0)" is invalid: SyntaxError: The expression is not a legal expression.


AND I tried this :
browser.find_elements(:css,"table[@id='booktable']/tbody/tr/td:nth-child(0)")

I got :

Given css selector expression "table[@id='booktable']/tbody/tr/td:nth-child(0)" is invalid: SyntaxError: 'table[@id='booktable']/tbody/tr/td:nth-child(0)' is not a valid selector

Titus Fortner

unread,
Feb 1, 2018, 12:15:43 PM2/1/18
to seleniu...@googlegroups.com
Hi Abdel,

Another Watir example
To get an array of text of the data cell in the 3rd column of each row
of a table:

browser.table(id: 'booktable').rows.map { |row| row.td(index: 2).text }

___

Titus Fortner
Core Contributor | Selenium & Watir
Solution Architect | Sauce Labs
> --
> You received this message because you are subscribed to the Google Groups
> "Selenium Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to selenium-user...@googlegroups.com.
> To post to this group, send email to seleniu...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/selenium-users/c9468e71-b44b-4b85-8335-a4802a7ca8a9%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

BlackFrost

unread,
Feb 1, 2018, 12:25:09 PM2/1/18
to Selenium Users
Hi Abdel,

Don't want to sound rude, but have you tried testing your selectors in Dev Tools every browser has? 
That would hopefully help you resolve many of your selectors related questions.

Just making sure you are aware of this functionality:
If you press F12 you'll get to dev tools and in the search box you can type your selectors and see what you are getting.
Chrome supports both Xpath and Css selectors (need to press F12 to open dev tools and then Ctrl+F to open the search box in Chrome), Firefox - Css selectors only.



And you could use out-of-the-box functionality to generate selectors (the use absolute path that is not the best but could be enough in many cases). Locate the element you need in HTML and right-click:




Titus Fortner

unread,
Feb 1, 2018, 1:13:35 PM2/1/18
to seleniu...@googlegroups.com
Please do not do this and especially do not recommend that others do this. It practically guarantees that any changes to a site you are working with will break your code. It does not take much effort to find something less brittle.


___

Titus Fortner
Core Contributor | Selenium & Watir
Solution Architect | Sauce Labs



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

BlackFrost

unread,
Feb 1, 2018, 1:32:26 PM2/1/18
to Selenium Users
I'm afraid I have to disagree.

Xpath and Css selectors can be written in a generic way that would cater for many site changes without actual need to update selectors.
It's just a matter of mastering selector syntax and capabilities that CSS and XPath support. And this syntax luckily is fairly easy to learn.

However you are right in the context of using absolute path selectors. In this case changes to the site most likely will require changes to the selectors. That is why in my previous post I mentioned it wasn't the best to use such selectors. But my guess is that Abdel is only starting to use Selenium and those auto-generated selectors would help him at least see the correct syntax and get the tests working in a more a less stable way.


Abdel Latif

unread,
Feb 1, 2018, 2:21:01 PM2/1/18
to Selenium Users
Thanks BlackFrost, I am aware of the dev tool, but somethimes it does not give you exactelly what you want.
 let say I have a table, and I right clicked (on the first cell of the the first column) and get the css from the dev tool :

#booktable > tbody > tr:nth-child(1) > td:nth-child(1)

But how about to get one that give you all cells in a column

I tried this :
#booktable > tbody > tr > td:nth-child(1)
BUT  I got :

invalid selector: Unable to locate an element with the xpath expression #content > div > div > table > tbody > tr > td:nth-child(4) because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '#content > div > div > table > tbody > tr > td:nth-child(4)' is not a valid XPath expression.


Thanks,

Abdel Latif

unread,
Feb 1, 2018, 3:48:23 PM2/1/18
to seleniu...@googlegroups.com
Thanks Titus, it looks like Watir make the life easy, I may switch to it.

> email to selenium-users+unsubscribe@googlegroups.com.
> To post to this group, send email to selenium-users@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/kCEsrPq8_ew/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAKSYPujTLLv5dyrT6zQWdLUVX50%3DLDxtPmirPWGZsR4TpoNZ4Q%40mail.gmail.com.

BlackFrost

unread,
Feb 1, 2018, 4:54:39 PM2/1/18
to Selenium Users
Hey Abdel,

I think you accidentally missed that you are using a CSS selector but you Selenium test is treating it as an Xpath (see it's complaining about "not a valid XPath expression"?). You may want to update either the selector to be an XPath selector or Selenium method you are using to locate an element. 

To make things a little easier as I'm not aware of the exact table structure you are trying to select I used the one available in W3C web site https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro
It's a very simple table, no classes, pure HTML.  Let's assume you want to select the first column values using XPath.
The selector could look like this:

.//table//td[1]



Now using this selector you could use FindElements() method to get a collection of all elements that match the selector. And then iterate through these elements and (if that's the goal) check for a specific text.

Sorry I'm not familiar with Ruby syntax, but you could get the idea from the C# code below:

                IList<IWebElement> firstColumn = driver.FindElements(By.XPath(".//table//td[1]"));


               
foreach (IWebElement el in firstColumn)
               
{
                   
if (el.Text.Contains("Alfreds Futterkiste")) //let's say we want to make sure "Alfreds Futterkiste" is present in the first column no matter the table line number
                   
{
                       
<...>
                   
}
               
}

P.S. When I was suggesting Dev tools I was more referring to the functionality that allows you to test your custom written selectors than using the generation functionality. Selector generator is not good with generating generic selectors indeed.

Titus Fortner

unread,
Feb 1, 2018, 5:07:33 PM2/1/18
to seleniu...@googlegroups.com
BlackFrost, sorry, I missed the part where you said "not the best." My target audience with Selenium is large test suites where it it is never a good idea, but still frequently used, so I get concerned when I see people recommending it in any way.

Because he is using Ruby, Watir is an easy choice to make all of this pretty straightforward. One goal of the project is for people to be able to quickly locate elements in a non-brittle fashion without needing to know any XPath, and with limited need for knowing CSS.




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.

Abdel Latif

unread,
Feb 1, 2018, 7:55:03 PM2/1/18
to seleniu...@googlegroups.com
Thanks lot BlackFrost, this is exactly what I was looking for, very helpful.

--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/kCEsrPq8_ew/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/81491757-f1f8-41b2-926a-f8919eed20cb%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages