按照课程查找Selenium的所有链接 [英] Find all links by class with Selenium

查看:148
本文介绍了按照课程查找Selenium的所有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页上有几篇文章。我需要获得所有文章的链接。



我使用Selenium和Powershell。



我使用以下内容进行搜索:

  FindElementByXPath(// * [contains(@class,'without')])。getattribute(href)`

但只能获得第一篇文章的链接。



如何获取链接到所有的文章?



所有链接文章视图:

  < a class =withouthref =http://articlelink.html>< h2> article< / h2>< / a> 


解决方案

我对 Powershell 但是使用 java with selenium 你可以像下面提到的代码那样做。



我知道这不是一个适当的答案,但下面的代码会给你提示,你应该如何去用其他语言。

 列表< WebElement> links = driver.findElements(By.className(without)); //使用列表web-element获取所有web-elements,其类名称为without
System.out.println(links.size()); //页面上的链接总数。

for(int i = 0; i< links.size(); i ++)
{
System.out.println(links.get(i).getAttribute( HREF)); //使用for循环获取一个一个的链接名称。
links.get(i).click(); //点击链接,如果你想点击
Thread.sleep(2500); //等待2.5秒
}

希望我的上述回答对您有所帮助。


On the web page there are a few articles. I need to get links to all articles.

I use Selenium and Powershell.

I do a search with:

FindElementByXPath("//*[contains(@class, 'without')]").getattribute("href")` 

but only get a link to the first article.

How to get links to all the articles?

All links articles view:

<a class="without" href="http://articlelink.html"><h2>article</h2></a>

解决方案

I don't know anything about Powershell But Using java with selenium you can do this like below mentioned code.

I know it's not a proper answer to deal with, but below code will give you the hint, that how you should go with other language.

List<WebElement> links = driver.findElements(By.className("without"));   // Using list web-element get all web-elements, whose classname name as "without"
System.out.println(links.size());             //total number of links on the page.

for(int i = 0;i<links.size();i++)           
{
    System.out.println(links.get(i).getAttribute("href"));   //Using for loop getting one by one links name.
    links.get(i).click();                         // click the link if you want to click
    Thread.sleep(2500);                          //wait for 2.5 seconds
}

Hope my above answer will help you.

这篇关于按照课程查找Selenium的所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