gpt4 book ai didi

javascript - Powershell搜索自动化-GetElementbyID不会返回使用Javascript创建的控件的值

转载 作者:行者123 更新时间:2023-12-03 00:14:02 25 4
gpt4 key购买 nike

我正在进行一个项目,以在县网站上自动搜索税收记录。最终,我希望能够为Powershell提供ID编号列表,并使其返回所有这些编号的结果。到目前为止,我的代码在这里:

#Open IE and go to www.gastontax.com
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.gastontax.com/")
$ie.visible = $true

#Yield the script while the page is loading
While ( $ie.busy -eq $true){
[System.Threading.Thread]::Sleep(200)
}

#Set the document and the frames
$doc = $ie.document
$frames = $doc.frames

#Accept the notification
$btn = $doc.getElementByID("ctl00_Tax_btnAccept")
$btn.click()

#Yield the script while the page is loading
While ( $ie.busy -eq $true){
[System.Threading.Thread]::Sleep(200)
}

#Create the drop-down search parameters
$taxyear = "All"
$status = "Unpaid"
$searchtype = "Both"
$searchparam = "Parcel Number"
$searchtext = "XXXXXX"

#Set the drop-down search parameters
$doc.getElementbyID("ctl00_Tax_drpTaxYear").value = $taxyear
$doc.getElementbyID("ctl00_Tax_drpStatus").value = $status
$doc.getElementbyID("ctl00_Tax_drpSearchType").value = $searchtype
$doc.getElementbyID("ctl00_Tax_drpSearchParam").value = $searchparam

#Create the parcel name parameter
$doc.getElementbyID("ctl00_Tax_txtSearchParam").value = $searchtext

$btn2 = $doc.getElementByID("ctl00_Tax_btnSearch")
$btn2.click()

但是,每当我尝试设置控件的值时,对于每条getElementbyID行我都会收到以下消息:

The property 'value' cannot be found on this object. Verify that the property exists and can be set.



我注意到,我要查找的值-“ctl00_Tax_drpTaxYear”,“ctl00_Tax_drpStatus”等-直到我点击欢迎页面上的按钮(在$ btn.click( ))。这可能与Powershell抛出的错误有关吗?如果是这样,我将如何解决?

谢谢!

最佳答案

我只能将其与涉及战略性地切换IE(Internet Explorer)可见性的黑客一起使用,这似乎是DOM在$ie.Document中完全体现的必要条件(请参见下面的源代码中的注释)。

该骇客似乎可以在装有IE 11的Windows 10机器上可靠地运行,但可以使用YMMV。

通常:

  • Internet Explorer COM Automation interface已过时,因此值得考虑替代方案,例如Edge's WebDriver support(与may become a W3C standard完全不同的技术)。
  • 坚持使用IE COM对象,如果改为使用DOM-related events,则可能不需要黑客,但我不确定PowerShell中是否支持它们。

  • # Helper function for wating until a condition is true.
    function await([scriptblock] $sb) {
    while (-not (& $sb)) { Start-Sleep -Milliseconds 200 }
    }

    $ie = new-object -com "InternetExplorer.Application"
    $ie.navigate("http://www.gastontax.com/")

    # Wait until the page has finished loading.
    await { $ie.ReadyState -eq 4 }

    # Click the "Yes, I accept" button.
    $ie.document.getElementById("ctl00_Tax_btnAccept").click()

    # !! HACK (part 1 of 2):
    # !! Delaying making IE visible until here seems to be the only way
    # !! to get the new DOM to be at least *partially* reflected in $ie.document.
    $ie.visible = $true

    # Wait until the page has finished loading.
    await { $ie.ReadyState -eq 4 }

    # !! HACK (part 2 of 2):
    # !! Inexplicably, another toggling of visibility is what it takes for *all*
    # !! elements in the new DOM to be accessible via $ie.Document.
    $ie.visible = $false
    $ie.visible = $true

    #Create the drop-down search parameters
    $taxyear = "All"
    $status = "Unpaid"
    $searchtype = "Both"
    $searchparam = "Parcel Number"
    $searchtext = "XXXXXX"

    #Set the drop-down search parameters
    $doc = $ie.document
    $doc.getElementById("ctl00_Tax_drpTaxYear").value = $taxyear
    $ie.document.getElementById("ctl00_Tax_drpStatus").value = $status
    $ie.document.getElementById("ctl00_Tax_drpSearchType").value = $searchtype
    $ie.document.getElementById("ctl00_Tax_drpSearchParam").value = $searchparam

    #Create the parcel name parameter
    $ie.document.getElementById("ctl00_Tax_txtSearchParam").value = $searchtext

    $ie.document.getElementById("ctl00_Tax_btnSearch").click()

    关于javascript - Powershell搜索自动化-GetElementbyID不会返回使用Javascript创建的控件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202568/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com