Jump to content

How to click link by text in WebDriver UDF


MRAJ
 Share

Recommended Posts

Hello,

Currently i am using the code for one of the text to click in IE browser and its working fine

Local $sMyString = "home"
Local $oLinks = _IELinkGetCollection($oIE)

For $oLink In $oLinks
    Local $sLinkText = _IEPropertyGet($oLink, "innerText")
    If StringInStr($sLinkText, $sMyString) Then
        _IEAction($oLink, "click")
        ExitLoop
    EndIf
 Next

. Now i am using Edge Browser but i am not able to find the same function in Webdriver UDF as it is IE.

Please let me know which function will match. I tried to use _WD_LinkClickByText but its not working.

Link to comment
Share on other sites

2 hours ago, MRAJ said:

Currently i am using the code for one of the text to click in IE browser and its working fine

Why didn't you use _IELinkClickByText here?

Quote

I tried to use _WD_LinkClickByText but its not working

"Not working" doesn't come close to giving us the necessary details so that we can help you. If you want help with this, then please do the following --

  • Post a short (but complete) reproducer script that we can run to observe the problem
  • Run the same script and show us your complete results from the Scite output panel
Link to comment
Share on other sites

The same script using IE browser is working fine as attached, but i want to try the same using edge browser which will click on the text. if you will check the script it is using IE browser. So i tried the below in my script using edge browser but it is not working. So i need to check what is the Webdriver UDF for clicking on any link text.

Local $sMyString = "About this Page"
Local $oLinks = _IELinkGetCollection($oIE)

For $oLink In $oLinks
    Local $sLinkText = _IEPropertyGet($oLink, "innerText")
    If StringInStr($sLinkText, $sMyString) Then
        _IEAction($oLink, "click")
        ExitLoop
    EndIf
 Next

Using WD function:

_WD_LinkClickByText($sSession, "$sMyString")
_WD_ElementAction($sSession,'click')

Link to comment
Share on other sites

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


$_WD_DEBUG = $_WD_DEBUG_None ; Could also use $_WD_DEBUG_Error
Local $sDesiredCapabilities, $sSession, $sElement

SetupEdge()
_WD_Startup()
Sleep(2000)
_WD_ConsoleVisible()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession,$URL)
Sleep(4000)



Local $sMyString = "About this Page"
Local $oLinks = _IELinkGetCollection($oIE)

For $oLink In $oLinks
    Local $sLinkText = _IEPropertyGet($oLink, "innerText")
    If StringInStr($sLinkText, $sMyString) Then
        _IEAction($oLink, "click")
        ExitLoop
    EndIf
 Next

_WD_LinkClickByText($sSession, "$sMyString")
_WD_ElementAction($sSession,'click')


EndIf

Func SetupEdge()

_WD_Option('Driver', 'msedgedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--verbose')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"binary": "' & StringReplace (@ProgramFilesDir, "\", "/") & '/Microsoft/Edge/Application/msedge.exe", "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false}}}}'

EndFunc

once the website page will open i have to click on the text which is About this page. My IE browser script is working fine, but using WD function edge browser it is not clicking, even i used the below script WD function script.
_WD_LinkClickByText($sSession, "$sMyString")
_WD_ElementAction($sSession,'click')

Link to comment
Share on other sites

That code has so many issues, there's no way you got it to run --

  • Non-existent $URL variable
  • Calling IE UDF functions
  • "$sMyString" is a string constant. Remove the quotes if you want to use the variable
  • _WD_ElementAction called with wrong number of parameters
  • Endif without matching If

Want to try again, this time put in some actual effort and make sure the code runs before you post it?

Link to comment
Share on other sites

Hello Dan,

My code is running, i forget to add the URL variable and remove the IE udf(i just added IE UDF so that you can know what i was using earlier and it was working, so now i am using WD udf function to do the same thing like clicking on the text but it is not working using WD UDF). I have uploaded script what exactly i am running using WD UDF. I didnt mention the Application URL, but while running the script i am using the URL variable. Application URL is opening fine but it is not clicking on the text link which is "About the page"

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

