I'm trying to grab the innerHTML if a popup comes up while doing a task in chrome and then based on the innerHTML either click okay and save or exit. The problem where I am running into is just before the possible pop-up I am in an iframe and need to switch back to the main body. I'm using selenium:
main body (possible pop_up *i think*)
iframe (I am here)
Below is some of the code I have, taken out the non-necessary lines:
Dim sel As Selenium.ChromeDriver
Dim pop as Object
Set sel = New Selenium.ChromeDriver
sel.Get URL
' Switch to iframe
Set iframe = sel.FindElementByTag("iframe", 10000)
sel.SwitchToFrame iframe, 5000
code...
' Where the popups sometimes show up
On Error Resume Next
sel.SwitchToDefaultContent
Set pop = sel.FindElementsByClass("popupText", 2000) *Error Could not locate class=popupText
On error goto 0
I get this error if I turn off on error resume next even if there are popups. Below is the HTML:
<span class="popupText">
<br />
*Text*
<br />
<br />
*Text*
</span>
I've tried a couple of other things: not using switchtodefaultcontent, and using switchtoparenframe and nothing seems to work.
There is also a .SwitchToAlert feature in selenium that is not working either...
strAlert = sel.SwitchToAlert.Text 'Get nothing
sel.SwitchToAlert.Accept 'Does not do anything
Figured out a workaround to this, it seems like there is an issue with the selenium vba add-in. The workaround:
sel.SwitchToDefaultContent
inner_text = Right(sel.PageSource, 1000)
Instead I grabbed the page_source after switching to default content (the popup text happened to be at the very end of the html) I then continue to check if a popup (4 different pop_up options) is in the inner_text variable. Then continue from there.
The reason I think this is an issue with the VBA Selenium add-in is I replicated the above code with Python and it worked great... so not sure what was going on there. VBA Selenium is dated about 4 years.
Related
I'm having trouble getting the inner text of a web element on this page (Using VBA + Selenium): https://www.accuweather.com/en/ch/geneva/313082/daily-weather-forecast/313082?day=2
I'm trying to get the text (The "20°") in the following element:
<div class="temperature" style="">20°</div>
I tried using mystring = driver.FindElementByCss(".row.first > .temperature:nth-of-type(2)").text but for some reason it doesn't seem to work, it returns an empty value in Excel, as if it's not getting anything from the element.
I'd appreciate any help, I've been trying to get this working for a little while now.
To get the inner text of a web element i.e. 21° using VBA and Selenium you can use either of the Locator Strategies:
FindElementByXPath:
mystring = driver.FindElementByXPath("//h2[#class='title' and text()='Day']//following-sibling::div[#class='temperature']").text
FindElementByCss:
mystring = driver.FindElementByCss("div.content-module div:nth-of-type(2) div.temperature").text
I would like to take a picture using a webcam from excel macros, and I am a complete beginner when it comes to excel. I tried to follow other sources on the internet, but I can't even type in "Public Class Form1" without getting an error. What I did was I inserted a button into a spreadsheet, clicked "New," then replaced the sub data that comes with the button with code I found on the internet, and I just have tons of red lines throughout the code.
I looked at this post and its answers, but none of it works because, as I've said, excel doesn't want to work. If it's possible for you guys to give me more baby-step instructions (use this button and not that button, you gotta type "Public Class Form1" then pray to the gods for it to work instead of just pressing enter, stuff like that), that would help me greatly.
Visual Basic Connect to Webcam and Save Picture to File
Me not knowing how to terminate line. I tried &HD and &HA since that's a solution from microsoft, but that obviously didn't work.
The answer is don't try to do the whole thing from excel. In excel, assign a macro to a button. The code in that macro will be:
Sub RunPython()
Dim objShell As Object
Dim PythonExe, PythonScript As String
Set objShell = VBA.CreateObject("Wscript.Shell")
PythonExe = """PATH TO PYTHON.EXE"""
PythonScript = "PATH TO PYTHON CODE THAT NEEDS TO BE RUN"
objShell.Run PythonExe & PythonScript
End Sub
In Python 3, you, use the following code:
from cv2 import *
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test", WINDOW_NORMAL)
imshow("cam-test",img)
waitKey(1000)
destroyWindow("cam-test")
imwrite("filename.jpg",img) #save image
Credit:
Using Excel to use Python: https://stackoverflow.com/a/60658227/9582521
Using Python to use Webcam: https://stackoverflow.com/a/11094891/9582521
I'm creating a program through Microsoft Excel VBA using Selenium Chromedriver. The program aims to take information from a Spreadsheet, create links, then open the pages and fill form fields with information from the spreadsheet. I've been able to get most all of the functionality to work, with one annoying hiccup. Each time the program opens a new link, the website signs me out of my profile and returns it to the default Guest profile.
Sub startingout()
Dim URL As String
Dim driver As New WebDriver
driver.Start "Chrome", ""
driver.Get "https://website.com/default.aspx"
driver.FindElementByName("ctl00$LoginForm$UserName").SendKeys ("username")
driver.FindElementByName("ctl00$LoginForm$Password").SendKeys ("password")
driver.FindElementByName("ctl00$LoginForm$LoginButton").Click
URL = "https://website.com/specificURL"
Call secondtest(driver, URL)
End Sub
Sub secondtest(driver As WebDriver, URL As String)
driver.Get URL
driver.FindElementByName("ctl00$ContentPlaceHolder1$lvItems$ctrl0$TextBoxQty").SendKeys ("1")
driver.FindElementByName("ctl00$ContentPlaceHolder1$lvItems$ctrl0$btnAddCart").Click
Application.Wait (Now + TimeValue("0:00:01"))
driver.FindElementByName("ctl00$ContentPlaceHolder1$btnContinue").Click
Application.Wait (Now + TimeValue("0:00:1"))
End Sub
Above is the test code that works fine. I stay logged in. The actual program is a sub procedure that calls one of three other sub procedures when the link is created. Below is how I began the main sub procedure, to load up the website.
Dim driver As New WebDriver
driver.Start "Chrome", ""
driver.Get "https://website.com/default.aspx"
driver.FindElementByName("ctl00$LoginForm$UserName").SendKeys ("username")
driver.FindElementByName("ctl00$LoginForm$Password").SendKeys ("password")
driver.FindElementByName("ctl00$LoginForm$LoginButton").Click
Once the link is created, it moves to another sub procedure that starts as follows:
Sub FullBar(URL As String, Quantity As Integer, CurRow As Integer, driver As
WebDriver)
Dim gotit As Boolean
gotit = False
driver.Get URL
Application.Wait (Now + TimeValue("0:00:01"))
Now, it does not seem that these two cases are fundamentally different, yet they act like they are. The strange thing about this is that when I create a test program using just VBA and some test links, I am able to stay logged in just fine. Why is this, and what makes the two situations different?
I didn't really figure out why this is happening, but I did find a solution. I followed this page:
https://www.reddit.com/r/vba/comments/a63c2k/open_chrome_and_set_profile_with_chromedriver/
I created a new chrome profile and loaded from there, adding the
bot.addArguments("--no-sandbox")
to the code. It could be that the Selenium doesn't like using the default program or something. Anyway, I got it to work and I stay logged in.
I'm currently trying to learn VBA in Excel and I've written a simple procedure where I should be able to launch Chrome and click on a button. Problem is that I'm getting a Run-time error '0': KeyNotFoundError Dictionary key not found:status whenever I try to use the .click method.
I downloaded the Selenium Basic from the following site https://florentbr.github.io/SeleniumBasic/ activated the reference in VBA and downloaded the latest chromedriver
Sub driver()
Dim driver As New Selenium.WebDriver
Set driver = New Selenium.WebDriver
driver.Start "chrome"
driver.Get "http://www.google.com"
Set Element = driver.FindElementByName("btnI")
Element.Click
End Sub
This is just a simple code where I launch Chrome and go to Google and click the "I feel Lucky" button.
A quick test for firing that button and I needed to use javascript
driver.ExecuteScript "document.querySelector('[name=btnI]').click();"
If the error remains I suggest checking chromedriver.exe is the right version for your Chrome browser and do a selenium re-install and check the selenium type reference as usual in the VBE.
The latest version of Chrome is throwing "KeyNotFoundError Dictionary key not found:status" error.
Try replacing your Chromedriver with version 2.46. Here is the link: https://chromedriver.storage.googleapis.com/index.html?path=2.46/
Below is the code I have tried and it worked perfectly fine for me. Let us know if this solution worked for you! :)
Sub driver()
Dim driver As New Selenium.WebDriver
Set driver = New Selenium.WebDriver
driver.Start "chrome"
driver.Get "http://www.google.com"
driver.FindElementByCss("#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input[type=submit]:nth-child(2)").Click
End Sub
I wrote a macro on VBA using Selenium.ChromeDriver, which opens the site and gives it various actions.
But the problem is that if an error occurs in the macro and I click "Stop" in debugging mode, then the browser closes.
The problem is that when you close the browser and open it, you have to enter your login and password each time.
How can I avoid closing the browser opened via Chrome Driver if the macro ends or when an error occurs?
Did so
Public Driver as new ChromeDriver
but that doesn't help, the browser closes.
Need some more code really but some pointers
When error occurs click debug rather than end and the browser should remain open
Don't use .Quit if you want automated browser to remain open at end of sub. However, you will need to grab the session id if you wish to reconnect with that instance, or pass the driver object to any receiving sub. Just remember at some point to Quit.
Here is how you open another tab:
Option Explicit
'download selenium https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
'Ensure latest applicable driver e.g. ChromeDriver.exe in Selenium folder
'VBE > Tools > References > Add reference to selenium type library
Public Sub DownloadFile()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "www.something/login/"
With d
.Start "Chrome"
.get URL
.FindElementById("name").SendKeys ""
.FindElementById("email").SendKeys ""
.FindElementByCss("[type=submit]").Click
Application.Wait Now + TimeSerial(0, 0, 5) '< better to have a wait condition for something on the post login page
.ExecuteScript "window.open(" & Chr$(34) & "postloginUrl" & Chr$(34) & ",'_blank');"
Stop
.Quit
End With
End Sub