Using Selenium and VBA to navigate Chrome, but getting popup: Loading of unpacked extensions is disabled by the administrator - vba

I am trying to use selenium to open Chrome, but I am getting an popup that I can't resolve. It seems a solution has been found using JAVA, but I can't easily find something for VBA. In case the image doesn't load, it says "Failed to load extension from: <>local location<>. Loading of unpacked extensions is disabled by the administrator."
The closest thing I have found is this line, but it doesn't seem to do anything.
.SetPreference "profile.default_content_setting_values.notifications", 2
Here is the full code:
Option Explicit
Public Sub Demo()
Dim d As WebDriver
Set d = New ChromeDriver
Const URL = "https://www.google.com/"
With d
.SetPreference "profile.default_content_setting_values.notifications", 2
.Start "Chrome"
.Get URL
.FindElementById("lst-ib").SendKeys "Selenium basic GitHub"
.FindElementsByTag("form")(1).FindElementByCss("input[value='Google Search']").Click
'.Quit
End With
End Sub

Related

Selenium Webdriver and Embedded Browser in a Winform - How to connect web_driver instance?

I need to connect/create an instance of Selenium Webdriver to web content that my application under test opens in a Winform.
My test framework opens the Winform application with CodedUI, and further test steps cause the Winform application to open another Winform window that has a browser embedded in it. I need to create or tie a Selenium instance to this embedded browser content. It's using WebViewer/Edge in the embedded area.
Note - Not yet ready to move to WinAppDriver, if ever.
Resolved this. I found out that the web pane is an object called a WebView2.
Automating inside it requires the application to be launched in a special way:
Dim AppPath As String = "[full path to your app and its name].exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim AppStartInfo = New ProcessStartInfo(AppPath)
AppStartInfo.UseShellExecute = False
AppStartInfo.EnvironmentVariables("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS") = "--remote-debugging-port=9222"
Diagnostics.Process.Start(AppStartInfo)
You also need:
Selenium Support and Selenium Webdriver version 4.0.0-rc3 installed in the projects
msedgedriver from here: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
And finally in your automation code, when the window with WebView2 in it finally appears, you need to run this code to tie selenium webdriver to the WebView2 pane:
Dim driver As String = "[path to:]msedgedriver.exe"
Dim edgeOptions As New EdgeOptions
edgeOptions.UseChromium = True
edgeOptions.UseWebView = True
edgeOptions.DebuggerAddress = "localhost:9222"
Dim WebDriver = New EdgeDriver(EdgeDriverService.CreateDefaultService(".", driver), edgeOptions)

How to enable IE mode in Chromium Edge Browser in selenium C#?

