0

我有一些手風琴元素像這樣的網站:如何打開並捕捉手風琴內的所有鏈接?

enter image description here

<div class="col-md-12"> 

     <a data-toggle="collapse" data-parent="#accordion1" title="Click to expand pet name" href="#petName1"> 
      <span class="list-unstyled" style="text-decoration: none;"></span> Dog 
     </a> 


     <ul id="petName1" class="collapse"> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=200465" title="Click to view Dog | PET SHOP"> 
           Dog</a> | PET SHOP 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=203874" title="Click to view Dog | PET SHOP"> 
           Dog</a> | PET SHOP 
        </a> 

       </li> 

     </ul> 
     </div> 
    </td> 
</tr> 

<tr> 
    <td> 

    <div class="col-md-12"> 

     <a data-toggle="collapse" data-parent="#accordion1" title="Click to expand pet name" href="#petName2"> 
      <span class="list-unstyled" style="text-decoration: none;"></span> Cat 
     </a> 


     <ul id="petName2" class="collapse"> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=076477" title="Click to view Cat"> 
           Cat 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=077575" title="Click to view Cat"> 
           Cat 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=078773" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=090548" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=091226" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=091624" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=091650" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=202357" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=204846" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=205300" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=205519" title="Click to view Cat"> 
           Cat</a> 
        </a> 

       </li> 

     </ul> 


     </div> 
    </td> 
</tr> 

<tr> 
    <td> 

    <div class="col-md-12"> 

     <a data-toggle="collapse" data-parent="#accordion1" title="Click to expand pet name" href="#petName3"> 
      <span class="list-unstyled" style="text-decoration: none;"></span> Gold Fish 
     </a> 

     <ul id="petName3" class="collapse"> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=021540" title="Click to view Gold Fish"> 
           Gold Fish </a> 
        </a> 

       </li> 

     </ul> 


     </div> 
    </td> 
</tr> 
<tr> 
    <td> 

    <div class="col-md-12"> 

     <a data-toggle="collapse" data-parent="#accordion1" title="Click to expand pet name" href="#petName4"> 
      <span class="list-unstyled" style="text-decoration: none;"></span> Horse 
     </a> 


     <ul id="petName4" class="collapse"> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=020702" title="Click to view Horse"> 
           Horse </a> 
        </a> 

       </li> 

     </ul> 
     </div> 
    </td> 
</tr> 

<tr> 
    <td> 

    <div class="col-md-12"> 

     <a data-toggle="collapse" data-parent="#accordion1" title="Click to expand pet name" href="#petName5"> 
      <span class="list-unstyled" style="text-decoration: none;"></span> Ferret 
     </a> 
     <ul id="petName5" class="collapse"> 

       <li> 
        <a href="/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=200153" title="Click to view Ferret"> 
           Ferret </a> 
        </a> 
       </li>    
     </ul> 

     </div> 
    </td> 
</tr> 
</tbody> 

我怎樣才能在所有的部分與硒的網絡驅動器點擊並保存手風琴的每個部分下面的鏈接?我試圖:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox() 
driver.get("https://example.com") 
inputElement = driver.find_element_by_id("searchterm") 
inputElement.send_keys('pets') 
inputElement.send_keys(Keys.ENTER) 
driver.find_element_by_id("mp-pusher").click() 
inputElement.submit() 

然而,它不起作用,因爲它沒有做任何事(它只是伸出了)。有關如何獲得所有鏈接並打開所有手風琴的任何想法?

+0

有時我只是使用'driver.page_source'屬性(所有JavaScript運行後的瀏覽器源代碼)抓取源代碼並使用類似beautifulsoup的東西進行解析。 –

+0

能否爲這種情況提供一個例子?......感謝您的幫助! @PauloScardine –

回答

0

以下是java代碼不知道蟒蛇,但你可以嘗試同樣的邏輯

List<WebElement> accordions = driver.findElements(By.xpath("\\a[@data-parent='#accordion1']"); 

這將讓所有的手風琴在列表中。現在遍歷列表並點擊每個手風琴。

要保存每個手風琴的鏈接,您可以使用先前列表的webelement。

+0

我得到了這個:'InvalidSelectorException:消息:給定xpath表達式「\ a [@ data-parent ='#accordion']」無效:SyntaxError:表達式不是合法表達式。' –

+0

它的「\\ a [@ data-parent ='#accordion1']「你寫過」\ a [@ data-parent ='#accordion']「 –

相關問題