9

I'd like to select some text and perform a click action - like in Winword where we click Bold after selecting some text...

I have to select the text and click on the <B> bold icon in the textarea.

Any idea on how to do this using Selenium/Webdriver?

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
smriti
  • 1,124
  • 5
  • 14
  • 35
  • How about sending Ctrl+B keys using driver.sendKeys()? – A.J Apr 02 '12 at 15:30
  • AJ tnx for response But followig are my questions...1-the button is on the browser iframe itself. Not from Keyboard. 2-And u did not mention how to select a text in selenium/webdriver. – smriti Apr 02 '12 at 16:05
  • Here is the answer for qn 2. http://stackoverflow.com/questions/7516383/how-to-manipulate-user-selected-text-using-webdriver – A.J Apr 02 '12 at 17:14

3 Answers3

9

In Java, The Advanced User Interactions API has your answer.

// the element containing the text
WebElement element = driver.findElement(By.id("text"));
// assuming driver is a well behaving WebDriver
Actions actions = new Actions(driver);
// and some variation of this:
actions.moveToElement(element, 10, 5)
    .clickAndHold()
    .moveByOffset(30, 0)
    .release()
    .perform();
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
1

I tried with Action builder and played with offset. It worked for me.

Actions action = new Actions(driver);
action.moveToElement(wblmt,3,3).click().keyDown(Keys.SHIFT).moveToElement(wblmt,200, 0).click().keyUp(Keys.SHIFT).build().perform(); 
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
Siju Vasu
  • 11
  • 1
0

I tried this way and it did not work. Here are the codes:

System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com.vn");
    driver.manage().window().maximize();

    WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
    Actions actions = new Actions(driver);
    actions.moveToElement(text, 10, 5).clickAndHold().moveByOffset(30, 0).release().perform();

I switched to JavascriptExecutor and it worked:

    System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com.vn");
    driver.manage().window().maximize();

    WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
    JavascriptExecutor js = (JavascriptExecutor) driver;

    js.executeScript("arguments[0].setAttribute('style', 'background: blue;');", text);
Ho Nguyen
  • 1
  • 1