$URL = "abc.com"
$_WD_DEBUG = $_WD_DEBUG_None ; Could also use $_WD_DEBUG_Error
Local $sDesiredCapabilities, $sSession, $sElement

SetupEdge()
_WD_Startup()
Sleep(2000)
_WD_ConsoleVisible()
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession,$URL)
Sleep(4000)

$sMyString = " About this page"
_WD_LinkClickByText($sSession, "$sMyString")
_WD_ElementAction($sSession,'click')


EndIf

Func SetupEdge()

_WD_Option('Driver', 'msedgedriver.exe')
_WD_Option('Port', 9515)
_WD_Option('DriverParams', '--verbose')

$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"binary": "' & StringReplace (@ProgramFilesDir, "\", "/") & '/Microsoft/Edge/Application/msedge.exe", "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false}}}}'

EndFunc

 

Link to comment
Share on other sites

Are you running your script with the full Scite package, available here? If you were, then I'd expect it to highlight some of the issues with your code, such as --

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /Prod /AU3check /in "C:\Users\danpollak\Dropbox\webdriver\test.au3"
+>08:18:56 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\danpollak\Dropbox\webdriver\test.au3
"C:\Users\danpollak\Dropbox\webdriver\test.au3"(18,36) : error: _WD_ElementAction() called with wrong number of args.
_WD_ElementAction($sSession,'click')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\danpollak\Dropbox\webdriver\wd_core.au3"(722,76) : REF: definition of _WD_ElementAction().
Func _WD_ElementAction($sSession, $sElement, $sCommand, $sOption = Default)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\danpollak\Dropbox\webdriver\test.au3"(21,1) : error: syntax error
EndIf
^
"C:\Users\danpollak\Dropbox\webdriver\test.au3"(21,1) : error: Statement cannot be just an expression.
EndIf
^
C:\Users\danpollak\Dropbox\webdriver\test.au3 - 3 error(s), 0 warning(s)
!>08:18:57 AU3Check ended. Press F4 to jump to next error.rc:2
+>08:18:57 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.391

