您的位置:首页 > 其它

鼠标悬停事件

2014-06-12 15:41 155 查看
这两天郁闷坏了.

自己写的自动话,早两天还可以跑得很顺畅的.

昨天下午发现,基本上跑不动了,要看运气.然后调试了下,换了种方法,发现还是如此.但是debug的时候却都能通过。

然后早上问一个群友,他说应该是元素不稳定导致的。

但是我偶然发现,如果我把鼠标放在下拉列表处,让下拉列表展现出来,是100%能过的。

我不得不重新考虑我的代码,悬浮处理那块。

前面是想进办法用了hold, hover, wait, mouse over, move element to

效果不太好,没有彻底解决我的问题

// driver.navigate().refresh();

WebDriverWrapper.hoverandclick(luntan,driver);

// WebDriverWrapper.waitPageLoad(driver,3);

WebDriverWrapper.hold(luntan,driver);

luntan.sendKeys(Keys.RIGHT);

// try{

// WebDriverWrapper.rightclick(luntan,driver);

// }catch (Exception e) {

// //TODO Auto-generated catch block

// e.printStackTrace();

// }

然后我在网上找了些答案,重新定义了我的hold,还是没招。

publicstatic void hold(WebElement path, WebDriver driver) {

Actions action = new Actions(driver);

// action.clickAndHold();// 鼠标悬停在当前位置,既点击并且不释放

// action.clickAndHold(path);// 鼠标悬停在 onElement 元素的位置

action.moveToElement(path).click();

}

WebDriverWrapper.hoverandclick(newpen,driver);

newpen.click();

WebDriverWrapper.hold(newpen,driver);

但是debug是发现不了什么问题的。

于是网上去搜。

Actions actions = newActions(driver);

actions.moveToElement(element).click().build().perform();

源文档 <http://stackoverflow.com/questions/14692607/how-to-mouseover-on-a-webelement>

这个方法跟我定义在hoverandclick()里面是一样的。

这个方法,定义在我代码里面有错,也不知道怎么改,就放弃了。

LocatablehoverItem = (Locatable) driver.findElement(By.xpath("//foo"));
Mouse mouse =((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());

源文档 <https://groups.google.com/forum/#!topic/selenium-users/oxmxWe-Roro>

这个,看起来跟我的没什么区别,他写了好多方法,好像也没有解决我的问题。

actions action = new Actions(driver);
action.moveToElement(projectElement).moveByOffset(10, 3).build().perform();

源文档 <http://blog.csdn.net/hcx1234567/article/details/17605533>

然后这个方法,我也没有弄成功

import sys

from selenium import webdriver

##导入Action Chains模块
from selenium.webdriver.common.action_chainsimport *

##建立动作链
chain = ActionChains(driver)
##定位到HTML元素Daily Case Management
implement =driver.find_element_by_xpath("//a[@data-subid='1']")

##执行
chain.move_to_element(implement).perform()
##点击Add Target
driver.find_element_by_xpath("//a[@data-tabid='tab-202101001']").click()

源文档 </article/7230124.html>

加了个sentkey 方法,也一点作用都没有

public void moveToElement(WebDriver driver, By locator) {

driver.findElement(locator).sendKeys(Keys.DOWN);
}

源文档 <http://blog.tianya.cn/blogger/post_read.asp?BlogID=4686115&PostID=50852651>

这个是先找到元素,然后停留

IWebElement elementToHover =graphRegion.FindElement(By.CssSelector(CSSSelector));

Actions hover = new Actions(driver);
hover.MoveToElement(elementToHover);

Thread.Sleep(2000);

hover.Perform();

源文档 <http://www.91r.net/ask/15401945.html>

感觉跟我的差不多。

下面这个,跟我的差不多,也不能解决问题

WebElementmnuElement;

WebElement submnuElement;

mnEle = driver.findElement(By.Id("mnEle")).Click();

sbEle = driver.findElement(By.Id("sbEle")).Click();

Actionsbuilder = new Actions(driver);

// Move cursor to the Main MenuElement

builder.MoveToElement(mnEle).Perform();

// Giving 5 Secs for submenu to be displayed

Thread.sleep(5000L);

// Clicking on the Hidden SubMenu

driver.findElement(By.Id("sbEle")).Click();

源文档 <http://stackoverflow.com/questions/12275383/how-to-select-an-element-from-a-menu-using-webdriver-selenium-the-menu-drop-do>

12 down vote accepted

Its not really possible to perform a 'mouse hover' action, instead you need to chain all of the actions that you want to achieve in one go. So move to the element that reveals the others, then during the same chain, move to the now revealed element and click
on it.

When using Action Chains you have to remember to 'do it like a user would'.

源文档 <http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java>

Actionsaction = new Actions(webdriver);

WebElement we =webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));

action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();

源文档 <http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java>

老外的也没解决我的问题。

尝试封装一个类,用右键来获取焦点:

publicstatic void rightclick(WebElement path, WebDriver driver) throws Exception {

Actionsaction = new Actions(driver);

Robot robot = new Robot();

action.contextClick(path).perform();//右点击

robot.keyPress(KeyEvent.VK_ESCAPE);

}

public void moveToElement(WebDriver driver, By locator) {

driver.findElement(locator).sendKeys(Keys.DOWN);
}

源文档 <http://www.366help.com/index.php?m=content&c=index&a=show&catid=14&id=686>

一闪而过,没解决我的问题。

button= driver.find_element_by_class_name('btn')

driver.execute_script('$(arguments[0]).fadeOut()',button)

time.sleep(5)

源文档 </article/4666272.html>

这个之前在哪里见过,也跑不动。

Actionsactions = new Actions(driver);

WebElement menuHoverLink = driver.findElement(By.linkText("Menuheading"));

actions.moveToElement(menuHoverLink);

WebElementsubLink = driver.findElement(By.cssSelector("#headerMenu.subLink"));

actions.moveToElement(subLink);

actions.click();

actions.perform();

源文档 <http://stackoverflow.com/questions/15339311/how-to-do-mouse-hover-using-selenium-webdriver-in-firefox-19>

这个是一样一样滴。

Actions action = new Actions(driver);

action.clickAndHold();// 鼠标悬停在当前位置,既点击并且不释放

action.clickAndHold(onElement);// 鼠标悬停在 onElement元素的位置

源文档 <http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/>

这个方法定义过,没用,擦。

然后跟老大讨论了一下,他说可以用url绕过去,别在这里卡太久了。

哥在这里搞得太崩溃了。

准备放弃了。

等下次再看看,能否灵光一现,就解决了。

Bless me!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: