How to mouse hover then need to click on the tab- Selenium WebDriver

橙三吉。 提交于 2019-12-12 05:39:45

问题


Working on selenium webdriver First i want the mouse need to hover to tab which is shown in image. from Age to Time to Resolve. Using Java.
if(existsElement("ext-pr-backlog-evolution")==true){
WebElement menuHoverLink = driver.findElement(By.id("ext-pr-backlog-evolution"));// Problem in this line
actions.moveToElement(menuHoverLink).perform();//
JavascriptExecutor executor = (JavascriptExecutor)driver;// This is exactly opening the page
executor.executeScript("arguments[0].click();", driver.findElement(By.id("ext-pr-backlog-evolution") ));// This is exactly opening the page
Thread.sleep(6000);
}
else{
System.out.println("element not present -- so it entered the else loop");
}

Here is the HTML tag

<a id="ext-pr-backlog-evolution" class=" ext-pr-backlog-evolution" name="ext-pr-backlog-evolution" href="https://10.4.16.159/extranet_prbacklogevolutiontendency/reports/type/default/">Overview & Evolution</a>

In the Image upto Problem Reports(PR) the mouse is hovering when trying to click on Overview and Evloution tab it is moving to Ticket tab but the Overview and Evloution page is opening. Exactly it is opening the tab but not hovering and clicking.


回答1:


Try to avoid using hardcoded sleep because it slows your test.

How about using the implicit wait?

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

What it does:

  1. When the element is present, continue on your test
  2. When the element is not present (when the site is slow), it will throw a timeout.

That is much better test so you know why your test fails

More info here: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp




回答2:


This error comes if the element is not visible or enabled.

Here is two points from me 1. If you are hovering then properly using selenium just check if the element is visible or enabled. 2. Or use JavascriptExecutor. It can click on hidden elements with out opening them.

(JavascriptExecutor(webdriver)).executeScript("document.getElementsById('ext-pr-backlog-evolution').click();");


来源:https://stackoverflow.com/questions/21276350/how-to-mouse-hover-then-need-to-click-on-the-tab-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!