Jump to content

WebDriver UDF (W3C compliant version) - 2024/09/21


Danp2
 Share

Recommended Posts

6 hours ago, Danp2 said:

That's not the issue. 

You can't click an element that wasn't found by the prior function call. You'll need to locate the element first. Also some error checking in your code would help you here as well.

Can you give me an example?

Link to comment
Share on other sites

@NguyenLe Just check @error after the function call, like this --

SetupGecko()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, 'http://google.com')
$sElement = _WD_FindElement($sSession,$_WD_LOCATOR_ByXPath,"//a[@class='gsfi']")

If @error = $_WD_ERROR_Success Then
    _WD_ElementAction($sSession, $sElement, 'value','Hello')
EndIf

 

Link to comment
Share on other sites

10 hours ago, Danp2 said:

@NguyenLe Just check @error after the function call, like this --

SetupGecko()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, 'http://google.com')
$sElement = _WD_FindElement($sSession,$_WD_LOCATOR_ByXPath,"//a[@class='gsfi']")

If @error = $_WD_ERROR_Success Then
    _WD_ElementAction($sSession, $sElement, 'value','Hello')
EndIf

 

I found the error.

Thank for your answer.

Edited by NguyenLe
Link to comment
Share on other sites

I've done a quick attempt to use this stuff with IE11 on Win7 and at first glance it seems to work.
I've only tryed to adapt the simple test from post #2 to be used with IE.
more infos here https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
steps to do the test
1) Download the IEDriverServer.exe from here  http://selenium-release.storage.googleapis.com/index.html?path=3.8/ and save it along with the other scripts
2) open IE11 settings -> security -> and set (or remove) the "protected mode" flag for each zone (The value can be on or off, as long as it is the same for every zone)
3) use this modified script for the quick test

#include "wd_core.au3"
#include "wd_helper.au3"

Local Enum $eFireFox = 0, _
        $eChrome, _
        $eIE

Local $aTestSuite[][2] = [["TestTimeouts", False],["TestNavigation", False],["TestElements", False],["TestScript", False],["TestCookies", False],["TestAlerts", True]]

Local Const $_TestType = $eIE ; $eFireFox
Local $sDesiredCapabilities
Local $iIndex
Local $sSession

$_WD_DEBUG = True

Switch $_TestType
    Case $eFireFox
        SetupGecko()

    Case $eChrome
        SetupChrome()

    Case $eIE
        SetupIE()

EndSwitch

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

For $iIndex = 0 To UBound($aTestSuite, $UBOUND_ROWS) - 1
    If $aTestSuite[$iIndex][1] Then
        ConsoleWrite("Running: " & $aTestSuite[$iIndex][0] & @CRLF)
        Call($aTestSuite[$iIndex][0])
    Else
        ConsoleWrite("Bypass: " & $aTestSuite[$iIndex][0] & @CRLF)
    EndIf
Next

_WD_DeleteSession($sSession)
_WD_Shutdown()


Func TestTimeouts()
    _WD_Timeouts($sSession)
    _WD_Timeouts($sSession, '{"pageLoad":2000}')
    _WD_Timeouts($sSession)
EndFunc   ;==>TestTimeouts

Func TestNavigation()
    _WD_Navigate($sSession, "http://google.com")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    _WD_Action($sSession, "back")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    _WD_Action($sSession, "forward")
    ConsoleWrite("URL=" & _WD_Action($sSession, 'url') & @CRLF)
    ConsoleWrite("Title=" & _WD_Action($sSession, 'title') & @CRLF)
EndFunc   ;==>TestNavigation

;_WDWindow($sSession, 'frame', '{"id":nullelse
Func TestElements()
    Local $sElement, $aElements, $sValue

    _WD_Navigate($sSession, "http://google.com")
    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib1']")

    If @error = $_WD_ERROR_NoMatch Then
        $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
    EndIf

    $aElements = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div/input", '', True)

    _ArrayDisplay($aElements)

    _WD_ElementAction($sSession, $sElement, 'value', "testing 123")
    Sleep(500)

    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'clear')
    Sleep(500)
    _WD_ElementAction($sSession, $sElement, 'value', "abc xyz")
    Sleep(500)
    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'clear')
    Sleep(500)
    _WD_ElementAction($sSession, $sElement, 'value', "fujimo")
    Sleep(500)
    _WD_ElementAction($sSession, $sElement, 'text')
    _WD_ElementAction($sSession, $sElement, 'click')

    _WD_ElementAction($sSession, $sElement, 'Attribute', 'text')

    $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='lst-ib']")
    $sValue = _WD_ElementAction($sSession, $sElement, 'property', 'value')

    ConsoleWrite('value = ' & $sValue & @CRLF)

