B4J Library Selenium Browser Automation - Create any Robot with Browser



Selenium WebDriver - Mastering Test Automation with Selenium

What is Selenium?
Selenium is an open source automated testing suite for web applications across different browsers and platforms. It is quite similar to HP Quick Test Professional (QTP) only that Selenium focuses on automating web-based applications.

Selenium is not just a single tool but a suite of software's, each catering to different testing needs of an organization.

Based on Selenium WebDrivers, this new Library for B4J can automate browsers and create any robot and get any information from websites.

And you can use with or Without UI



Step 1:
Download the library and dependencies and put in your B4J Library Folder

https://visualnet.ddns.me/vncorp/store




Step 2:
Download the ChromeDriver for your platform.
Can be used on Windows/Linux/MacOSX, just download correct driver for your platform on:

http://chromedriver.chromium.org/downloads

TIP: Inside the samples you already have ChromeDriver.exe for windows


Step 3:
Try the samples


Sample 1: (SampleNasdaq.zip) Open Nasdaq.com website and get the top stocks on market

Sample 2: (SampleOyster.zip) Open UK TLF website for retrieve Oysters card and balance from each one

Check with a few lines of code you can retrieve a table from nasdaq website
B4X:
Dim objSelenium As Selenium
objSelenium.DebugMode = True
objSelenium.Initialize("chromedriver.exe","objSelenium")
objSelenium.Navigate("https://www.nasdaq.com/")
Dim lstElements As List = objSelenium.findElementList("indexTable")
For Each k As String In lstElements
      Log(k)
Next


Check others libraries on:

https://visualnet.ddns.me/vncorp/store
 
Last edited:

Harris

Expert
Licensed User
Longtime User
Wee bit confused here.

Can I use this - for example:

Run this bot against my ( low powered VPS - web app (ABM) - stress test ) and determine what the maximum hit rate would be (before failure) - and what I need to upgrade the power of my VPS too (suggested)?

This has always been a concern for me, since I have no idea what "level" of VPS power I need to handle expected load. I start with cheapest, since it is only me testing.

Thanks
 

marcellissimo

Member
Licensed User
Longtime User
Hi Alberto, I'm trying your library but objSelenium.ClickByXPath doesn't seem to work, work like objSelenium.Click
 

marcellissimo

Member
Licensed User
Longtime User
thanks for the quick response

link privacy from google ...
for example:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim objSelenium As Selenium
    Private robot As JavaObject
    Private Button1 As Button
    Private Button2 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("btn") 'Load the layout file.
    MainForm.Show
    objSelenium.DebugMode = True
    objSelenium.Initialize("chromedriver.exe","objSelenium")

    Dim jo As JavaObject
    jo.InitializeStatic("com.sun.glass.ui.Application")
    robot = jo.RunMethodJO("GetApplication",Null).RunMethodJO("createRobot",Null)
    objSelenium.Navigate("https://www.google.com")
   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
    '//*[@id="fsr"]/a[1] <- from google copy xpath
    objSelenium.ClickByXPath("//*[@id=""fsr""]/a[1]")
    ' error : ->org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"//*[@id="fsr"]/a[1]"}
   
   
   
End Sub



'/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]

Sub Button2_Click
    '/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1] <- from firefox copy xpath
    objSelenium.ClickByXPath("/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]")
    ' error : ->org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]"}
End Sub


errore...
B4X:
apr 23, 2019 1:33:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMAZIONI: Detected dialect: OSS
[Java][EventB4]onInitialized
[Java][Navigate]URL=https://www.google.com
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][EventB4]onNavigate
[Java][DEMO] Remember this is a demo version of this library, please consider buy me a coffe. Paypal: alberto@vnsoft.es
[Java][ClickByXPath]ElementID=/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]
Errore nella linea: 49 (Main)
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]"}
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
System info: host: 'DESKTOP-E1RMGQ1', ip: '192.168.1.38', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 73.0.3683.68 (47787ec04b6e3..., userDataDir: C:\Users\Marcy\AppData\Loca...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:54876}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.103, webStorageEnabled: true}
Session ID: 650a3682eff78d20532e7d9d1eede32d
*** Element info: {Using=id, value=/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1]}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
@marcellissimo Hello my friend, sorry, I receive just today your message.

You right, find by xpath I found one bug on library and I already fixed on version v1.0.1

http://vnsoft.es/download/b4j/selenium/LibrayB4J_v1.0.1.zip

but for me, the xpath is:
B4X:
    objSelenium.ClickByXPath("/html/body/div[1]/div[8]/div[1]/div/div/div/div/span[1]/a[1]")

Check this video:
 

aeric

Expert
Licensed User
Longtime User
I tested both buttons are working now. Thanks for the example.
B4X:
Sub Button1_Click
    '//*[@id="fsr"]/a[1] <- from google copy xpath
    objSelenium.ClickByXPath($"//*[@id="fsr"]/a[1]"$)
End Sub

Sub Button2_Click
    '/html/body/div/div[7]/div[1]/div/div/div/div/span[1]/a[1] <- from firefox copy xpath
    objSelenium.ClickByXPath($"/html/body/div[1]/div[8]/div[1]/div/div/div/div/span[1]/a[1]"$)
End Sub
 

tchart

Well-Known Member
Licensed User
Longtime User
Alberto, the SendKeys function seems to want the element name and not the id. Can you please add a second method for SendKeys by ID?
 

fabio borges

Member
Licensed User
Longtime User
How to use headless mode?

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…