使用sendKeys()而不单击Selenium中的元素

时间:2018-12-19 06:40:26

标签: selenium sendkeys html-input cleditor

我想输入一个字符串,然后使用Cleditor对其进行格式化。但是,当我单击 B 图标,然后又在文本区域框架上单击以输入文本时,它失去了加粗效果。然后我发现,如果单击 B 图标,然后立即输入文本而无需在文本区域框架上单击,则该文本将为粗体。 不幸的是,我在单击B图标代码后立即使用sendKeys(),因此它单击了文本区域框架,并且失去了所有粗体效果。这是我的代码:

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();

我是否可以在不单击文本区域的情况下输入文本?任何解决方案将不胜感激。

1 个答案:

答案 0 :(得分:0)

正如我之前在评论中建议的那样,请尝试输入文本,突出显示该文本,然后单击B(粗体)按钮。

在C#代码中的示例(在Java中应该非常相似):

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

    boldButton.Click();
相关问题