Rajith Kariyawsam
Rajith Kariyawsam

Reputation: 113

Text highlight with mouse actions in selenium with java not working

Using selenium and java, I need to highlight the below text using a mouse click and move, After highlighting, the text I can see another popup and I need to do some validation on that popup.

<h2 class="lstw">16.1 This is sample text, another, and heading</h2>

This is my current code and it is not working sometimes. Please note KeyDown or set CSS through javascript is cannot use here.

public boolean userMoveHighlightText(String text){
    String secName = "//h2[text()='"+text+"']";
    getFrame(); // switch to iframe
    By element = getLocator(secName, BY_TYPE.BY_XPATH);
    Actions action = new Actions(getWebDriver());
    WebElement content = $(element);
    
    action.moveToElement(content, 0, 0).clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2);
    action.release().build().perform();
    return true;
}

Upvotes: 0

Views: 541

Answers (1)

mk7644
mk7644

Reputation: 33

Replace the below code with the recommended:

action.moveToElement(content, 0, 0).clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2);

Recommended:

 action.moveToElement(content, 0, 0).build().perform()
//implicit wait 
 action.clickAndHold().moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getHeight() / 2).release().build().perform();

Upvotes: 1

Related Questions