actions.moveByOffset() the method does not work

1,029 views
Skip to first unread message

Yan Lincle

unread,
Dec 2, 2013, 12:54:57 AM12/2/13
to webd...@googlegroups.com
hi,all:
    I use the class of actions to simulate the user to move mouse to the correct area to make the invisible element visible.the method is this:
                   actions.moveByOffset(1073, 503).moveToElement(wb.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/span"))).click().build().perform();
   the method means:
           first:simulate the user to move mouse to pointer(1073, 503) to make <div style="height: 680px; display: none;" class="pageRight">
                    <span style="margin-top: 302px; ">
                        <a onfocus="this.blur()" href="javascript:void(0);"></a>
                    </span>
                </div>
       visible.
           second:move to the visible element and click
   but finally,it does not work.why,is there any place wrong to the method : actions.moveByOffset(1073, 503):

susanth.nair

unread,
Dec 3, 2013, 2:04:41 PM12/3/13
to webd...@googlegroups.com
how you got this (1073, 503)?




--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.

susanth.nair

unread,
Dec 3, 2013, 2:12:06 PM12/3/13
to webd...@googlegroups.com
try this


use java script  in JavascriptExecutor


String enableHiddenElement=
                "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";



Yan Lincle

unread,
Dec 3, 2013, 9:43:58 PM12/3/13
to webd...@googlegroups.com
hi,firstly,thank you for your reply.I get the pointer by extral tool which can locate the mouse point number

Yan Lincle

unread,
Dec 3, 2013, 9:48:12 PM12/3/13
to webd...@googlegroups.com
yes,I hava already being successful in javascript,but you know,it can not stimulate a real user do.actully ,a real user will move the mouse to correct area,and then the invisible element will display,then click them.our aim for automation testing is trying best to simulate final user,that is why I abandoned the javascript way and try to use actions class.

susanth.nair

unread,
Dec 4, 2013, 11:51:14 AM12/4/13
to webd...@googlegroups.com
i think Actions class is also triggering the java scrips to stimulate the user actions
are you looking for something like cursor movement on to DOM  and do mouse down?


darrell

unread,
Dec 4, 2013, 3:05:41 PM12/4/13
to webd...@googlegroups.com
I seldom use the moveByOffset method. It is quite brittle using something which depends on screen size and position of objects. There is a moveTo(WebElement) method. Find the invisible element the use that WebElement in a moveTo() call. There isn't really a need for the moveByOffset here. Also, is the findElement working? Try breaking it out into two calls and confirm the findElement is working before using it in an Action.

Yan Lincle

unread,
Dec 5, 2013, 1:56:08 AM12/5/13
to webd...@googlegroups.com
hi,darrell:
   you mean use the method moveToElement( WebElement) instead?are you sure it works well to move to the invisible element,Before I have already tried this way,but it does work.
actions.moveToElement(wb.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/span"))).click().build().perform()

susanth.nair

unread,
Dec 5, 2013, 10:12:52 AM12/5/13
to webd...@googlegroups.com
this is a absolute Xpath
change to relative xpath

else please share the html




darrell

unread,
Dec 5, 2013, 1:50:26 PM12/5/13
to webd...@googlegroups.com
Yan,

Are you still trying to automate http://hd.club.news.sohu.com/zz524/photo/1vir7dpdpo4? Try using a better locator. Read http://darrellgrainger.blogspot.com/2011/12/selecting-webdriver-locators.html for how to select better locators. This will not solve your problem but you should be in the habit of using good locators. 

First you are locating the span element and clicking it. A span is not typically clickable. You probably want to locate the a element under the span. Additionally, an absolute XPath should start with /, e.g. /html/body/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/span/a

Rather than

    By.xpath("/html/body/div[1]/div[2]/div[2]/div[2]/div[2]/div[1]/span/a")

I would use:

    By.xpath("div[@id='mouseOverright']/div[@class='pageRight']/span/a")

Actually, CSS selectors are often faster. So I would use:

    By.cssSelector("div#mouseOverright>div.pageRight>span>a")

This CSS selector is exactly the same as the XPath but faster. Or even better would be:

    By.cssSelector("#mouseOverright>.pageRight>span>a")

So the whole thing would be:

    WebElement pageRight = driver.findElement(By.cssSelector("#mouseOverright>.pageRight>span>a"));
    Actions actions = new Actions(driver);
    actions.moveToElement(pageRight).click().build().perform();

Yan Lincle

unread,
Dec 6, 2013, 3:34:59 AM12/6/13
to webd...@googlegroups.com
hi,darrell:
   thank you for your help these days,really appreciated,it is really shame to commit,but it is true,I am still trying to automate  http://hd.club.news.sohu.com/zz524/photo/1vir7dpdpo4 ,all the problem has solved ,the only one left ,I am not reconciled.so I tried all the methods I can get,so embarrassing.

Yan Lincle

unread,
Dec 6, 2013, 3:36:26 AM12/6/13
to webd...@googlegroups.com
relative xpath?but how to locate the relative xpath?and what relative xpath?which is object of reference?

Yan Lincle

unread,
Dec 6, 2013, 4:07:33 AM12/6/13
to webd...@googlegroups.com
I tried your method,but still mistake occurs,which says the element is not visible.it does not work.


On Friday, December 6, 2013 2:50:26 AM UTC+8, darrell wrote:

darrell

unread,
Dec 6, 2013, 10:13:04 AM12/6/13
to webd...@googlegroups.com
If it does not work I would suspect a timing issue. The example I gave does the following:

- move to the element
- click the element

If there is a delay between moving to the element and the element being clickable you need to add some sort of pause between the two actions. So you might need:

- move to the element
- wait for element to be visible
- click the element

I don't believe there is a 'wait for element to be visible' in Actions. So you will have to use Actions to move to the element, WebDriverWait to wait for the element to be clickable. Then click it (might be able to use WebElement.click() or might need to use Actions.click()). How you automate javascript (AJAX, Web 2.0, etc.) is very dependent on how the javascript was implemented. Because there is no single answer, this is why WebDriver does not have build in features to handle this.

I learned Selenium by including the src jar from Selenium in my project. I could then step into the Selenium code and analyze it just like I could step into my own code and analyze it. By looking at how the Selenium code did and did not work with different websites I slowly started to figure out how to deal with different javascript libraries. It is still an ongoing process. Whenever I am on a project which uses a different javascript library I find myself occasionally figuring out a new why to do things.
Reply all
Reply to author
Forward
0 new messages