B4J Library Selenium Library - Pnd_Selenium

Selenium automates browsers. That's it!
What you do with that power is entirely up to you.
Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that.
Boring web-based administration tasks can (and should) also be automated as well.

Wrapper is based on Selenium Java v4.27.0 (November 25, 2024) from HERE.


Versions:

v0.70

  • Removed ActionSendNonTextKey, as it not needed anymore
  • Now you can send multiple special keys at once, for example CTRL+A (select all)
  • Selenium is no longer connected to the version of Chrome browser installed on your computer
  • New KeyCodes class for special keys
  • New example app
  • Selenium upgrade to v4.27.0

v0.80
  • New Cookies class
  • New example app

v0.81
  • New functions like: WebElementIsSelected, WebElementIsDisplayed, etc. for direct interaction with WebElement objects
  • Support for Alerts - modal dialogs
  • Better documentation
  • New example app

v0.85
  • Return list As Objects of WebElement, to be able to interact with WebElement inside list
  • New option: ExecuteAsyncScript
  • New timeout options: ImplicitlyWait, PageLoadTimeout and ScriptTimeout for ExecuteAsyncScript
  • ExecuteAsyncScript and ExecuteScript (synchronous) return String as value if return or callback statement exists
  • New example app

v0.90
  • New class LogLevel
  • Removed one old class
  • Now is possible to use nested WebElements
  • Every FindBy function as first argument can use WebElement object
  • New example apps


Examples:

Selenium1.zip

  • How to setup Chrome for Testing and Chrome Driver
  • Where to find and how to setup Argument for Chrome
  • Cookies: add, edit, delete, delete all
  • ExecuteScript, ExecuteAsyncScript
  • Fill form and submit form
  • General library usage examples

Selenium2.zip
  • How to get all links from the page with FindByTagNameList
  • How to get all links from the page with FindByXPathList
  • How to filter links
  • How to get all TD text from all TABLES from all pages
  • How to navigate from page to page without reopening already visited links

Download library from: https://www.dropbox.com/scl/fi/yqdu...ey=91dfe0o19u45xqsebd2j26yth&st=jheflmt1&dl=0

When you download Chrome For Testing as well as the Chrome Driver, make sure to extract everything into one folder.
chrome.exe and chromedriver.exe should be in same folder.
For Windows use:
Chrome For Testing: https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.204/win64/chrome-win64.zip
Chrome Driver: https://storage.googleapis.com/chrome-for-testing-public/131.0.6778.204/win64/chromedriver-win64.zip

If you are using other OS then go to: https://googlechromelabs.github.io/chrome-for-testing/
This version is tested with Chrome For Testing and Chrome Driver v131.0.6778.204
 

Attachments

  • Selenium1.zip
    4.6 KB · Views: 1
  • Selenium2.zip
    2.9 KB · Views: 1
Last edited:

behnam_tr

Active Member
Licensed User
Longtime User
thank you man the excutescript is ok
can i send key combinations like ctrl+a with ActionSendNonTextKey ??
 

behnam_tr

Active Member
Licensed User
Longtime User
Thank you for the application library, is it possible to add methods to manage cookies?

 

Pip

Member
Hi Pendrush,

I have been able to work around the "Access-Denied" problem and am looking for a more elegant solution than the following to determine the currently installed Chrome version. The function will need to work on different systems.

Current working code:
B4X:
Try
    Selenium.AddArgument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0 Safari/537.36 Edg/115.0.1901.183")
    Selenium.Initialize("Selenium", "C:\B4J\Projects\UPS_test_selenium\chromedriver115.exe")
Catch
    Log(LastException)
End Try
    
Try
    Selenium.AddArgument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0 Safari/537.36")
    Selenium.Initialize("Selenium", "C:\B4J\Projects\UPS_test_selenium\chromedriver114.exe")
Catch
    Log(LastException)
End Try

It's unfortunately try and error code, is there any way to select the correct Chromedriver based on the installed version?

Kind regards
Pip
 

hanyelmehy

Active Member
Licensed User
Longtime User
when try i get this error
B4X:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Invalid port. Exiting...
Error occurred on line: 25 (Main)
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Host info: host: 'DESKTOP-AI6L6HO', ip: '192.168.1.100'
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:84)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:73)
    at com.pendrush.selenium.kotlin.SeleniumLib.initialize(SeleniumLib.kt:32)
    at com.pendrush.selenium.Wrapper.initialize2(Wrapper.java:39)
    at com.pendrush.selenium.Wrapper.Initialize(Wrapper.java:35)
    at b4j.example.main._appstart(main.java:81)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.start(main.java:38)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely.
Build info: version: '4.9.1', revision: 'eb2032df7f'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '14.0.1'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:242)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:114)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    ... 34 more
 

pixet

Member
Licensed User
Longtime User
Last edited:

Pendrush

Well-Known Member
Licensed User
Longtime User

I will add Cookies in next few days.
I made an update to the library to ensure Selenium works. There has been a change in how Selenium interacts with the Chrome browser. Check post #1.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…