Cannot Login with Excel VBA Selenium - vba

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

Related

Handle login with user and password using selenium and 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.

How to click HREF using Selenium

Trying to click a download button on Chrome using Selenium Type Library. The code below is one that I picked up off the boards but I am receiving a Syntax Error
Sub Test()
Dim bot
Set bot = CreateObject("Selenium.WebDriver")
bot.Start "Chrome", "https://www.afterpaytouch.com"
bot.get "/results-reports"
bot.findElement(By.linkText("https://www.afterpaytouch.com/images/28082019-FY2019-Results-Presentation.pdf")).click()
End Sub
I would add a reference to Selenium Type Library in VBE > Tools > References then use early bound reference, full url and apply VBA selenium basic syntax to find link by css and click
Option Explicit
Public Sub Test()
Dim bot As WebDriver
Set bot = New ChromeDriver
With bot
.Start "Chrome"
.get "https://www.afterpaytouch.com/results-reports"
.FindElementByCss("[href='https://www.afterpaytouch.com/images/28082019-FY2019-Results-Presentation.pdf']").Click
Stop '<delete me later
End With
End Sub

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"

Trouble clicking on a load more button using vba

I'm trying to click on the Load More button located at the bottom of the left window of this webpage using vba in combination with selenium but the script always throws timeout error pointing at this .Get Url line. Although it seems I've defined an accurate xpath to locate the element, I can't think further as to what I should do now to achieve the same.
How can I click on that Load More button?
Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.Get Url
Set post = .FindElementByXPath("//div[contains(.,'Load More')]")
.ExecuteScript "arguments[0].scrollIntoView();", post
post.Click
End With
End Sub
I see two "Load More" buttons. Both are matched by "//div[contains(.,'Load More')]". The first one is hidden. You need to handle second one.
Try this XPath
"//div[#class='content' and . = 'Load More']"
At least for me there were a couple of banners to dismiss as well as scrolling. There was no problem with the get line
Option Explicit
Public Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.get Url
If .FindElementsByCss(".close-notice.close-this").Count > 0 Then
.FindElementByCss(".close-notice.close-this").Click
End If
.SwitchToFrame .FindElementByCss("[id^='spout-unit-iframe']")
With .FindElementByCss("#spout-ads #spout-header-close")
.ScrollIntoView
.Click
End With
.SwitchToDefaultContent
.ExecuteScript "document.querySelector('.result-list [onclick*=LoadMore]').scrollIntoView(true);" & _
"window.scrollBy(0, -(window.innerHeight - this.clientHeight) / 2);"
.FindElementByCss(".result-list [onclick*=LoadMore]").Click
Stop '<== Delete me later
'other code
.Quit
End With
End Sub

How to click the date picker selenium VBA?

I'd like to use selenium VBA to download some data from Yahoo Finance KOSPI COmposite Index.
I got the difficulty when click the date picker arrow to get the mini window to select the end date as today. I tried to record the marco through selenium IDE in chrome, but IDE does not record the step when I click the arrow of the Time period to get the date picker visible.
Below is my code in VBA.
Public Function seleniumKorea(bot As WebDriver)
Dim url As String
url = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d"
bot.Start "chrome", url
bot.Get "/"
'Not sure how to add date picker here
bot.FindElementByName("endDate").Clear
bot.FindElementByName("endDate").SendKeys (Date)
bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='End Date'])[1]/following::button[1]").Click
Application.Wait (Now + TimeValue("0:01:00"))
bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='As of'])[1]/following::div[4]").Click
Application.Wait (Now + TimeValue("0:01:00"))
bot.FindElementByXPath("(.//*[normalize-space(text()) and normalize-space(.)='Currency in KRW'])[1]/following::span[2]").Click
Application.Wait (Now + TimeValue("0:01:00"))
End Function
I tried to use the ByXPath to get the svg class but failed.
Thanks in advance.
I would use the following which submits the OATH consent if required and scrolls the date picker into the viewport
Option Explicit
Public Sub DatePicking()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d/"
With d
.get URL
If .Title = "Yahoo is now part of Oath" Then
.FindElementByCss("form").submit
End If
With .FindElementByCss("[data-test='date-picker-full-range']")
.ScrollIntoView
.Click
End With
With .FindElementByCss("[name=startDate]")
.Clear
.SendKeys "05/10/2017"
End With
With .FindElementByCss("[name=endDate]")
.Clear
.SendKeys "05/10/2017"
End With
Stop '<==Delete me later
.Quit
End With
End Sub
Check this out. It should serve the purpose. I used xpath to solve the issue.
Sub CustomizeDate()
Const Url$ = "https://finance.yahoo.com/quote/%5EKS11/history?period1=1484018309&period2=1515554309&interval=1d&filter=history&frequency=1d"
Dim driver As New ChromeDriver
With driver
.get Url
.FindElementByXPath("//input[#data-test='date-picker-full-range']", timeout:=5000).Click
.FindElementByXPath("//input[#name='startDate']").Clear.SendKeys ("01/05/2017")
.FindElementByXPath("//input[#name='endDate']").Clear.SendKeys ("01/08/2017")
.FindElementByXPath("//button/span[.='Done']", timeout:=5000).Click
.FindElementByXPath("//button/span[.='Apply']", timeout:=5000).Click
End With
End Sub
Don't use hardcoded delay for any item to appear. Use Explicit Wait instead. The script will wait upto 5000, meaning 5 seconds for that item to be available.