2

I am trying to select text which is already present on the browser.

I want to select that particular text and perform right click operation on it. However, the page on the browser has disabled right click.

How can I select text in such situation?

1
  • Select as in highlight the text? Commented Jul 4, 2014 at 14:15

2 Answers 2

4

Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.

I don't know if it will be OK for you because your website may be relying on javascript for essential features.

However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :

FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);

I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer : Select an Option from the Right-Click Menu in Selenium Webdriver - Java

Edit:

I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.

driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();

However it only highlighs one word in the html element that contains your text, so it's not really convenient.

I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :

Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();

It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).

I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described

Sign up to request clarification or add additional context in comments.

7 Comments

There are several ways to select a text (double click, click and hold then drag, CTRL+F and type , ..) What kind of text you want to select ? Only one word, several, a whole paragraph ?
Mostly click and hold then drag. You can say me any of that way.I want to try all.
Some words and paragraph both.Thank you very much. But can you give another example based some other type search e.g. with drag and move type.I also want to apply mouse event.
I didn't succeed in performing a drag and move to select the text, maybe it's not possible with Selenium, let's see if someone else has managed to do this
It is fine.Thank you very much.
|
0

Move to middle of the element

Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();

Move to starting of element, click and hold, move to end

Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();

Comments

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.