How to abbreviate the long Selenium CSS code for table elements?

Go To StackoverFlow.com

1

I am having trouble abbreviating the following selenium CSS code which has the element inside multiple tables. The code below gives me two checkboxes.

table[id$=gridReports]>tbody>tr:nth-of-type(2)>td:nth-of-type(2)>table[id$=panelReportInformation]>tbody>tr:nth-of-type(2)>td>table[id$=panelReportContent]>tbody>tr:nth-of-type(2)>span[id$=reportCheckBox] input

I cannot use this code since there is also another table with the same span and checkbox. The only difference is that the it is in different row. So if i put the code for another checkbox, it would look like this.

table[id$=gridReports]>tbody>tr:nth-of-type(3)>td:nth-of-type(2)>table[id$=panelReportInformation]>tbody>tr:nth-of-type(2)>td>table[id$=panelReportContent]>tbody>tr:nth-of-type(2)>span[id$=reportCheckBox] input

So the only difference is nth-of-type(i) for every table. So how can i shorten the css code?

Is there any option that i can shorten like table[id$=gridReports]>tbody>tr:nth-of-type(i) followed by span[id$=reportCheckBox] input.

Any help would be appreciated.

Thanks

2012-04-03 23:11
by seleniumlover


1

You could shorten it by narrowing your universe

el = driver.find_element_by_css_selector("table[id$=gridReports]>tbody>tr:nth-of-type(3)")
el.find_element_by_css_selector("span[id$=reportCheckBox] input")

OR

els = driver.find_elements_by_css_selector("span[id$=reportCheckBox] input")
for el in els:
    el.click()

That's in python but same idea in all bindings as you can chain a webelement request to a previous webdriver element

2012-05-01 04:01
by Bob Evans
Ads