I would also suggest that you comment out the $WD_DEBUG line until you have completely debugged your script. This will provide more details in the Scite output panel that will assist with debugging. This is the output from running your script (once I addressed the two errors shown above) --

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\danpollak\Dropbox\webdriver\test.au3" /UserParams    
+>08:24:12 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:00000409  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\danpollak\Dropbox\webdriver\test.au3
+>08:24:12 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\danpollak\Dropbox\webdriver\test.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
_WD_IsLatestRelease: True
_WD_IsLatestRelease ==> Success
_WDStartup: OS: WIN_10 WIN32_NT 18363 
_WDStartup: AutoIt: 3.3.14.5
_WDStartup: WD.au3: 0.3.0.8 (Up to date)
_WDStartup: WinHTTP:    1.6.4.2
_WDStartup: Driver: msedgedriver.exe
_WDStartup: Params: --verbose
_WDStartup: Port:   9515
__WD_Post: URL=HTTP://127.0.0.1:9515/session; $sData={"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"binary": "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", "excludeSwitches": [ "enable-automation"], "useAutomationExtension": false}}}}
__WD_Post: StatusCode=200; ResponseText={"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"msedge","browserVersion":"85.0....
_WD_CreateSession: {"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"msedge","browserVersion":"85.0.564.44","ms:edgeOptions":{"debuggerAddress":"localhost:56839"},"msedge":{"msedgedriverVersion":"85.0.564.44 (3b106b45f8e7cd4c3e2aac5a6ba411645c16df60)","userDataDir":"C:\\Users\\danpollak\\AppData\\Local\\Temp\\scoped_dir41128_1347304264"},"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platformName":"windows","proxy":{},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","webauthn:virtualAuthenticators":true},"sessionId":"1186ebc1d2495bfde4ce234ed129469c"}}
__WD_Post: URL=HTTP://127.0.0.1:9515/session/1186ebc1d2495bfde4ce234ed129469c/url; $sData={"url":"abc.com"}
__WD_Post: StatusCode=400; ResponseText={"value":{"error":"invalid argument","message":"invalid argument\n  (Session info: MicrosoftEdge=85....
__WD_Post ==> Invalid argument: {"value":{"error":"invalid argument","message":"invalid argument\n  (Session info: MicrosoftEdge=85.0.564.44)","stacktrace":"Backtrace:\n\tOrdinal0 [0x004ABFA3+3194787]\n\tOrdinal0 [0x002875A2+947618]\n\tOrdinal0 [0x0027C111+901393]\n\tOrdinal0 [0x0027C175+901493]\n\tOrdinal0 [0x0028879F+952223]\n\tOrdinal0 [0x002360BB+614587]\n\tOrdinal0 [0x0022CE8D+577165]\n\tOrdinal0 [0x00235C98+613528]\n\tOrdinal0 [0x0022CD5B+576859]\n\tOrdinal0 [0x00213302+471810]\n\tOrdinal0 [0x0021406A+475242]\n\tOrdinal0 [0x00214007+475143]\n\tOrdinal0 [0x003D04AB+2294955]\n\tGetHandleVerifier [0x005A9FDD+940269]\n\tGetHandleVerifier [0x005BAAB7+1008583]\n\tGetHandleVerifier [0x005AA906+942614]\n\tOrdinal0 [0x003C8EF8+2264824]\n\tOrdinal0 [0x003D1A4B+2300491]\n\tOrdinal0 [0x003D1BC1+2300865]\n\tOrdinal0 [0x003DFB70+2358128]\n\tBaseThreadInitThunk [0x751B6359+25]\n\tRtlGetAppContainerNamedObjectPath [0x77987C24+228]\n\tRtlGetAppContainerNamedObjectPath [0x77987BF4+180]\n"}}
_WD_Navigate: {"value":{"error":"invalid argument","message":"invalid argument\n  (Session info: MicrosoftEdge=85.0.564.44)","stacktrace":"Backtrace:\n\tOrdinal0 [0x004ABFA3+3194787]\n\tOrdinal0 [0x002875A2+947618]\n\tOrdinal0 [0x0027C111+901393]\n\tOrdinal0 [0x0027C175+901493]\n\tOrdinal0 [0x0028879F+952223]\n\tOrdinal0 [0x002360BB+614587]\n\tOrdinal0 [0x0022CE8D+577165]\n\tOrdinal0 [0x00235C98+613528]\n\tOrdinal0 [0x0022CD5B+576859]\n\tOrdinal0 [0x00213302+471810]\n\tOrdinal0 [0x0021406A+475242]\n\tOrdinal0 [0x00214007+475143]\n\tOrdinal0 [0x003D04AB+2294955]\n\tGetHandleVerifier [0x005A9FDD+940269]\n\tGetHandleVerifier [0x005BAAB7+1008583]\n\tGetHandleVerifier [0x005AA906+942614]\n\tOrdinal0 [0x003C8EF8+2264824]\n\tOrdinal0 [0x003D1A4B+2300491]\n\tOrdinal0 [0x003D1BC1+2300865]\n\tOrdinal0 [0x003DFB70+2358128]\n\tBaseThreadInitThunk [0x751B6359+25]\n\tRtlGetAppContainerNamedObjectPath [0x77987C24+228]\n\tRtlGetAppContainerNamedObjectPath [0x77987BF4+180]\n"}}
_WD_Navigate ==> Invalid argument: HTTP status = 400
__WD_Post: URL=HTTP://127.0.0.1:9515/session/1186ebc1d2495bfde4ce234ed129469c/element; $sData={"using":"partial link text","value":"$sMyString"}
__WD_Post: StatusCode=404; ResponseText={"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\...
__WD_Post ==> No match: {"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"partial link text\",\"selector\":\"$sMyString\"}\n  (Session info: MicrosoftEdge=85.0.564.44)","stacktrace":"Backtrace:\n\tOrdinal0 [0x004ABFA3+3194787]\n\tOrdinal0 [0x0028770C+947980]\n\tOrdinal0 [0x0021AAA5+502437]\n\tOrdinal0 [0x002371F0+618992]\n\tOrdinal0 [0x0022CEC0+577216]\n\tOrdinal0 [0x00235C98+613528]\n\tOrdinal0 [0x0022CD5B+576859]\n\tOrdinal0 [0x00213302+471810]\n\tOrdinal0 [0x0021406A+475242]\n\tOrdinal0 [0x00214007+475143]\n\tOrdinal0 [0x003D04AB+2294955]\n\tGetHandleVerifier [0x005A9FDD+940269]\n\tGetHandleVerifier [0x005BAAB7+1008583]\n\tGetHandleVerifier [0x005AA906+942614]\n\tOrdinal0 [0x003C8EF8+2264824]\n\tOrdinal0 [0x003D1A4B+2300491]\n\tOrdinal0 [0x003D1BC1+2300865]\n\tOrdinal0 [0x003DFB70+2358128]\n\tBaseThreadInitThunk [0x751B6359+25]\n\tRtlGetAppContainerNamedObjectPath [0x77987C24+228]\n\tRtlGetAppContainerNamedObjectPath [0x77987BF4+180]\n"}}
_WD_FindElement: {"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"partial link text\",\"selector\":\"$sMyString\"}\n  (Session info: MicrosoftEdge=85.0.564.44)","stacktrace":"Backtrace:\n\tOrdinal0 [0x004ABFA3+3194787]\n\tOrdinal0 [0x0028770C+947980]\n\tOrdinal0 [0x0021AAA5+502437]\n\tOrdinal0 [0x002371F0+618992]\n\tOrdinal0 [0x0022CEC0+577216]\n\tOrdinal0 [0x00235C98+613528]\n\tOrdinal0 [0x0022CD5B+576859]\n\tOrdinal0 [0x00213302+471810]\n\tOrdinal0 [0x0021406A+475242]\n\tOrdinal0 [0x00214007+475143]\n\tOrdinal0 [0x003D04AB+2294955]\n\tGetHandleVerifier [0x005A9FDD+940269]\n\tGetHandleVerifier [0x005BAAB7+1008583]\n\tGetHandleVerifier [0x005AA906+942614]\n\tOrdinal0 [0x003C8EF8+2264824]\n\tOrdinal0 [0x003D1A4B+2300491]\n\tOrdinal0 [0x003D1BC1+2300865]\n\tOrdinal0 [0x003DFB70+2358128]\n\tBaseThreadInitThunk [0x751B6359+25]\n\tRtlGetAppContainerNamedObjectPath [0x77987C24+228]\n\tRtlGetAppContainerNamedObjectPath [0x77987BF4+180]\n"}}
_WD_FindElement ==> No match: HTTP status = 404
_WD_LinkClickByText ==> No match
+>08:24:23 AutoIt3.exe ended.rc:0
+>08:24:23 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 11.46

If you take the time to examine these logs, you'll see that there's an issue with the URL being passed to _WD_Navigate. You can fix this by adding the https prefix.

There are other issues with your code, some of which I pointed out in a previous post but you haven't yet fixed.

Link to comment
Share on other sites

  • Moderators

A note to some of our report-happy forum members; if we begin reporting every thread where someone is showing less than the greatest effort, I do believe we'll bring down the site.

Yes, people who supply little to no detailed information, or don't show a lot of effort, can be irritating. This could be because they are new to the language, there is a language barrier, or they're just used to people spoon-feeding them answers on other forums. Regardless, if you see a thread where you believe the OP should provide more information you can: (1) do what @Danp2 has been doing here and try to nudge them in the right direction, or (2) move on to another thread. No need to report and ask the Moderation staff to scold them.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...