0

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;
}
2
  • We are missing debugging details. Please share your actual code including a link to that web page and clear description what are you exactly trying to do there. Commented Jan 24, 2022 at 7:20
  • I'm trying to highlight the text using a mouse click and there is no error on that, Since the application cannot access to public, I shared the element and it is inside the IFrame, also this is working on sometimes Commented Jan 26, 2022 at 4:04

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

I tried above but still the same issue, content does not highlight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.