2013-10-11 112 views
0

我想解析這個代碼在硒上,基本上需要列表中的第一項,即「product one-col new」!點擊...python selenium css selector第n個元素

<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> 

我已經試過這

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

,但它不工作?

有什麼想法嗎?

UPDATE

正確的路徑是

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

回答

2

看起來你想直接孩子,所以選擇應該使用兒童組合子。另外,還有:first-of-type/:first-child

#product-list > :first-child 
+0

感謝奏效? – krisdigitx

相關問題