Jump to content

Not able to find Element using ChromeDriver


Go to solution Solved by SOLVE-SMART,

Recommended Posts

Posted (edited)

I have been trying to select a field in a page in order to insert a value. I have tried so many tings but am not able to find the elemet on the page. The element is there I can see it. once the page is loaded. I have tried Sleep for 10 seconds, Load wait and every suggestion chat GPt could offer. I have to mention that I have played with the files because I was getting errors in relation to function used in wd-core.au3 so I had to change some function names and all. It could be that me chrome driver is not in synch with the included file versions. As of now. I am getting the error ""Could not find the search button"

 

#include "JSON.au3"
#include "WinHttp.au3"
#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"

; Setup ChromeDriver
Global $_WD_DRIVER = "C:\Tools\ChromeDriver\chromedriver.exe"
Global $_WD_PORT = 9515
Global $_WD_DRIVER_PARAMS = "--port=9515"

; Start WebDriver
_WD_Startup()

; === Set Capabilities ===
Local $sCaps = '{"capabilities":{"alwaysMatch":{"browserName":"chrome","goog:chromeOptions":{"args":["--start-maximized"]}}}}'
_WD_Option('Capabilities', $sCaps)

; Create Session
Local $sSession = _WD_CreateSession()
If @error Then
    MsgBox(16, "Error", "Failed to create session.")
    Exit
EndIf

; Navigate to website
_WD_Navigate($sSession, "https://distributionjjcandy.com/account/login")
;Local $sSource = _WD_GetSource($sSession)
;ConsoleWrite($sSource & @CRLF)
;_WD_LoadWait($sSession) ; wait for page to load
Sleep(3000)

Local $jsCode = "return (function() {" & _
               "var el = document.querySelector('#input--template--22102333292843__main--customeremail');" & _
               "if(el) {return (el.offsetWidth > 0 && el.offsetHeight > 0);} else {return false;}" & _
               "})()"

Local $result = _WD_ExecuteScript($sSession, $jsCode)
ConsoleWrite("Visible? " & $result & @CRLF)

; Find and click the search button (by CSS selector)
Local $sElement = _WD_FindElement($sSession, "xpath", "//input[@type='email']")
;Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByLinkText, "Login")
;_WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, "a[href='/account/login']", 10000)
;_WD_ElementAction($sSession, $sElement, 'focus')
_WD_SetElementValue($sSession, $sElement, "your@email.com")
Sleep(1000)
If StringStripWS($sElement, 3) = "" Or StringLower($sElement) = "null" Or StringInStr($sElement, "error") Then
    MsgBox(16, "Error", "Could not find the search button.")
Else
    _WD_ElementAction($sSession, $sElement, 'click')
EndIf
;Local $sJS = "document.querySelector(""a[href='/account/login']"").click();"
;Local $sResult = _WD_ExecuteScript($sSession, $sJS, "[]")
;ConsoleWrite("JavaScript click result: " & $sResult & @CRLF)
Sleep(3000)

; Close everything
;_WD_DeleteSession($sSession)
;_WD_Shutdown()

ConcoleOutput..A.txt JSON.au3 WinHttp.au3 wd_core.au3 wd_helper.au3 wd_capabilities.au3

Edited by eesirrius
Added code in meddage
Posted

Hi @eesirrius,

are you talking about this URL "https://www.murraycashandcarry.com" (in you "test WebDriver.au3" file) or URL "https://distributionjjcandy.com/account/login" (in your "ConcoleOutput..A.txt" file)?

  1. Please describe what do you want to achieve? Like => click search icon, enter text in popup search area ... !?
  2. Is it important for you to use CSS Selector or would XPath also be fine?

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

  • Solution
Posted

Oh wow, I guess you probably relatively new to browser automation tasks, am I right? Don't get me wrong, all good, but you do not have to use _WD_ExecuteScript and javascript code to interact with the elements. As far as I can see, you want to login to the page by email, password and submit, right?

There should be plenty of examples how to do this. Let me suggest: have a look into my autoit-webdriver-boilerplate project/template which can help you by the basic structure, dependencies and more. Please let me know wheater this already helps you or you need more directions 🤝 .

Also please answer 1. and 2. of my post above.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

yeah 2 days old apprentice 😁 I installed everyting. i am running this but nothing happens:

#include-once
#include ".\init.au3"

Func _Steps()
    _OpenWebsite()

    _ConfirmConsentDialog()
    _NavigateBetweenPages()
    _GetTableTexts()

    _Print('Automation steps completed!')
    Sleep(1500)
EndFunc

Func _OpenWebsite()
    _NavigateTo('https://www.w3schools.com/xml/xpath_intro.asp')
EndFunc
 

Posted

Consol

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" /ErrorStdOut "C:\Program Files (x86)\AutoIt3\SciTE\autoit-webdriver-boilerplate-main\src\website-steps-handler.au3"    
>Exit code: 0
 

Posted
1 hour ago, SOLVE-SMART said:

Hi @eesirrius,

are you talking about this URL "https://www.murraycashandcarry.com" (in you "test WebDriver.au3" file) or URL "https://distributionjjcandy.com/account/login" (in your "ConcoleOutput..A.txt" file)?

  1. Please describe what do you want to achieve? Like => click search icon, enter text in popup search area ... !?
  2. Is it important for you to use CSS Selector or would XPath also be fine?

Best regards
Sven

1) As far as I can see, you want to login to the page by email, password and submit, right? Thas is exactly it to start

2) I have no preference 

Posted (edited)
1 hour ago, eesirrius said:

i am running this but nothing happens:

1 hour ago, eesirrius said:

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" /ErrorStdOut "C:\Program Files (x86)\AutoIt3\SciTE\autoit-webdriver-boilerplate-main\src\website-steps-handler.au3"    
>Exit code: 0

💡 Like described in usage section, you have to run the "main.au3" file (program entry point) instead of "website-steps-handler.au3". Then the session will be created and you see some actions in the browser.

-----------------------

I will give you helping hand for the start ... code follows.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
1 minute ago, SOLVE-SMART said:

💡 Like described in usage section, you have to run the "main.au3" file (program entry point) instead of "website-steps-handler.au3".

-----------------------

I will give you helping hand for the start ... code follows.

Best regards
Sven

Thanks for your help. I have been able to run the main but was getting an error with _ConfirmConsentDialog(). There might have been a dialog on the test page initially. I commented out that part and the script is now running correctly. Thank you very much for the library. 

Posted (edited)

FYI @eesirrius :
A example of how to login to the *jjcandy site can be cloned (GitHub) by:

git clone --branch jjcandy-example https://github.com/sven-seyfert/autoit-webdriver-boilerplate.git

In case you are not familiar with Git, let me know and I would provide a zip archive instead.

You have to adjust function _Steps() in "website-steps-handler.au3" and start "main.au3".
==> Logged in.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

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
×
×
  • Create New...