Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用Selenium按类查找所有链接_Html_Powershell_Selenium_Xpath - Fatal编程技术网

Html 使用Selenium按类查找所有链接

Html 使用Selenium按类查找所有链接,html,powershell,selenium,xpath,Html,Powershell,Selenium,Xpath,网页上有几篇文章。我需要得到所有文章的链接 我使用Selenium和Powershell 我使用以下工具进行搜索: FindElementByXPath("//*[contains(@class, 'without')]").getattribute("href")` 但只能获得第一篇文章的链接 如何获得所有文章的链接 所有链接文章查看: <a class="without" href="http://articlelink.html"><h2>article<

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

我使用Selenium和Powershell

我使用以下工具进行搜索:

FindElementByXPath("//*[contains(@class, 'without')]").getattribute("href")` 
但只能获得第一篇文章的链接

如何获得所有文章的链接

所有链接文章查看:

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

我对
Powershell
一无所知,但是使用
java和selenium
可以像下面提到的代码一样完成这项工作

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

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
}
List links=driver.findElements(By.className(“无”);//使用list web元素获取类名为“without”的所有web元素
System.out.println(links.size())//页面上的链接总数。

对于(int i=0;i我对
Powershell
一无所知,但是使用
java和selenium
可以像下面提到的代码那样执行此操作

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

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
}
List links=driver.findElements(By.className(“without”);//使用List web元素获取所有web元素,其类名为“without”
System.out.println(links.size());//页面上的链接总数。

对于(int i=0;i使用复数形式:
FindElementsByXPath
谢谢!但现在我得到了指向同一元素的两个相同链接。可能是为了避免重复?我不知道Selenium,但您可以在PS:
sort-unique
中重复删除列表:
FindElementsByXPath
谢谢!但现在我得到了两个id指向同一元素的诱惑链接。可能是为了避免重复?我不知道Selenium,但您可以在PS:
sort-unique
中对列表进行重复删除。getText()不一定返回URL。您的代码示例对我来说很好,只是我使用了.getAttribute(“href”) instead@Bill,你说得对,我的答案也正确。顺便说一句,谢谢你的回答。:)我对代码
的解释对你有帮助吗?如果有帮助,请将此答案标记为
已接受
,如果它解决了你的问题。..getText()不一定返回URL。您的代码示例在我看来很好,只是我使用了.getAttribute(“href”)instead@Bill,你说得对,我的答案也对了。顺便说一句,谢谢你的回答。:)我对代码
的解释对你有帮助吗
如果有帮助,请将此答案标记为
已接受
,如果它解决了你的问题。