Java 选择单个单词或字符串(双击)进行编辑

Java 选择单个单词或字符串(双击)进行编辑,java,eclipse,selenium,select,Java,Eclipse,Selenium,Select,我正在测试文本编辑器的功能,我希望能够选择文本(一个单词或“p”标记之间的字符串),然后通过单击按钮(B代表粗体,I代表斜体)进行编辑。很像Stackoverflow的编辑器。这将添加一个HTMLB标记,使其显示为粗体。 我知道下面的Xpath指向一个文本字符串,但我找不到如何选择此文本或如何选择此文本中的单词 这是页面源代码的一部分(文本编辑器位于iframe中) 引出一段文字: This is a line of text to test Bold 我想选择单词“Bold”(或整个句子,如

我正在测试文本编辑器的功能,我希望能够选择文本(一个单词或“p”标记之间的字符串),然后通过单击按钮(B代表粗体,I代表斜体)进行编辑。很像Stackoverflow的编辑器。这将添加一个HTMLB标记,使其显示为粗体。 我知道下面的Xpath指向一个文本字符串,但我找不到如何选择此文本或如何选择此文本中的单词

这是页面源代码的一部分(文本编辑器位于iframe中)

引出一段文字:

This is a line of text to test Bold

我想选择单词“Bold”(或整个句子,如果更简单的话),然后单击文本上方的按钮使其加粗,例如,您可以在
JLabel
s中使用
标记

我的想法如下:

boldButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            String formattedText = "<html><b>" + text + "</b></html>";
            label.setText(formattedText);
       }
}

italicButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            String formattedText = "<html><i>" + text + "</i></html>";
            label.setText(formattedText);
       }
}
请注意,我还没有尝试过这个,所以您可能会调整一些东西以使其工作。

试试这个

Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();

谢谢,但这将“简单地”添加html以获得所需的结果。我想通过在选择文本或单词后单击按钮来测试按钮是否有效。这是有效的!我不得不将代码稍微更改为:Actions a=newactions(driver);a、 双击(driver.findelelement(By.xpath(“/html/body/p[1]”)).perform();很好,那么请接受答案,如果它真的帮助你点击右边的标志在我的答案非常左边
boldButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            String formattedText = "<html><b>" + text + "</b></html>";
            label.setText(formattedText);
       }
}

italicButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            String formattedText = "<html><i>" + text + "</i></html>";
            label.setText(formattedText);
       }
}
// retrieve selected text from text area
String selectedText = textArea.getSelectedText();
// get whole text
StringBuffer text = new StringBuffer(textArea.getText());
// remove the selected text
text.remove(textArea.getSelectionStart(), textArea.getSelectionEnd());
// make the selected text, e.g., bold.
selectedText = "<b>" + selectedText + "</b>";
// add the bold text to the whole text
text.insert(textArea.getSelectionStart(), selectedText);
//eventually apply outer <html> tags
String result = "<html>" + text.toString() + "</html>";
// set new text (with bold element) to the text area
textArea.setText(result);
Actions a = new Actions(driver);
a.doubleClick(driver.findElement(By.xpath("/html/body/p[1]")).build().perform();