EndFunc   ;==>TestElements

Func TestScript()
    _WD_ExecuteScript($sSession, "return arguments[0].second;", '{"first": "1st", "second": "2nd", "third": "3rd"}')
    _WD_Alert($sSession, 'Dismiss')
EndFunc   ;==>TestScript

Func TestCookies()
    _WD_Navigate($sSession, "http://google.com")
    _WD_Cookies($sSession, 'Get', 'NID')
EndFunc   ;==>TestCookies

Func TestAlerts()
    ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
    _WD_ExecuteScript($sSession, "alert('testing 123')")
    ConsoleWrite('Alert Detected => ' & _WD_Alert($sSession, 'status') & @CRLF)
    ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
    _WD_Alert($sSession, 'sendtext', 'new text')
    ConsoleWrite('Text Detected => ' & _WD_Alert($sSession, 'gettext') & @CRLF)
    Sleep(5000)
    _WD_Alert($sSession, 'Dismiss')

EndFunc   ;==>TestAlerts


Func SetupGecko()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace')
    _WD_Option('Port', 4444)

    $sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
EndFunc   ;==>SetupChrome

Func SetupIE()
    _WD_Option('Driver', 'IEDriverServer.exe')
    _WD_Option('Port', 5555)
    _WD_Option('DriverParams', '--log-file=' & @ScriptDir & '\IE.log')

    $sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupIE

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

2 hours ago, ban64 said:

I've been looking through the samples but couldn't find what I was looking for. How do we connect to an existing session? 

This is controlled by the parameters passed to the specific driver when launched. If you look here, you will see that the Mozilla Geckodriver supports the option "--connect-existing". You would use _WD_Option to set the DriverParams option before calling _WD_Startup.

Link to comment
Share on other sites

Hello,

first of all thx @Danp2 for this great WD UDF. ^_^ It helps a lot for repeating work and speed in browsing. :-)

One question: i start a new firefox session and i´m missing my already included add-ons, which i like to refer during execution of some scripts.

Is there a way to start a new session wit FF Browser including Add-ons?

many thx,

horphi

 

Link to comment
Share on other sites

On 2/4/2018 at 2:19 AM, Danp2 said:

@NguyenLe Just check @error after the function call, like this --

SetupGecko()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, 'http://google.com')
$sElement = _WD_FindElement($sSession,$_WD_LOCATOR_ByXPath,"//a[@class='gsfi']")

If @error = $_WD_ERROR_Success Then
    _WD_ElementAction($sSession, $sElement, 'value','Hello')
EndIf

 

What i do, if i cannot really identify an unique object, i switch to wd_script for instance...

 

Func _attach() ;--> open attachment dialog
    $sinputATT = "document.getElementsByName('attachment')[0].click();" 
    _WD_ExecuteScript($sSession, $sinputATT)

EndFunc   ;==>_attach

BR

Link to comment
Share on other sites

53 minutes ago, horphi said:

Is there a way to start a new session wit FF Browser including Add-ons?

This would be controlled using the DesiredCapabilities parameter that gets passed to _WD_CreateSession. 

Each driver has it's own implementation method. You can gather more details at these links --

https://github.com/mozilla/geckodriver

https://sites.google.com/a/chromium.org/chromedriver/capabilities

Link to comment
Share on other sites

Hi,

When I want to invoke a search box in the web driver session, sending "^F" or "!F" is not working. It works outside of the script, but it doesn't inside the script.

I'm going around by clicking the setting button and sending "F".

Am I doing anything wrong?

$_WD_DEBUG = False
SetupChrome()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, "https://www.88countryclub.co.kr/")
$hwnd = WinWaitActive("88")
WinSetState($hwnd, "", @SW_MAXIMIZE)
Sleep(1000)
Send("^F")

 

Link to comment
Share on other sites

4 minutes ago, CYCho said:

ControlSend($hwnd, "", "", "^f") does not work either. I can't find a suitable webdriver command in your UDFs.

It did in my testing. Here's the complete code --

#include "wd_core.au3"

Local $sDesiredCapabilities

$_WD_DEBUG = False

SetupChrome()
_WD_Startup()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, "https://www.88countryclub.co.kr/")
$hwnd = WinWaitActive("88")
WinSetState($hwnd, "", @SW_MAXIMIZE)

Sleep(1000)
ControlSend($hwnd, "", "", "^f")

Func SetupChrome()
_WD_Option('Driver', 'chromedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--log-path=' & @ScriptDir & '\chrome.log')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true }}}}'
EndFunc

 

Link to comment
Share on other sites

  • Danp2 changed the title to WebDriver UDF (W3C compliant version) - 2024/09/21
  • Melba23 pinned this topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...