Code is working well but after running several times it stuck with Run-time error '21'.
It cannot open webdriver with run-time error '21': as attached picture.
'bot. start "Chrome" which is the code that has the error.
Chrome version: 83.0.4103.97
Selenium: SeleniumBasic-2.0.9.0
enter image description here
Dim bot As New WebDriver
Dim ele As WebElement
Dim picEle As WebElement
Dim awbaLI As WebElements
Dim AWB As List
bot.Start "Chrome"
bot.Get "https://google.com"
The error message says that you Port (62982, according to your provided screenshot) is still in use and thus cannot be reused.
You must also close to bot instance in order to release the port again (this also explains why it works again when you restart your PC (because then all the ports are released automatically)
In this case this can be done by calling bot.Quit at the very end of your bot usage / script!
Related
Please help me to solve this problem, i have tried many ways since one month ago, but i always failed.
I use Windows 7, Service Pack 1, 64 bit - Microsoft Excel professionl plus 2019.
My Current Chrome version is 94.0.46, I have tried other versions as well.
I use Chromedrive version 94.0.4606 but also have tried 93 and 92 versions.
I have tried different versions of SeleniumBasic, from 2.0.2.0 to 2.0.9.0 .
Also Google Chrome is not running as an administrator.
What i do always is: I uninstll Selenium, install it again or its another versaion, download the same chromedrive version that my google chrome uses, copy the chromedrive into the Selenium folder, either in "program files" path or "users" path and start it. Also "selenium Type Library" is selected in VBA References.
I run a very simple code:
Sub TestSelenium()
Dim Mybrowser As Selenium.ChromeDriver
Set Mybrowser = New Selenium.ChromeDriver
Mybrowser.Start
End Sub
I always get same error:
Define Mybrowser as webdriver. Please try below code.
Sub TestSelenium()
Dim Mybrowser As new webdriver
Mybrowser.start "chrome"
Mybrowser.get "https://google.com"
Application.Wait Now+Timevalue("00:00:20")
End Sub
in order to kill a process, I usually use
Process.GetProcessesByName("process name")(0).Kill()
This does not work if I want to close Chrome, using "Chrome" as the process name.
I know that Google Chrome runs with more than one process, at least that's what my task manager shows, so I guess this is the problem: because there are several processes called "Chrome", vb.net is unable to close them all or just closes one which doesn't lead to Chrome closing.
What code could I use instead, to close ALL processes called "Chrome"?
Try using this
Dim i As Integer = Ubound(Process.GetProcessesByName("Chrome.exe"))
For x As Integer = i To 0 Step -1
Process.GetProcessesByName("Chrome.exe")(x) .Kill()
Next
I am using Selenium Webdriver to load a specific feature from an application, a rich text editor (actually a custom release of CKEditor) and the code below works perfectly for that... except that I would like to release Selenium objects (and geckodriver.exe/marionnette black cmd window) since the desired page was loaded. But either .Close(), .Quit() or .Dispose() methods will wipe out the Firefox window as well...
Is there a way to dismiss Selenium Webdriver and keep Firefox running by its own?
Thank you very much
Private Sub LoadResource()
Dim FFD As New OpenQA.Selenium.Firefox.FirefoxDriver()
'Set timeout of 60 seconds for steps to complete successfully
Dim WDW As New OpenQA.Selenium.Support.UI.WebDriverWait(FFD, TimeSpan.FromSeconds(60))
'navigate to login page
FFD.Navigate.GoToUrl("https://www.myapplication.com/login")
'Wait until application loads main page (this means login was successful)
WDW.Until(Function() FFD.Url = "https://www.myapplication.com/")
'Load built-in rich text editor Rich text
FFD.Navigate.GoToUrl("https://www.myapplication.com/editor?document=1080199")
'Wait for successful loading of the editor page
WDW.Until(Function() FFD.Url = "https://www.myapplication.com/editor?document=1080199")
'That's all.
'here I'd like to release Firefox to keep running and get rid of WebDriver's objects and resources, if possible.
End Sub
This is based on Kirhgoph's comment and seems to work well:
Private Sub LoadResource()
Dim FFD As New OpenQA.Selenium.Firefox.FirefoxDriver()
Dim GDP As Process = Process.GetProcessesByName("geckodriver").Last
Dim WDW As New OpenQA.Selenium.Support.UI.WebDriverWait(FFD, TimeSpan.FromSeconds(60))
FFD.Navigate.GoToUrl("https://www.myapplication.com/login")
WDW.Until(Function() FFD.Url = "https://www.myapplication.com/")
FFD.Navigate.GoToUrl("https://my.application.com/editor?document=1080199")
WDW.Until(Function() FFD.Url = "https://www.myapplication.com/editor?document=1080199")
GDP.CloseMainWindow()
GDP.WaitForExit()
FFD.Quit()
End Sub
I have VBA code in Excel that is supposed to login to a website and download some files using Selenium. I have my code working using the ChromeDriver and am trying to modify it to work with the PhantomJSDriver so I can do something else while the program runs (it runs for ~45 minutes). The issue is that when I try to have Selenium click on the login button I get a timeout error:
Run-time error '101':
WebRequestTimeout:
No response from the server within 30000 seconds
The interesting thing is that after it times out, I can use the immediate window to take a screenshot and it's clear that the button was clicked and the browser has advanced to the next page.
Dim D As New PhantomJSDriver
With D
.ExecuteScript ("window.resizeTo(1920,1080)")
.SendKeys MyKeys.Control, "0" 'Set zoom to 100% (causes errors if not 100%)
.Get "LoginPage.com"
.FindElementByName("username").SendKeys "UserName"
.FindElementByXPath("/html/body/div[#class='centreContent']/form[#id='loginForm']/input[#id='passwordDummy']").Click
.FindElementByXPath("/html/body/div[#class='centreContent']/form[#id='loginForm']/input[#id='password']").SendKeys "Password"
.TakeScreenShot.SaveAs "C:\Users\110SidedHexagon\Downloads\Capture.png" '<---Takes screenshot of login screen with uesername and password filled in
.FindElementByName("loginSubmitButton", 0.1).Click '<---Error occurs here
<--Using the immediate window taking a picture after the error breaks code execution shows login was successful-->
End With
It means that after the button is clicked, the new loaded page doesn't return a completed state within 30 seconds.
It could be due to a dead resource within the page.
You could try to increase the server timeout:
Dim driver As New PhantomJSDriver
driver.Timeouts.Server = 60000 ' 60 seconds
driver.Get "https://..."
driver.FindElementByName("loginSubmitButton").Click
Or you could define a timeout to load the page and skip the error:
Dim driver As New PhantomJSDriver
driver.Timeouts.PageLoad = 20000 ' 20 seconds
driver.Get "https://..."
On Error Resume Next
driver.FindElementByName("loginSubmitButton").Click
On Error Goto 0
To get the latest version in date working with the above example:
https://github.com/florentbr/SeleniumBasic/releases/latest
I am writing a macro that will scrape my company's internal SAP site for vendor information. For several reasons I have to use VBA to do so. However, I cannot figure out why I keep getting these three errors when I attempt to scrape the page. Is it possible that this has something to do with the UAC integrity model? Or is there something wrong with my code? Is it possible for a webpage using http can be handled differently in internet explorer? I am able to go to any webpage, even other internal webpages, and can scrape each of those just fine. But when i attempt to scrape the SAP page, i get these errors. The error descriptions and when they occur are:
800706B5 - The interface is unknown (occurs when I place breakpoints before running the offending code)
80004005 - Unspecified error (occurs when I don't place any errors and just let the macro run)
80010108 - The Object invoked has disconnected from its clients. (I can't seem to get a consistent occurrence of this error, it seems to happen around the time that something in excel is so corrupted that no page will load and i have to reinstall excel)
I have absolutely no idea what is going on. The Integrity page didn't make much sense to me, and all the research I found on this talked about connecting to databases and using ADO and COM references. However I am doing everything through Internet Explorer. Here is my relevant code below:
Private Sub runTest_Click()
ie.visible = True
doScrape
End Sub
'The code to run the module
Private Sub doTest()
Dim result As String
result = PageScraper.scrapeSAPPage("<some num>")
End Sub
PageScraper Module
Public Function scrapeSAPPage(num As Long) As String
'Predefined URL that appends num onto end to navigate to specific record in SAP
Dim url As String: url = "<url here>"
Dim ie as InternetExplorer
set ie = CreateObject("internetexplorer.application")
Dim doc as HTMLDocument
ie.navigate url 'Will always sucessfully open page, regardless of SAP or other
'pauses the exection of the code until the webpage has loaded
Do
'Will always fail on next line when attempting SAP site with error
If Not ie.Busy And ie.ReadyState = 4 Then
Application.Wait (Now + TimeValue("00:00:01"))
If Not ie.Busy And ie.ReadyState = 4 Then
Exit Do
End If
End If
DoEvents
Loop
Set doc = ie.document 'After implementation of Tim Williams changes, breaks here
'Scraping code here, not relevant
End Function
I am using IE9 and Excel 2010 on a Windows 7 machine. Any help or insight you can provide would be greatly appreciated. Thank you.
I do this type of scraping frequently and have found it very difficult to make IE automation work 100% reliably with errors like those you have found. As they are often timing issues it can be very frustrating to debug as they don't appear when you step through, only during live runs To minimize the errors I do the following:
Introduce more delays; ie.busy and ie.ReadyState don't necessarily give valid answers IMMEDIATELY after an ie.navigate, so introduce a short delay after ie.navigate. For things I'm loading 1 to 2 seconds normally but anything over 500ms seems to work.
Make sure IE is in a clean state by going ie.navigate "about:blank" before going to the target url.
After that you should have a valid IE object and you'll have to look at it to see what you've got inside. Generally I avoid trying to access the entire ie.document and instead use IE.document.all.tags("x") where 'x' is a suitable thing I'm looking for such as td or a.
However after all these improvements although they have increased my success rate I still have errors at random.
My real solution has been to abandon IE and instead do my work using xmlhttp.
If you are parsing out your data using text operations on the document then it will be a no-brainer to swap over. The xmlhttp object is MUCH more reliable. and you just get the "responsetext" to access the entire html of the document.
Here is a simplified version of what I'm using in production now for scraping, it's so reliable it runs overnight generating millions of rows without error.
Public Sub Main()
Dim obj As MSXML2.ServerXMLHTTP
Dim strData As String
Dim errCount As Integer
' create an xmlhttp object - you will need to reference to the MS XML HTTP library, any version will do
' but I'm using Microsoft XML, v6.0 (c:\windows\system32\msxml6.dll)
Set obj = New MSXML2.ServerXMLHTTP
' Get the url - I set the last param to Async=true so that it returns right away then lets me wait in
' code rather than trust it, but on an internal network "false" might be better for you.
obj.Open "GET", "http://www.google.com", True
obj.send ' this line actually does the HTTP GET
' Wait for a completion up to 10 seconds
errCount = 0
While obj.readyState < 4 And errCount < 10
DoEvents
obj.waitForResponse 1 ' this is an up-to-one-second delay
errCount = errCount + 1
Wend
If obj.readyState = 4 Then ' I do these on two
If obj.Status = 200 Then ' different lines to avoid certain error cases
strData = obj.responseText
End If
End If
obj.abort ' in real code I use some on error resume next, so at this point it is possible I have a failed
' get and so best to abort it before I try again
Debug.Print strData
End Sub
Hope that helps.