如何点击<div>表格中的标签?



我是PowerShell脚本的新手,我被困在一个页面上,我需要点击一个将我重定向到下一页的链接。我尝试了许多组合,如childNodes/直接通过id获取它。但是这些方法似乎都不起作用。

网页代码片段:

<div id="id1" blablabla role="role1" aria-label="Redirect">
 <table role="role2" blabla>
  <tbody>
   <tr>
    <td class="class1" blabla>
     <div id="id2" blabla>Redirect</div>
    </td>
   </tr>
  </tbody>
 </table>
</div>
我代码:

$div = $ie.Document.getElementById("id1")
$table = $div.childNodes |? {$_.tagName -eq "table"}
$tbody = $table.childNodes |? {$_.tagName -eq "tbody"}
$tr = $tbody.childNodes |?{$_.tagName -eq "tr"}
$td = $tr.childNodes |?{$_.tagName -eq "td"}
$link = $td.childNodes |? {$_.tagName -eq "div"} |? {$_.innerText -match "Redirect"}
$link.click()

代码块2:

$link = $ie.Document.getElementById("id2")
$link.click()

上述两个代码块抛出的错误与下面相同。

You cannot call a method on a null-valued expression.
At *path* char:1
+ $td.click()
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

如何解决这个问题?

如果你想点击一个链接,你需要找到href属性,它是链接,并在上面做Invoke-WebRequest

<a class="header-nav-link notification-indicator tooltipped tooltipped-s js-socket-channel js-notification-indicator" aria-label="You have unread notifications" href="/notifications" data-ga-click="Header, go to notifications, icon:unread" data-hotkey="g n" data-channel="tenant:1:notification-changed:*****">
        <span class="mail-status unread"></span>
        <svg xmlns="http://www.w3.org/2000/svg" class="octicon octicon-bell" aria-hidden="true" viewBox="0 0 14 16" width="14" height="16" version="1.1"><path d="M 14 12 v 1 H 0 v -1 l 0.73 -0.58 c 0.77 -0.77 0.81 -2.55 1.19 -4.42 C 2.69 3.23 6 2 6 2 c 0 -0.55 0.45 -1 1 -1 s 1 0.45 1 1 c 0 0 3.39 1.23 4.16 5 c 0.38 1.88 0.42 3.66 1.19 4.42 l 0.66 0.58 H 14 Z m -7 4 c 1.11 0 2 -0.89 2 -2 H 5 c 0 1.11 0.89 2 2 2 Z" /></svg>
</a>

这是一个来自GitHub的通知按钮的示例html,你可以看到svg被包装在一个<a>标签中,如果你想'点击'那个svg,只是在<a>标签上找到了href属性(你可以用| select expand-property 'href'这样做)

,然后执行Invoke-WebRequest来获取url的内容

文档完全加载时调用代码块?

查找元素之前,先等待load

while($ie.busy) {sleep 1};

最新更新