Selenium

## CSS nth-child Selector

Consider if you want to get odd rows from a HTML table using Selenium WebDriver, your immediate idea will be ‘separating odd rows using FOR loop’. But we can accomplish it using nth-child (2n+1) selector.

In CSS Selector, we have a very useful structural pseudo-class selector i.e. ‘nth-child’ selector. ‘nth-child’ can be used to select ordered elements by giving the expression (an+b) or single element with positive integer as an argument. Consider if you want to get odd rows from a HTML table using Selenium WebDriver, your immediate idea will be ‘separating odd rows using FOR loop’. But we can accomplish it using nth-child (2n+1) selector.

nth-child (2n+1) expanded below.
(2*0) + 1=1=1st Row
(2*1) + 1=3=3rd Row
(2*2) + 1=5=5th Row

Selenium WebDriver-Java code below counts no. of odd rows in a table.

The below code gets first row from a table.

### Browser Compatibility

IE9+, FF 11.0+ on Win, FF 10.0.2+ on Mac, Safari 5.1, Chrome 18+ on Win and Chrome 17+ Mac.

#### Written By

In CSS Selector, we have a very useful structural pseudo-class selector i.e. ‘nth-child’ selector. ‘nth-child’ can be used to select ordered elements by giving the expression (an+b) or single element with positive integer as an argument. Consider if you want to get odd rows from a HTML table using Selenium WebDriver, your immediate idea will be ‘separating odd rows using FOR loop’. But we can accomplish it using nth-child (2n+1) selector.

nth-child (2n+1) expanded below.
(2*0) + 1=1=1st Row
(2*1) + 1=3=3rd Row
(2*2) + 1=5=5th Row

Selenium WebDriver-Java code below counts no. of odd rows in a table.

The below code gets first row from a table.

### Browser Compatibility

IE9+, FF 11.0+ on Win, FF 10.0.2+ on Mac, Safari 5.1, Chrome 18+ on Win and Chrome 17+ Mac.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6564369,"math_prob":0.7740543,"size":967,"snap":"2021-43-2021-49","text_gpt3_token_len":281,"char_repetition_ratio":0.11941849,"word_repetition_ratio":0.0,"special_character_ratio":0.2916236,"punctuation_ratio":0.14285715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.981137,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-27T07:09:20Z\",\"WARC-Record-ID\":\"<urn:uuid:2be5f279-a1c8-4133-bfff-a1a4b2dfb2e7>\",\"Content-Length\":\"190155\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fa2e4615-c0d0-418b-9bc6-d79139d00a4c>\",\"WARC-Concurrent-To\":\"<urn:uuid:d8634362-7a44-4a07-8096-42266634563f>\",\"WARC-IP-Address\":\"172.67.218.114\",\"WARC-Target-URI\":\"https://codoid.com/css-nth-child-selector/\",\"WARC-Payload-Digest\":\"sha1:G6UANX5LBQGYY6QZWYGFFURT4C73MA5P\",\"WARC-Block-Digest\":\"sha1:IMXEXOVOH4XPFFIK524J3ISEUMO7ZZRI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323588102.27_warc_CC-MAIN-20211027053727-20211027083727-00144.warc.gz\"}"}