AnhPhan61
AnhPhan61

Reputation: 9

Using sendKeys() without clicking on element in Selenium

I want to input a string then format it using Cleditor. However, when I clicked on the B icon then clicked back on the text area frame to input the text, it lost the bold effect. Then I found out that if I clicked on the B icon, then input the text immediately WITHOUT clicking back on the text area frame, the text would be bold. Unfortunately I use sendKeys() right after the clicking on B icon code, so it clicks on the text area frame and lose all the bold effect. Here's my code:

clickElement(driver.findElement(By.xpath("//div[@title='boldText']"))); **//click on the bold icon**
**//switch to the textarea frame**
clickElement2();
driver.switchTo().frame(0);
Thread.sleep(1000);
new Actions(driver).sendKeys(driver.findElement(By.xpath(".//*[@class='cleditor-content']")), "abc").perform();

Is there anyway I can input the text without clicking on the text area? Any solution will be appreciated.

Upvotes: 0

Views: 3496

Answers (1)

Rain9333
Rain9333

Reputation: 570

As I suggest before in the comments, try to input the text, highlight it and then click the B (bold) button.

Example of that in C# code (should be pretty similar in Java):

    var actions = new Actions(WebDriver);
    actions.SendKeys(< yourElement >, "your text").KeyDown(Keys.LeftShift).SendKeys(Keys.Home)
                    .KeyUp(Keys.LeftShift).Build().Perform();

    boldButton.Click();

Upvotes: 0

Related Questions