krisdigitx
krisdigitx

Reputation: 7126

python selenium css selector nth element

I am trying to parse this code on selenium and basically want the first item from the list i.e "product one-col new"! clicked...

<ul id="product-list">

    <li class="product one-col new">
        <ul>
            <li class="image" title="one item 5">
                <a href="#product/0f37f703-ed9a-3d10-86c0-7d0c0fcdb9ec">
                    <img width="" height="" alt="" src="/content/images/ProductImages/you-are-here/3/E/4/3E42561208174E6FBC81A10CDAE48659.jpg"></img>
                    <span class="new"> … </span>
                    <span class="hover"></span>
                </a>
                <p class="retailer">

                    CHECK ME

                </p>
                <p class="brand"></p>
            </li>
            <li class="price"> … </li>
            <li class="name" title="one item 5"> … </li>
            <li class="first-seen"> … </li>
        </ul>
    </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>

I have tried this

browser.find_element_by_css_selector("ul[id='product-list']  li:nth-of-type(1)").click()

but it does not work?

any idea?

UPDATE:

the correct path was

browser.find_element_by_css_selector("ul#product-list > :first-child").text

Upvotes: 0

Views: 1698

Answers (1)

Ry-
Ry-

Reputation: 224857

Looks like you want a direct child, so the selector should use the child combinator. Plus, there’s :first-of-type/:first-child:

#product-list > :first-child

Upvotes: 2

Related Questions