How to find element by "onClick"

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
smilinglyqing
Posts: 12
Joined: Thu Sep 29, 2011 7:00 pm

How to find element by "onClick"

Post by smilinglyqing » Tue Apr 21, 2015 1:03 am

Hi folks,

Is there any way to find div tag element by its attribute "onclick"?

eg.
<div class="hotzoneDiv" id="hotzoneDiv_main" onclick="install();">

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3914
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to find element by "onClick"

Post by odklizec » Tue Apr 21, 2015 7:08 am

Hi,

Could you please be more specific about what exactly you want to achieve?

If you mean to use the "onclick" attribute in xpath, then yes, you can do that. It can be used as any other attribute. So the xpath could look like this...

Code: Select all

/body/div[@id='hotzoneDiv_main' and @onclick='install();']
Just one important note. The "onclick" attribute is a dynamic one, which means the page/application must be loaded at a time of recording/tracking the elements or even fiddling with the path weight editor. If the page is not loaded/active, dynamic attributes are not visible in spy and so they cannot be recorded or tracked!

If you want the "onclick" attribute to be automatically added to the xpath while recording/tracking the element, then you need to change the xpath weights to give the onclick attribute higher priority over ID or any other attribute with higher priority. By default, onclick attribute "weight" is 0 and ID attribute is 200. So you need to set the onclick weight value at least 201, to give it priority over ID.
PathWeights.png
PathWeights.png (73.87 KiB) Viewed 2341 times
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration