HTML 5 Canvas objects don't have id, name nor xpath???

539 views
Skip to first unread message

Ahmed Neinae

unread,
Sep 18, 2014, 9:39:11 AM9/18/14
to seleniu...@googlegroups.com

I am using selenium 2.42.2 running my script on FF Browser. I am trying to identify some objects in my application so I can perform some actions like click or sendKeys, however these objects are HTML5 canvas objects and they all have the same id, name and xpath as well. I wonder how can I find a unique identification for this object?

<div style="position: absolute; overflow: hidden; width: 777px; height: 499px;">
<div style="transform-origin: 0px 0px 0px;">
<canvas style="position: absolute;" width="777" height="499"/>
<canvas style="position: absolute;" width="777" height="499"/>
<canvas style="position: absolute;" width="777" height="499"/>
</div>
</div>

Andreas Tolfsen

unread,
Sep 19, 2014, 8:51:44 AM9/19/14
to seleniu...@googlegroups.com
↪ Ahmed Neinae <ahmed....@gmail.com>:
> I am using selenium 2.42.2 running my script on FF Browser. I am trying
> to identify some objects in my application so I can perform some actions
> like click or sendKeys, however these objects are HTML5 canvas objects
> and they all have the same id, name and xpath as well. I wonder how can
> I find a unique identification for this object?

find_elements returns a collection of web elements. Pseudo code to get
the element reference of the third canvas:

cs = find_elements_by_tag_name("canvas")
canvas = cs[2]

Ahmed Neinae

unread,
Sep 28, 2014, 3:21:53 PM9/28/14
to seleniu...@googlegroups.com
Thanks much for the answer, Can you please explain little more??


--
You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/34wZcbA6I_0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/20140919125056.GC4571%40mozilla.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Tolfsen

unread,
Sep 29, 2014, 4:53:16 AM9/29/14
to seleniu...@googlegroups.com
On Sun, Sep 28, 2014 at 8:21 PM, Ahmed Neinae <ahmed....@gmail.com> wrote:
> On Fri, Sep 19, 2014 at 8:50 AM, Andreas Tolfsen <a...@mozilla.com> wrote:
>>
>> ↪ Ahmed Neinae <ahmed....@gmail.com>:
>> > I am using selenium 2.42.2 running my script on FF Browser. I am trying
>> > to identify some objects in my application so I can perform some actions
>> > like click or sendKeys, however these objects are HTML5 canvas objects
>> > and they all have the same id, name and xpath as well. I wonder how can
>> > I find a unique identification for this object?
>>
>> find_elements returns a collection of web elements. Pseudo code to get
>> the element reference of the third canvas:
>>
>> cs = find_elements_by_tag_name("canvas")
>> canvas = cs[2]
>>
>
> Thanks much for the answer, Can you please explain little more??

I'm not sure my answer could be made much clearer, but perhaps I'm
misunderstanding your problem.

You say you have three canvas elements in your document as such:

<canvas>…</canvas>
<canvas>…</canvas>
<canvas>…</canvas>

Because neither of them has a unique ID identifier, you need a way to
access one of them directly. My code above retrieves element
references to all three and access the third element of the sequence.

Using a CSS selector you could also select the third one directly:

third_el = find_elements_by_css("canvas:nth-child(3)")
Reply all
Reply to author
Forward
0 new messages