点击一个循环,直到页面“加载更多”显示完全

问题描述:

我需要从这个网页https://www.xxx.com/search/all?name=sporanox点击一个循环,直到页面“加载更多”显示完全

获取所有的名字在我的网页,我有一个按钮“加载更多”基本上意味着还有一些项目要在屏幕上显示。载入更多按钮

HTML源代码如下:

a href="#" data-number="1" data-size="15" data-url="/search/paginate?name=sporanox" class="pgntnCntnrBar btn btn-primary">Load More</a><div class="loading-icon v2 hide"></div> 

因此,我需要在PowerShell中的一些方式,其中脚本可以在一个循环中,从而显示整个页面点击本身“加载更多”。

我不能很好地使用PowerShell熟悉某处读取可以使用click()方法,所以我做了以下内容:

$regex = [RegEx]'">Load More</a>' 
$url = ‘https://www.xxx.com/search/all?name=sporanox’ 
$wc = New-Object System.Net.WebClient 
$content = $wc.DownloadString($url) 
$a = $regex.Matches($content) | ForEach-Object { $_.Groups[0].Value } 
$a.click() 

但我得到一个错误:

Method invocation failed because System.String does not contain a method named 'click'


更新: 我可以找到以下方法来点击。但不知道如何将其放入循环。

$ie = New-Object -ComObject InternetExplorer.Application 
$ie.visible=$true 
$ie.navigate2('https://www.xxx.com/search/all?name=sporanox') 
while($ie.Busy) {Start-Sleep 1} 
$links = $ie.Document.getElementsByTagName('A') 
$yt = $links | where {$_.innerText -eq 'Load More'} 
$yt.click() 
+0

您的第一个示例将不会按原样工作,因为您操作的是不再连接到网站的字符串。为什么第二个示例需要循环?哦..负载更多不断来...我想我得到它。 – Matt 2014-12-13 16:15:57

+0

我无法理解你的评论。你的第一个例子...连接到网站。这是怎么回事? – Yogesh 2014-12-14 15:07:32

+1

该方法被称为'DownloadString'。在你的第一个例子中,你只使用文本。是的,你找到了这场比赛,但你没有在那个网站上工作。你只是在处理你下载的字符串,这就是为什么你会得到错误。 – Matt 2014-12-14 15:11:29

更新,看起来像他们隐藏'加载更多'按钮,一旦他们完成。所以额外的检查是必要的。代码更新:

$ie = New-Object -COMObject InternetExplorer.Application 
$ie.visible = $true 
$site = $ie.Navigate('https://www.xxx.com/search/all?name=za') 
$ie.ReadyState 

while($true) 
{ 
    while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 } 
    try { 

     $numTries = 0 
     $link = $null 
     while ($link -eq $null -and $numTries -le 5) 
     { 
      $link = $ie.Document.get_links() | where-object {$_.innerText -eq 'Load More'} 
      if ($link -eq $null) 
      { 
       sleep -Milliseconds 1000 
      } 

      $numTries++ 
     } 
     if ($link -ne $null) 
     { 
      if ($link.clientHeight -eq 0) 
      { 
       break 
      } 
      [Void]$link.click() 
     } 
     else 
     { 
      break 
     } 
    } 
    catch 
    { 
     break 
    } 
} 

PS。我想用$ie.Document.getElementsByTagName('A')但我得到一个例外。

+0

有时它不起作用。也许在我的结尾很慢,这可能是单一的原因。例如:https://www.healthkartplus.com/search/all?name=za不工作 – Yogesh 2014-12-24 14:38:36

+1

是的,我能够重现它。看起来'get_links'没有完成,所以'$ link'仍然是$ null。看看这个新版本是否有帮助 – 2014-12-24 15:27:17

+0

非常好,谢谢你。 – Yogesh 2014-12-24 16:48:34