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();
다음의 조작을 실시하려고 해도, 이러한 회답은 기능하지 않습니다.
- 메뉴 항목 위에 마우스를 놓습니다.
- 호버 뒤에만 사용할 수 있는 숨겨진 요소를 찾습니다.
- 하위 메뉴 항목을 클릭합니다.
moveToElement 뒤에 'perform' 명령을 삽입하면 요소로 이동하며 하위 메뉴 항목이 잠깐 표시되지만 이는 호버가 아닙니다.숨겨진 요소는 발견되기 전에 즉시 사라지며 Element Not Found Exception이 발생합니다.두 가지 시도를 해봤습니다.
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);
[ Actions to hover ](동작)과 표준 WebDriver(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();
마우스를 위로 이동한 후 표시된 정보에 대해 다음 작업을 수행할 수 있습니다.
이것을 실장하는 예를 확인해 주세요.
public class HoverableDropdownTest {
private WebDriver driver;
private Actions action;
//Edit: there may have been a typo in the '- >' expression (I don't really want to add this comment but SO insist on ">6 chars edit"...
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();
}
}
상세한 것에 대하여는, 여기를 참조해 주세요.http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/
이 질문은 Protractor(Selenium의 자바스크립트 프런트 엔드)를 사용하여 Javascript 테스트에서도 동일한 작업을 수행할 수 있는 방법을 찾고 있었습니다.
프로젝터 1.2.0 및 웹 드라이버 2.1을 사용한 솔루션:
browser.actions()
.mouseMove(
element(by.css('.material-dialog-container'))
)
.click()
.perform();
또한 오프셋을 사용할 수 있습니다(요소의 위 및 왼쪽을 클릭하는 데 사용합니다).
browser.actions()
.mouseMove(
element(by.css('.material-dialog-container'))
, -20, -20 // pixel offset from top left
)
.click()
.perform();
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();
}
}
다음 작업을 수행할 수 있습니다.
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();
웹에 여러 카테고리가 있는 경우 첫 번째 방법을 사용합니다.당신이 원하는 메뉴는 두 번째 방법만 있으면 됩니다.
이 재사용 가능한 방법을 사용해 보십시오.
public void MoveThePoiterToElement(By by){
log.info("Moving the cursor to the element");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(by));
action.build().perform();
log.info("Cursor moved to the element");
}
시도해보니 정상적으로 작동했어
action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()
언급URL : https://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java
'itsource' 카테고리의 다른 글
동일한 창과 탭에서 URL 열기 (0) | 2022.09.13 |
---|---|
oracle Linux 7에 mariaDB를 설치하는 방법 (0) | 2022.09.13 |
XAMPP - 테이블 'C:\xampp\tmp\#sql3a10_4_4'에 대한 권한 문제.MAI' (0) | 2022.09.12 |
PHP는 Python의 목록 이해 구문과 동등합니까? (0) | 2022.09.12 |
배열에서 선택한 모든 확인란 가져오기 (0) | 2022.09.12 |