Ave
Ave

Reputation: 4430

Detect element and click this element on tag <canvas> in Selenium?

The website only has one tag is it contains all element.

I want to click on an element in this canvas.

I tried with code:

public void ClickCanvas(QA.IWebElement wbCanvas, int x, int y)
{
    try
    {
        QA.Interactions.Actions actionBuilder = new QA.Interactions.Actions(wd);
        Actions hoverClick = actionBuilder.MoveToElement(wbCanvas).MoveByOffset(x, y).ContextClick();
        hoverClick.Build().Perform();
    }
    catch { throw; }
}

And I use this function:

var width = int.Parse(browser.FindElementById("easCanvas").GetAttribute("width"));
var height = int.Parse(browser.FindElementById("easCanvas").GetAttribute("height"));
for (int i = 0; i < height; i += 5)
{
    for (int j = 0; j < width; j += 5)
    {
        browser.ClickCanvas(browser.FindElementById("easCanvas"), j,i);
    }
}

It scans at browser only centre of the screen. Must start at Point(0, 0) of the screen instead of start at the centre of the screen.

And I don't know when an element is clicked.

RESOLVED:

In selenium document,

moveToElement(WebElement toElement) : Moves the mouse to the middle of the element.

moveToElement(WebElement toElement, int xOffset, int yOffset) : Moves the mouse to an offset from the top-left corner of the element.

There is the reason for my question:

It scans at browser only centre of the screen

I need at position (0, 0) to it start at begin of an element.

Should be changed to:

Actions hoverClick = actionBuilder.MoveToElement(wbCanvas, 0, 0).MoveByOffset(x, y).ContextClick();

And I don't know when an element clicked.

In my site, I test when clicking on the element it will open a new popup, this popup is contained <iframe> tag.

I need add code:

while(browser.FindElementById("iframe_D") == null)
     Thread.Sleep(50);
if(browser.FindElementById("iframe_D") != null)
     browser.GoToFrame(browser.FindElementById("iframe_D"));

It's working for me.

Upvotes: 0

Views: 997

Answers (1)

marwaha.ks
marwaha.ks

Reputation: 549

This may be weird, but try using a minus y figure and see what happens.

It worked for me recently on one of my projects.

Upvotes: 1

Related Questions