我想获取所有菜单页面链接,以便从每个菜单页面获取产品, 这是菜单的HTML代码
<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>
我需要获取所有href链接并单击每个链接以执行某些操作。
这里是我尝试但输出为null的代码:
List<WebElement> cat=driver.findElements(By.xpath("//*[@id='cavas_menu']"));
for (WebElement catmenu : cat){
System.out.println(catmenu.getAttribute("href"));
}
}
答案 0 :(得分: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"));
}
答案 1 :(得分: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);
}
}