PowerShell报错:无法对空值表达式调用方法,求助实现登录按钮点击
Hey there! Let's break down why your script is throwing that null error and get it working smoothly. You’re right—your $link variable isn’t finding the "Log ind" button, so let’s fix this step by step, no jargon required.
1. First, Fix the Page Wait Logic
Your current while ($ie.Busy -eq $true) check isn’t super reliable for IE. Sometimes IE says it’s not busy, but the page hasn’t finished rendering all elements yet. Let’s replace that with a more robust wait that checks both the busy state and the page’s ready state:
$ie = New-Object -com InternetExplorer.Application $ie.visible=$true $ie.navigate('https://itd-skp.sde.dk/') # Wait until the page is fully loaded (ReadyState 4 means "complete") while ($ie.Busy -or $ie.ReadyState -ne 4) { Start-Sleep -Milliseconds 500 # Wait half a second instead of 4 to be responsive }
2. Find the "Log ind" Button Correctly
There are a few common reasons your original getElementsByTagname('A') might miss the button:
- The button might have extra spaces around its text (like
' Log ind 'instead of'Log ind') - It might not be an
<a>tag at all (could be a<button>or<input>) - It might be hidden inside an iframe (some sites load login forms in these)
Try These Simple Fixes:
Fix A: Trim Extra Spaces from the Text
Add .Trim() to remove any leading/trailing spaces from the element’s text—this is a super common fix:
$link = $ie.Document.getElementsByTagName('A') | Where-Object { $_.innerText.Trim() -eq 'Log ind' }
Fix B: Search All Elements for the Button
If the button isn’t an <a> tag, let’s look through every element on the page to find it by its text:
# Look through all elements to find any with the trimmed text 'Log ind' $loginButton = $ie.Document.all | Where-Object { $_.innerText.Trim() -eq 'Log ind' }
Fix C: Handle Iframes (If Needed)
If the login button is inside an iframe, you need to access the iframe’s document first. Let’s loop through iframes to find the right one:
# Get all iframes on the page $iframes = $ie.Document.getElementsByTagName('IFRAME') # Loop through each iframe to check for the login button foreach ($iframe in $iframes) { $iframeDoc = $iframe.contentDocument $loginButton = $iframeDoc.all | Where-Object { $_.innerText.Trim() -eq 'Log ind' } if ($loginButton) { break # Stop looping once we find the right iframe } }
3. Test and Click the Button
Once you have the button stored in $loginButton, add a quick check to make sure we found it before clicking—this avoids the null error:
if ($loginButton) { $loginButton.click() # Wait again for the login page to load after clicking while ($ie.Busy -or $ie.ReadyState -ne 4) { Start-Sleep -Milliseconds 500 } # Now you can add code to fill in username/password fields here! } else { Write-Host "Couldn't find the 'Log ind' button. Check the element details using the debug tip below." }
Pro Debugging Tip
If you’re still stuck, add this line after the page loads to print every element’s trimmed text—this will show you exactly what text the button has (no hidden spaces!):
$ie.Document.all | ForEach-Object { if ($_.innerText) { Write-Host "Element Text: '$($_.innerText.Trim())'" } }
内容的提问来源于stack exchange,提问作者Mike skovby

