Select a text and perform a click action
Asked Answered
H

4

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?

Harappa answered 2/4, 2012 at 14:14 Comment(3)
How about sending Ctrl+B keys using driver.sendKeys()?Araiza
AJ tnx for response But followig are my questions...1-the <B> button is on the browser iframe itself. Not from Keyboard. 2-And u did not mention how to select a text in selenium/webdriver.Harappa
Here is the answer for qn 2. #7516883Araiza
M
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();
Moss answered 2/4, 2012 at 22:32 Comment(0)
I
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(); 
Insupportable answered 12/9, 2017 at 6:17 Comment(0)
Z
1

driver.navigate().to("https://leafground.com/input.xhtml"); WebElement cls= driver.findElement(By.xpath("//div[@class="ql-editor ql-blank"]")); cls.sendKeys("this is rakshika " + Keys.CONTROL + "a"); driver.findElement(By.xpath("//button[@class="ql-italic"]\r\n")).click();

Zeller answered 29/10, 2023 at 13:16 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Kendallkendell
N
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);
Nonperformance answered 29/12, 2016 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.