Excel VBA运行报错-2147319765(8002802b)及工作表选择问题咨询
Let's break down your two issues one by one and fix that ChromeDriver code snippet along the way:
This error almost always ties to one of three common problems with Selenium/ChromeDriver setups or element timing:
Mismatched ChromeDriver and Chrome Browser Versions
This is the #1 culprit. ChromeDriver must be exactly aligned with the version of Chrome you have installed (e.g., if Chrome is v119.x, you need ChromeDriver v119.x).
Fix: Download the matching ChromeDriver version, replace your existing driver file, and either place it in your system PATH or explicitly define its location in code:driver.SetChromeDriver "C:\Your\Path\To\chromedriver.exe" driver.SetBinary "C:\Program Files\Google\Chrome\Application\chrome.exe"Missing/Broken Selenium References
If your VBA project doesn't have the correct Selenium library referenced, it can throw this error.
Fix: Open the VBA Editor → Go toTools > References→ Ensure "Selenium Type Library" (or "Selenium Basic") is checked. If it's missing, reinstall Selenium Basic.Element Not Found Due to Timing
Your code tries to interact with elements immediately after loading the page, but the page might not be fully rendered yet.
Fix: Replace hard waits with explicit waits to ensure elements are ready:Dim wait As New WebDriverWait wait.Timeout = 10 ' Wait up to 10 seconds wait.Until (Condition:=Function(d) d.FindElementByXPath(".//*[@id='loginForm']/div[1]/div[1]/input").IsDisplayed)
First, a quick best practice: avoid using Select/Activate in VBA whenever possible—directly reference worksheet objects instead. But if you need to switch focus to a sheet mid-macro, here's how:
Directly Reference Sheets (No Selection Needed)
Instead of selecting a sheet to modify it, target it directly:' Instead of Sheets("Sheet1").Select → Range("A1").Value = "Done" Sheets("Sheet1").Range("A1").Value = "Login initiated"Switch Focus to Excel (If Needed)
If you need the Excel window to become active (e.g., for user visibility), use this after ChromeDriver actions:AppActivate Application.Caption ' Brings Excel to front Sheets("Sheet1").Activate ' Optional: Focuses the specific sheet
Here's your updated code incorporating all the fixes above, plus proper variable declaration and cleanup:
Sub Selstart() Dim driver As ChromeDriver Set driver = New ChromeDriver ' Optional: Define paths if Chrome/ChromeDriver aren't in system PATH driver.SetChromeDriver "C:\Your\Path\To\chromedriver.exe" driver.SetBinary "C:\Program Files\Google\Chrome\Application\chrome.exe" driver.Get "abc.com" ' Wait for initial page load driver.Wait 3000 Dim a As String, b As String ' Fix: Declare both variables as String a = "getlam" b = "strak" Dim wait As New WebDriverWait wait.Timeout = 10 ' Wait for username input and send keys wait.Until (Condition:=Function(d) d.FindElementByXPath(".//*[@id='loginForm']/div[1]/div[1]/input").IsDisplayed) driver.FindElementByXPath(".//*[@id='loginForm']/div[1]/div[1]/input").SendKeys a ' Wait for password input and send keys wait.Until (Condition:=Function(d) d.FindElementByXPath(".//*[@id='loginForm']/div[1]/div[2]/input").IsDisplayed) driver.FindElementByXPath(".//*[@id='loginForm']/div[1]/div[2]/input").SendKeys b ' Wait for submit button and click wait.Until (Condition:=Function(d) d.FindElementByXPath(".//*[@id='Submit_button']").IsDisplayed) driver.FindElementByXPath(".//*[@id='Submit_button']").Click ' Switch back to Excel and update sheet AppActivate Application.Caption Sheets("YourSheetName").Range("A1").Value = "Login completed successfully" ' Clean up driver instance driver.Quit Set driver = Nothing End Sub
内容的提问来源于stack exchange,提问作者Bhaskar Ravuri

