There is a big difference between the positioning of rich text boxes and ordinary text boxes. The common implementation of rich text boxes uses the Frame tag, and a complete HTML web page structure is implemented in the Frame, so it is impossible to directly locate rich text using the ordinary positioning mode. box object.
Taking http://mail.sohu.com as an example, two methods are introduced to read rich text boxes.
method one:
package cn.om.webdriverapi; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterMethod; public class TestRichTextBox1 { WebDriver driver; String url; @Test public void testSohuMailWriteEMail() { driver.get(url); //Set the timeout for getting elements to 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebElement username = driver.findElement(By.xpath(".//*[@id='theme']/form/div[1]/div[1]/input")); WebElement password = driver.findElement(By.xpath(".//*[@id='theme']/form/div[2]/div[1]/input")); WebElement sub = driver.findElement(By.xpath(".//*[@id='theme']/form/div[5]/input")); //Enter the account and password, and click to log in. Since there is a verification code, it will jump to the page where you enter the verification code username.sendKeys("fosterwu"); password.sendKeys("1111"); sub.click(); //Wait for 10 seconds and manually enter the verification code try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } //After entering the verification code, click Login again. sub.click(); //Mobile phone security authentication, click the [Next time] button WebElement notdo=driver.findElement(By.xpath(".//*[@id='theme']/div[1]/div[1]/div[4]/span[2]")); notdo.click(); WebDriverWait wait=new WebDriverWait(driver, 15); //Display wait for 15 seconds, by looking for the [Write Email] button, to determine whether it has been logged in to the mailbox wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='addSkinClass']/div[4]/div/ul/li[1]"))); //Locate the [Write Email] button, click it to enter the letter writing page WebElement writeMailButton=driver.findElement(By.xpath(".//*[@id='addSkinClass']/div[4]/div/ul/li[1]")); writeMailButton.click(); //Locate the sender input box and enter the data to be tested WebElement recipients=driver.findElement(By.xpath(".//*[@id='mailContent']/div/div[1]/div[1]/div[1]/div[1]/div/span/input")); recipients.sendKeys("[email protected]"); //Because after the sender enters it, you need to press Enter or click to edit the next one. So add the operation of clicking enter Robot robot=null; try { robot=new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace (); } robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); //Locate the theme input box and enter the data to be tested WebElement topic=driver.findElement(By.xpath(".//*[@id='mailContent']/div/div[1]/div[1]/div[4]/input")); topic.sendKeys("Letter to myself"); //Find the iframe element by tabname and switch to the iframe WebElement iframe=driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iframe); //Declare the JavaScriptExecutor object to execute the JavaScript script JavascriptExecutor js=(JavascriptExecutor)driver; //In the iframe, find the editing area object p, and set the text content in html format, that is, the body content js.executeScript("document.getElementsByTagName('p')[0].innerHTML='<b>The content of the mail to be sent<b>'"); //Switch to the default page. (main page) driver.switchTo().defaultContent(); //click send WebElement send=driver.findElement(By.xpath(".//*[@id='mailContent']/div/div[2]/span[1]")); send.click(); } @BeforeMethod public void beforeMethod() { url = "http://mail.sohu.com"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } }
The advantage of this method is that it can support HTML-formatted text as the text input content of the rich text box.
The disadvantage is that because the implementation mechanism of the rich text box of different web pages is different, it is difficult to locate the text editing area object of the rich text box. It is necessary to be proficient in understanding the meaning of HTML code and the method of entering and leaving the frame, and the ability to implement script positioning is relatively high. .
Method Two:
package cn.om.webdriverapi; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterMethod; public class TestRichTextBox2 { WebDriver driver; String url; @Test public void testRichTextBox2() { driver.get(url); // Set the timeout for fetching elements to 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebElement username = driver.findElement(By.xpath(".//*[@id='theme']/form/div[1]/div[1]/input")); WebElement password = driver.findElement(By.xpath(".//*[@id='theme']/form/div[2]/div[1]/input")); WebElement sub = driver.findElement(By.xpath(".//*[@id='theme']/form/div[5]/input")); // Enter the account, password, and click to log in. Since there is a verification code, it will jump to the page where you enter the verification code username.sendKeys("fosterwu"); password.sendKeys("1111"); sub.click(); // Wait for 10 seconds and manually enter the verification code try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } // After entering the verification code, click Login again. sub.click(); // Mobile phone security authentication, click the [Next time] button WebElement notdo = driver.findElement(By.xpath(".//*[@id='theme']/div[1]/div[1]/div[4]/span[2]")); notdo.click(); WebDriverWait wait = new WebDriverWait(driver, 15); // Display waiting for 15 seconds, and determine whether you have logged in to the mailbox by looking for the [Write Email] button wait.until( ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='addSkinClass']/div[4]/div/ul/li[1]"))); // Locate the [Write Email] button, click it to enter the letter writing page WebElement writeMailButton = driver.findElement(By.xpath(".//*[@id='addSkinClass']/div[4]/div/ul/li[1]")); writeMailButton.click(); // Locate the sender input box and enter the data to be tested WebElement recipients = driver .findElement(By.xpath(".//*[@id='mailContent']/div/div[1]/div[1]/div[1]/div[1]/div/span/input")); recipients.sendKeys("[email protected]"); // After the sender enters it, it is necessary to press Enter or click to edit the next one. So add the operation of clicking enter pressEnterKey (); // Locate the theme input box and enter the data to be tested WebElement topic = driver.findElement(By.xpath(".//*[@id='mailContent']/div/div[1]/div[1]/div[4]/input")); topic.sendKeys("Letter to myself"); // Switch to rich text box by tab key pressTabKey (); setAndctrlVClipboardData("The body content of the email sent"); //click send WebElement send=driver.findElement(By.xpath(".//*[@id='mailContent']/div/div[2]/span[1]")); send.click(); } @BeforeMethod public void beforeMethod() { url = "http://mail.sohu.com"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } public void pressTabKey() { Robot robot = null; try { robot = new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace (); } robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); } public void pressEnterKey() { Robot robot = null; try { robot = new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace (); } robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } public void setAndctrlVClipboardData(String s) { // Declare the StringSelection object and use the function's string parameter to complete the instantiation StringSelection stringSelection = new StringSelection(s); // Use the Toolkit object's setContents method to put the string on the clipboard Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); // Declare the Robot object Robot robot = null; try { robot = new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace (); } // Call KeyPress to press the Crtl key robot.keyPress(KeyEvent.VK_CONTROL); // Call KeyPress to achieve pressing the V key robot.keyPress(KeyEvent.VK_V); // Call KeyPress to release the V key robot.keyRelease(KeyEvent.VK_V); // Call KeyPress to release the crtl key robot.keyRelease(KeyEvent.VK_CONTROL); } }
The advantage of this method is that no matter what kind of rich text box, as long as you find the element immediately above it, you can enter the text editing area of the rich text box by pressing the tab key, and you can use one method to solve all types of problems. The positioning problem of the rich text box editing area.
The disadvantage is that HTML-formatted text input is not possible in the rich text box edit area.
The two methods each have their own advantages. In actual use, the choice should be made according to the actual situation.