I want to automate a website in Edge which is require IE mode to be enabled. How can launch Edge in IE mode in selenium?
Below code which I currently use launches Edge in non IE mode, which won't display the website properly.
Dim edgeDriverService = Microsoft.Edge.SeleniumTools.EdgeDriverService.CreateChromiumService()
Dim edgeOptions = New Microsoft.Edge.SeleniumTools.EdgeOptions()
edgeOptions.PageLoadStrategy = PageLoadStrategy.Normal
edgeOptions.UseChromium = True
Dim driver As IWebDriver = New Microsoft.Edge.SeleniumTools.EdgeDriver(edgeDriverService, edgeOptions)
driver.Navigate().GoToUrl("http://example.com")
Tried using edgeOptions.AddAdditionalCapability("ie.edgechromium", True)but it didn't work
You could refer to the section Automating Internet Explorer mode in this article about how to use IE mode in Edge Chromium in Selenium C#.
You could refer to the following steps:
Download the latest version of IEDriverServer from Selenium site. Here I use 32 bit Windows IE version 3.150.1.
Make some preparations to use IEDriver according to this.
Create a C# console project using Visual Studio.
Install Selenium.WebDriver 3.141.0 nuget package from Nuget package manager.
Add the code below to the project and modify the paths to your owns in the code:
static void Main(string[] args)
{
var dir = "{FULL_PATH_TO_IEDRIVERSERVER}";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieService = InternetExplorerDriverService.CreateDefaultService(dir, driver);
var ieOptions = new InternetExplorerOptions{};
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", "{FULL_PATH_TO_MSEDGE.EXE}");
var webdriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
webdriver.Url = "http://www.example.com";
}
Run the project to test:
Notes:
Make sure to close all the Edge browser tabs and window before running the code.
Use full paths in the code. For example: ieOptions.AddAdditionalCapability("ie.edgepath", #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");.

Set Profile Chrome WebDriver Selenium VBA

I'm trying to set a profile in chrome, but it does not work.
Chrome Version: Version 80.0.3987.106
WebDriver: ChromeDriver 80.0.3987.106
Selenium Basic: SeleniumBasic-2.0.9.0.exe
Option Explicit
Public Sub openGChrome()
Dim obj As New WebDriver
Dim i As Integer
Const URL = "https://www.linkedin.com/feed/"
Const JS_PROFILE As String = "C:\Users\ChuckNorris\AppData\Local\Google\Chrome\User Data\Default"
Set obj = New ChromeDriver
With obj
.SetProfile JS_PROFILE, True
.Get URL
Stop
.Quit
End With
End Sub
What i'm doing wrong? Any help?
Edit:
The browser open, but i dont have any profile select, i can't login to websites without signin
You did not specify what "does not work", but I think you should take away the \Default from your profile path, because when you don't specify the name of the profile, meaning you want to use the default one, the \Default is then added implicitly.
To specify a certain profile other than the default, you would want to use:
.AddArgument ("profile-directory=foldername") 'foldername is the name of the folder with the profile you want to use

AttributeError: module 'selenium.webdriver' has no attribute 'switch_to_alert'

I am making a simple crawler that can open a site and when a pop up appears, it should close it. but the following command isn't working.
from selenium import webdriver
browser = webdriver.Chrome(executable_path=r"C:\Program Files\chromedriver.exe")
url = "https://www.bnbaccessories.com/"
browser.get(url)
alert = webdriver.switch_to_alert().dismiss()
innerHTML = browser.execute_script("return document.body.innerHTML")
browser.implicitly_wait(50)
browser.close()
Use this
alert = browser.switch_to.alert.dismiss()
instead
webdriver.switch_to_alert().dismiss()
driver instance name is browser not webdriver

Cannot locate element by Xpath for chrome download page

I am trying to write code to check download is completed by selenium and chrome driver. My idea is
1.Go to download page("chrome://downloads/")
2.Check the url to ensure we have downloaded file from that site (locate http://xxxxxxxx)
3.Check download status( If I found "show in folder", it implies download success. if not, download failed)
I am stucking in step2, when I try to locate the url. I used developer tools, move cursor to the url to locate the element,and then I right click and copy the xpath. The xpath is like this
//*[#id="file-link"]
And then I try to click ctrl+F in developer tool and paste the xpath again. I cannot locate the element. Why? Checked there is no frame.
It shadow DOM and need to select /deep/ using CSS selector
downloads-manager/deep/downloads-item/deep/a[id="file-link"]
Another way of doing this could be writing a small Java utility for the same using File Class.
Something like this:
File f = new File("C:\\Users\\username\\Downloads\\Users_" + fielName+ ".xls");
Assert.assertTrue(f.exists());
This work for me on python:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# disable file preview
options = webdriver.ChromeOptions()
options.add_experimental_option(
'prefs', {
'profile.default_content_settings.popups': 0,
'download.default_directory': '/some/download/dir',
'download.prompt_for_download': False,
'download.directory_upgrade': True,
'plugins.always_open_pdf_externally': True,
'plugins.plugins_disabled': 'Chrome PDF Viewer',
'disable-popup-blocking': True
}
)
driver = webdriver.Chrome(options=options)
# start load file
driver.get('url-to-file')
def condition_load_file(dr: webdriver.Chrome):
return dr.execute_script("""
try {
var el = document.querySelector("body > downloads-manager").shadowRoot.querySelector("#frb0").shadowRoot.querySelector("#show");
return el !== null;
} catch(exc) {
return false;
}
""")
# open new window and go to chrome://downloads/
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get('chrome://downloads/')
# waiting for download to finish
WebDriverWait(driver, 120).until(condition_load_file)