Handle login with user and password using selenium and VBA - vba

i have a trouble to authenticate my credentials using selenium with a chrome driver and VBA.
I need to download a file from a webpage which need that user put its credentials.
I read something in some forums and i could got this code:
Sub test()
Set ch = New Selenium.ChromeDriver
ch.AddArgument "start-maximized" ' Maximizar la pantalla del navegador
ch.Get ("http://gremio:#Dialer2022#https://www.dialer.com.ar/lista/archivos
/Lista_Gremio_Dialer.xlsx")
End Sub
When I run the code, I get a web page access error:
https://i.postimg.cc/wT0nTyk4/AAA.png
https://i.postimg.cc/T3cjXgqh/dd.png
If i put only the webpage : https://www.dialer.com.ar/lista/archivos/Lista_Gremio_Dialer.xlsx
i can get in on the webpage and then i have to put my credentials:
https://i.postimg.cc/2S19kHy3/bb.png
and the the user and password:
https://i.postimg.cc/hG9rvNB4/cc.png
But i need automatize the download routine with selenium and VBA.
For example if i try with another page with the same code, works fine.
Sub test()
Set ch = New Selenium.ChromeDriver
ch.AddArgument "start-maximized" ' Maximizar la pantalla del navegador
ch.Get ("http://test:test#browserspy.dk/password-ok.php")
End Sub
I think that the problem is about pop up on the page.
I really appreciated some kind of help, thank you very much.

Related

Error thrown trying launch a webpage from a button

I created a button and a menu item in vb.net (.NET 6). I found several answers here on SO that say the process to launching a webpage from such an event can be launched with this code:
Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)
However, trying launch the code, I'm given the error of "system cannot find the file specified".
Looking more into it, I know that .NET 6 is running a bit differently and changed the code to the following:
Using link As New Process()
link.StartInfo.UseShellExecute = True
link.Start(New ProcessStartInfo("https://example.com"))
End Using
But still to no avail, and I am given the same error. "System cannot find the file specified." I can run addresses via the regular Windows Run prompt... but the program still cannot launch.
Following Jimi's comment to my original question, I changed the Sub to the following:
Sub LaunchWebsite(strWebpageURL As String)
Using Process.Start(New ProcessStartInfo(strWebpageURL) With {.UseShellExecute = True})
End Using
End Sub
Using this, the webpage launched in my desktop's default browser with no problem.
You can use
Respone.Redirect("http://www.example.com/")
or use javascript in server side code
Dim url As String = "http://www.example.com"
Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
Above code open webpage in new popup window.
Regards
Aravind

Printing a webpage using selenium in VBA

I am trying to print a webpage using selenium in VBA. I want the bot to be able to press control+P and then Enter on the printing dialog but keep getting an object required error. Anyone have any ideas?
Sub printing()
Dim bot As New WebDriver, controlP As Variant, enter As Variant
bot.Start "chrome", ""
bot.Get "https://www.google.com/"
controlP = (Keys.Control & "p")
enter = (Keys.enter)
bot.FindElementByXPath("/html").SendKeys (conrolP)
bot.FindElementByXPath("/html").SendKeys (enter)
End sub
I am not sure the approach you are trying is the way to achieve your goal. However, here are some points:
I think the relevant xpath should be //html.
Use Option Explicit as first line of your module code; You have a typo with conrolP which Option Explicit would have alerted you to as an undeclared variable.
You also don't need those () with SendKeys.

Can't set chrome preferences in vba selenium

