This is the code I use: html code changes when you do click, because of that I use different xpath. // Send message
WebElement msgBox = driver.findElement(By.xpath(".//*[@id='js_5y']/div[2]/div[2]/div"));
msgBox.click();
WebElement msg = driver.findElement(By.xpath("//*[@id='js_4a']/div[2]/div[2]/div/div/div/div/div/div/div/span/span"));
msg.sendKeys(msgValue);
And your code?
You might have to sort out escaping for the combination of single and double characters when you put that into your Java code
// Find the text input element by its name
WebElement msgOn = driver.findElement(new ByClassName("_4-fs"));
msgOn.click();
// Send message
WebElement msgBox = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[3]/div[5]/div/div/div/div/div[2]/div/div[2]/div[2]/div"));
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "if(document.createEventObject){ arguments[0].textContent = '" + msgValue
+ "';} else if(document.createEvent) { arguments[0].textContent = '" + msgValue + "';}";
js.executeScript(script, msgBox);
//BUTTON
WebElement buttonSubmit = driver.findElement(new By.ByCssSelector("._1mf7._4jy0._4jy3._4jy1._51sy.selected._42ft"));
buttonSubmit.click();
WebElement msgBox = driver.findElement(By.xpath(".//*[@id='js_5y']/div[2]/div[2]/div"));
msgBox.click();
WebElement msg = driver.findElement(By.xpath("//*[@id='js_4a']/div[2]/div[2]/div/div/div/div/div/div/div/span/span"));
msg.sendKeys(msgValue);
//WebElement msgBox = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[3]/div[5]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div/div/div/div/div/div/span/span"));
//WebElement msgBox = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[3]/div[5]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div/div/div[2]/div/div/div"));
//WebElement msgBox = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/div/div[3]/div/div[1]/div[2]/div[3]/div[5]/div/div/div/div/div[2]/div/div[2]/div[2]/div"));
// Find the text input element by its name
WebElement msgOn = driver.findElement(new ByClassName("_4-fs"));
msgOn.click();
// Send message
WebElement msgBox = driver.findElement(By.xpath("//*[@title=\"Escribe algo...\""));
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "if(document.createEventObject){ arguments[0].textContent = '" + msgValue
+ "';} else if(document.createEvent) { arguments[0].textContent = '" + msgValue + "';}";
js.executeScript(script, msgBox); I got this error: Exception in thread "AWT-EventQueue-0" org.openqa.selenium.InvalidSelectorException: The given selector //*[@title="Escribe algo..." is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //*[@title="Escribe algo..." because of the following error: SyntaxError: The expression is not a legal expression.
// Send message
WebElement msgBox = driver.findElement(By.xpath("//*[@title=Escribe algo..."));
msgBox.sendKeys(msgValue); - Error: Exception in thread "AWT-EventQueue-0" org.openqa.selenium.InvalidSelectorException: The given selector //*[@title=Escribe algo... is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Unable to locate an element with the xpath expression //*[@title=Escribe algo... because of the following error: SyntaxError: The expression is not a legal expression.
You need single quotes around the value you are searching for in the title attribute.
Check your xpath expressions in the browser console or firebug.