2015-10-23 106 views
2

我想所有的菜單頁面的鏈接,以便從每個菜單頁面獲取產品, 這裏是菜單提取菜單鏈接

<nav id="cavas_menu" class="navbar navbar-default" role="navigation"> 
     <!-- Brand and toggle get grouped for better mobile display --> 
     <div class="navbar-header"> 
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> 
       <span class="sr-only">Toggle navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
      </button> 
     </div> 
     <!-- Collect the nav links, forms, and other content for toggling --> 
     <div id="leo-top-menu" class="collapse navbar-collapse navbar-ex1-collapse"> 
      <ul class="nav navbar-nav megamenu"><li class=" parent dropdown aligned-left "> 
        <a href="#" class="dropdown-toggle has-category" data-toggle="dropdown" target="_self"><span class="menu-title">Categories</span><b class="caret"></b></a><div class="dropdown-sub dropdown-menu" style="width:400px"><div class="dropdown-menu-inner"><div class="row"><div class="mega-col col-sm-6"> <div class="mega-col-inner "><div class="leo-widget"><div class="widget-subcategories"> 
     <div class="widget-inner"> 
     <div class="menu-title">Home</div> 
     <ul> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/14-e-juices" title="E-Juices" class="img"> 
         E-Juices 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/15-mods" title="Mods" class="img"> 
         Mods 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/17-accessories" title="Accessories" class="img"> 
         Accessories 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/19-starter-kits" title="Starter Kits" class="img"> 
         Starter Kits 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/21-ego" title="Ego" class="img"> 
         Ego 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/27-rda" title="RDA" class="img"> 
         RDA 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/28-regulated-mods" title="Regulated Mods" class="img"> 
         Regulated Mods 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/29-clearomizer" title="Clearomizer" class="img"> 
         Clearomizer 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/30-drip-tips" title="Drip Tips" class="img"> 
         Drip Tips 
        </a> 
       </li> 
          <li class="clearfix"> 
        <a href="https://www.vapors.com/33-sales" title="Sales" class="img"> 
         Sales 
        </a> 
       </li> 

     </ul> 
    </div> 
</div> 
</div></div></div><div class="mega-col col-sm-6"> <div class="mega-col-inner "><div class="leo-widget"> <div class="widget-products"> 
     <div class="menu-title"> 
     New Products 
    </div> 
     <div class="widget-inner"> 

       <div class="product-block"> 










      <div class="product-container clearfix"> 
        <div class="image "> 
          <a class="product_img_link" href="https://www.vapors.com/rda/794-plume-veil-v25-limited-ed.html" title="Plume Veil V2.5 Limited Ed." itemprop="url"> 
           <img class="replace-2x img-responsive" src="https://www.vapors.com/307-home_default/plume-veil-v25-limited-ed.jpg" alt="Plume Veil V2.5 Limited Ed." title="Plume Veil V2.5 Limited Ed." itemprop="image"> 
         </a> 
                  <span class="new-box"> 
            <span class="new-label">New</span> 
           </span> 
               </div> 
        <div class="product-meta"> 
          <h5 itemprop="name"> 
                   <a class="product-name" href="https://www.vapors.com/rda/794-plume-veil-v25-limited-ed.html" title="Plume Veil V2.5 Limited Ed." itemprop="url"> 
            Plume Veil V2.5 Limited Ed. 
           </a> 
          </h5> 
          <p class="product-desc" itemprop="description"> 

          </p>  
                  <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" class="content_price"> 
                      <span itemprop="price" class="price product-price"> 
              $89.99          </span> 
             <meta itemprop="priceCurrency" content="1"> 
                          </div> 
                </div> 
       </div>     



      <div class="product-container clearfix"> 
        <div class="image "> 
          <a class="product_img_link" href="https://www.vapors.com/rda/793-plume-veil-v25.html" title="Plume Veil V2.5" itemprop="url"> 
           <img class="replace-2x img-responsive" src="https://www.vapors.com/306-home_default/plume-veil-v25.jpg" alt="Plume Veil V2.5" title="Plume Veil V2.5" itemprop="image"> 
         </a> 
                  <span class="new-box"> 
            <span class="new-label">New</span> 
           </span> 
               </div> 
        <div class="product-meta"> 
          <h5 itemprop="name"> 
                   <a class="product-name" href="https://www.vapors.com/rda/793-plume-veil-v25.html" title="Plume Veil V2.5" itemprop="url"> 
            Plume Veil V2.5 
           </a> 
          </h5> 
          <p class="product-desc" itemprop="description"> 
           This all new version of the Veil is a Sub-Ohm V2.5 RDA by... 
          </p>  
                  <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" class="content_price"> 
                      <span itemprop="price" class="price product-price"> 
              $89.99          </span> 
             <meta itemprop="priceCurrency" content="1"> 
                          </div> 
                </div> 
       </div>     


     </div> 



    </div> 
