Programing

Java를 사용하여 Selenium WebDriver에서 마우스 오버 기능을 수행하는 방법은 무엇입니까?

lottogame 2020. 7. 14. 08:19
반응형

Java를 사용하여 Selenium WebDriver에서 마우스 오버 기능을 수행하는 방법은 무엇입니까?


드롭 다운 메뉴에서 마우스 오버 기능을 수행하고 싶습니다. 메뉴 위로 마우스를 가져 가면 새로운 옵션이 표시됩니다. xpath를 사용하여 새 옵션을 클릭하려고했습니다. 그러나 메뉴를 직접 클릭 할 수는 없습니다. 따라서 수동 방식으로 드롭 다운 메뉴 위로 마우스를 가져 가려고하면 새 옵션을 클릭합니다.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).build().perform();

실제로 '마우스 호버'작업을 수행하는 것은 불가능합니다. 대신 달성하려는 모든 작업을 한 번에 연결해야합니다. 따라서 다른 체인을 드러내는 요소로 이동 한 다음 동일한 체인에서 이제 밝혀진 요소로 이동하여 클릭하십시오.

액션 체인을 사용하는 경우 '사용자처럼 행동하는 것'을 기억해야합니다.

Actions action = 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();

다음을 시도 할 때 이러한 답변 중 어느 것도 작동하지 않습니다.

  1. 메뉴 항목 위로 마우스를 가져갑니다.
  2. 호버 후에 만 ​​사용할 수있는 숨겨진 요소를 찾으십시오.
  3. 하위 메뉴 항목을 클릭하십시오.

moveToElement 다음에 'perform'명령을 삽입하면 요소로 이동하고 하위 메뉴 항목이 잠시 동안 표시되지만 호버가 아닙니다. 숨겨진 요소는 즉시 사라져 ElementNotFoundException이 발생합니다. 나는 두 가지를 시도했다.

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();

이것은 나를 위해 작동하지 않았습니다. 다음은 나를 위해 일했습니다.

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);

동작을 사용하여 호버링하고 표준 WebDriver를 클릭하면 호버링 한 다음 클릭 할 수 있습니다.


블로그 게시물을 기반으로 Selenium 2 Webdriver에서 다음 코드를 사용하여 호버링을 트리거 할 수있었습니다.

String javaScript = "var evObj = document.createEvent('MouseEvents');" +
                    "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
                    "arguments[0].dispatchEvent(evObj);";


((JavascriptExecutor)driver).executeScript(javaScript, webElement);

이 코드는 완벽하게 작동합니다.

 Actions builder = new Actions(driver);
 WebElement element = driver.findElement(By.linkText("Put your text here"));
 builder.moveToElement(element).build().perform();

마우스를 올린 후 공개 된 정보에 대해 원하는 다음 작업을 수행 할 수 있습니다.


이 예제를 어떻게 구현할 수 있는지 확인하십시오.

enter image description here

public class HoverableDropdownTest {

    private WebDriver driver;
    private Actions action;

    Consumer < By > hover = (By by) - > {
        action.moveToElement(driver.findElement(by))
              .perform();
    };

    @Test
    public void hoverTest() {
        driver.get("https://www.bootply.com/render/6FC76YQ4Nh");

        hover.accept(By.linkText("Dropdown"));
        hover.accept(By.linkText("Dropdown Link 5"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4.1"));
    }

    @BeforeTest
    public void setupDriver() {
        driver = new FirefoxDriver();
        action = new Actions(driver);
    }

    @AfterTest
    public void teardownDriver() {
        driver.quit();
    }

}

For detailed answer, check here - http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/


I found this question looking for a way to do the same thing for my Javascript tests, using Protractor (a javascript frontend to Selenium.)

My solution with protractor 1.2.0 and webdriver 2.1:

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
)
.click()
.perform();

This also accepts an offset (i'm using it to click above and left of an element:)

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
  , -20, -20  // pixel offset from top left
)
.click()
.perform();

Sample program to mouse hover using Selenium java WebDriver :

public class Mhover {
    public static void main(String[] args){
       WebDriver driver = new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get("http://www.google.com");
       WebElement ele = driver.findElement(By.id("gbqfba"));
       Actions action = new Actions(driver);
       action.moveToElement(ele).build().perform();
    }
}

You can try:

WebElement getmenu= driver.findElement(By.xpath("//*[@id='ui-id-2']/span[2]")); //xpath the parent

Actions act = new Actions(driver);
act.moveToElement(getmenu).perform();

Thread.sleep(3000);
WebElement clickElement= driver.findElement(By.linkText("Sofa L"));//xpath the child
act.moveToElement(clickElement).click().perform();

If you had case the web have many category, use the first method. For menu you wanted, you just need the second method.

참고URL : https://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java

반응형