I've created a script in vba in combination with selenium to parse the first headline from this webpage. Most of the times my script throws this error timeout or this error Run-time error 21; Application defined or Object defined error while sometimes it works flawlessly. As the page takes too much time to load it's content, I suppose I'm having one of the side effect of a slow loading page, so I wish to disable images from that page.
I've tried with:
Sub TestSelenium()
Const URL$ = "https://www.marketscreener.com/"
Dim driver As Object, post As Object
Set driver = New ChromeDriver
driver.get URL
Set post = driver.FindElementByCss(".une_title")
MsgBox post.Text
driver.Quit
End Sub
When I go for python selenium binding, I can use this option to disable images:
option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
driver = webdriver.Chrome(options=option)
I know there are options to set different preferences in vba but in case of disabling images I can't find any proper way to set them:
driver.SetPreference
driver.AddArgument
How can I set chrome preferences in vba selenium to let the page load quickly without images?
To disable images from that page, this is how you can set the preference within vba selenium bindings:
driver.SetPreference "profile.managed_default_content_settings.images", 2
Your script looks like the following when you implement the above suggestion:
Sub TestSelenium()
Const URL$ = "https://www.marketscreener.com/"
Dim driver As Object, post As Object
Set driver = New ChromeDriver
driver.SetPreference "profile.managed_default_content_settings.images", 2
driver.get URL
Set post = driver.FindElementByCss(".une_title")
MsgBox post.Text
Stop
driver.Quit
End Sub
You could always try running it headless? That should remove any delay associated with image loading.
driver.AddArgument "--headless"

Cannot Login with Excel VBA Selenium

So I am tryin to create a Script that will automatically login for me on this website:
https://teespring.com/login
This is my script so far:
Sub openBrowser()
'Open new browser
Set driver = New Selenium.ChromeDriver
'Navigate to Website
urlWebsite = "https://teespring.com/login"
driver.Get urlWebsite
So I have tried to enter my username with the following lines of code:
driver.FindElementByCss("").sendKeys Username
But I got an error saying element not visible
Is there any way I can still automate the login process?
thanks for your help and if you need further information I will try my best to as I am still learning how to handle vba selenium :-)
It is not visible because it is inside a form.
You need to access via form:
Option Explicit
Public Sub EnterInfo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://teespring.com/login"
With d
.Start "Chrome"
.get URL
With .FindElementByClass("js-email-login-form")
.FindElementByCss("input[name=email]").SendKeys "myEmail"
.FindElementByCss("input[name=password]").SendKeys "myPassWord"
.FindElementByCss("input[type=submit]").Click
End With
Stop '<== Delete me after inspection
.Quit
End With
End Sub

VBA : How to set the value to a textarea in an object type of internetexplorer.application

I've been trying to write some text into a textarea element of an aspnet form using the object internetexplorer.application in a macro using VBA
When I add a watch to the object after setting the "Value" attribute, i get the text that i am setting (using a MsgBox), but the browser doesnot seem to get the text when i submit it.
Im using IE 8.0.6001 and Office 2003.
Any help / comment will be very much appreciated.
Thanks
Sub Test2()
Dim Title As String
Dim Comments As String
Set wb = CreateObject("internetexplorer.application")
wb.navigate2 "http://tudrintra01/ads/Lists/Tasks1/NewForm.aspx?RootFolder=%2Fads%2FLists%2FTasks1&ContentTypeId=0x01030062FE73EDFA7DA644A27CE244EA983DA4&Source=http%3A%2F%2Ftudrintra01%2Fads%2FLists%2FTasks1%2FMyItems%2Easpx"
wb.Visible = True
Title = "TITULO PRUEBA"
Comments = COMENTARIO PRUEBA" '"Comentario"
Do Until wb.readyState = 4 ' wait for page to load
DoEvents
Loop
'Si el objeto es el Titulo del task
wb.Document.getElementByID("ctl00_m_g_c0644918_3730_4e2c_8434_7b760939e3d4_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").Value = Title
'Si el objeto es el Comment
wb.Document.getElementByID("ctl00_m_g_c0644918_3730_4e2c_8434_7b760939e3d4_ctl00_ctl04_ctl14_ctl00_ctl00_ctl04_ctl00_ctl00_TextField").Value = Comments
' Clean up
Set wb = Nothing
End Sub
You have not got an answer for days, so I comment on your question now. I successfully automated IE via the WebBrowser object, not via an Internetexplorer.Application object. Webbrowser will not open an IE application instance but just a "document window" in a Webbrowser control in a form in your Access application. So if you don't use Access or for some other reason this might not be a valuable hint for you.
This is not an answer to your question, that's why I waited some time to give my comment, but it's my best comment and I did everything possible with the DOM model through a wb object and never experienced a problem like yours that I could not solve. So you might want to try and rewrite your sub to using a Webbrowser.