</div> 
</div></div></div></div></div></div></li><li class="active"> 
         <a href="https://www.vapors.com/14-e-juices" target="_self" class="has-category"><span class="menu-title">E-Juices</span></a></li><li class=""> 
         <a href="https://www.vapors.com/15-mods" target="_self" class="has-category"><span class="menu-title">Mods</span></a></li><li class=""> 
         <a href="https://www.vapors.com/28-regulated-mods" target="_self" class="has-category"><span class="menu-title">Regulated Mods</span></a></li><li class=""> 
         <a href="https://www.vapors.com/27-rda" target="_self" class="has-category"><span class="menu-title">RDA</span></a></li><li class=""> 
         <a href="https://www.vapors.com/19-starter-kits" target="_self" class="has-category"><span class="menu-title">Starter Kits</span></a></li><li class=""> 
         <a href="https://www.vapors.com/29-clearomizer" target="_self" class="has-category"><span class="menu-title">Clearomizer</span></a></li><li class=""> 
         <a href="https://www.vapors.com/30-drip-tips" target="_self" class="has-category"><span class="menu-title">Drip tips</span></a></li><li class=""> 
         <a href="https://www.vapors.com/21-ego" target="_self" class="has-category"><span class="menu-title">Ego</span></a></li><li class=""> 
         <a href="https://www.vapors.com/17-accessories" target="_self" class="has-category"><span class="menu-title">Accessories</span></a></li><li class="sales"> 
         <a href="https://www.vapors.com/33-sales" target="_self" class="has-category"><span class="menu-title">Sales</span></a></li></ul> 
     </div> 
</nav> 

HTML代碼,我需要得到所有HREF鏈接,點擊每個鏈接來執行一些操作。

這裏是代碼,我嘗試過,但得到的輸出爲空:

List<WebElement> cat=driver.findElements(By.xpath("//*[@id='cavas_menu']")); 


    for (WebElement catmenu : cat){ 

     System.out.println(catmenu.getAttribute("href")); 


    } 
    } 

回答

0

我看過槽你的代碼,這裏是一個稍微修改後的版本:

public void sample() { 
    driver.get("C:\\Users\\dmalinovschi\\Desktop\\index.html"); 
     List<WebElement> listElements; 
     listElements = driver.findElements(By.xpath("//li[@class='clearfix']/a")); 

     for (int i=0; i<listElements.size(); i++) { 
      String link = listElements.get(i).getAttribute("href"); 
      System.out.println(link); 
     } 
} 

這裏是與變異在新標籤中打開它們:

public void sample() throws InterruptedException { 
    driver.get("base website link"); 

     List<WebElement> listElements; 
     listElements = driver.findElements(By.xpath("//li[@class='clearfix']/a")); 
     Actions builder = new Actions(driver); 
     Actions control = builder.keyDown(Keys.CONTROL); 
     for (int i=0; i<listElements.size(); i++) { 

      String link = listElements.get(i).getAttribute("href"); 
      System.out.println(link); 

      control.click(listElements.get(i)); 
      builder.build().perform();; 
      Thread.sleep(2000); 

     } 

} 
+1

感謝您的快速回復,但我得到一個錯誤,無效的選擇 –

+0

你需要打開一個新標籤上的鏈接?對不起,已經很快寫好了代碼。現在檢查編輯,我已經測試過了 –

+0

Dude,你試過了嗎? –

2

有幾個問題與您的代碼。

1)您的XPath在List<>中只有1個元素,因爲只有一個元素帶有id=cavas_menu

<nav id="cavas_menu" class="navbar navbar-default" role="navigation"> 

而這不是你要找的。您需要該菜單中的鏈接。

2)第二個問題是Selenium只會與可見元素和您想要的元素(鏈接)交互在菜單中,因此您需要將鼠標懸停在菜單上以暴露並刮擦它們。

我測試了下面的代碼,它的工作原理。

driver.get("https://www.vapors.com/"); 

// dismiss Age Certification dialog 
driver.findElement(By.cssSelector("a.over-18")).click(); 

// hover the menu to expose it 
Actions builder = new Actions(driver); 
builder.moveToElement(driver.findElement(By.cssSelector("span.menu-title"))).build().perform(); 

// gather the links and print the HREFs 
List<WebElement> links = driver.findElements(By.cssSelector("a.img")); 
for (WebElement link : links) 
{ 
    System.out.println(link.getAttribute("href")); 
} 
+0

我會建議的一個補充將是使用明確的等待來解決頁面上的鏈接。 * WebDriverWait *與* ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(「a.img」)))*結合使用,以確保下拉內容在嘗試解析它們之前有時間變得可見。 – Jeremiah

+0

@Jeremiah我一般我會同意,但在這種情況下,我沒有發現它是必要的。我通常在不等待的情況下編寫代碼,然後根據需要添加它們。 – JeffC