Well, the solution of problem was found here How to type Gmail Body text in Selenium2 (Webdriver) using Java, but I wonder why did the old hpath worked so weird. Everyone thanks for your attention.
WebDriver + TestNG Gmail a strange error
Question
I tried to automate sending email from Gmail and worked with TestNG and Page Object pattern, browser FireFox, but first a part of code =)
I'm looking at locators with help annotation (Page Object pattern), and then goes to a function which I use in my test.
@FindBy (how=How.XPATH, using = "//div[@class='T-I J-J5-Ji T-I-KE L3']")
public WebElement writeMail;
@FindBy (how=How.XPATH, using = "//textarea[@class='vO']")
public WebElement adressTo;
@FindBy (how=How.XPATH, using = "//input[@class='aoT']")
public WebElement subjectOfMail;
@FindBy (how=How.XPATH, using = "//body[@role='textbox']")
public WebElement contentMail;
@FindBy (how=How.XPATH, using = "//div[@class='T-I J-J5-Ji aoO T-I-atl L3']")
public WebElement sendMail;
public void writeMail(String adress, String subject) {
writeMail.click();
adressTo.sendKeys(adress);
subjectOfMail.sendKeys(subject);
webDriver.switchTo().frame(webDriver.findElement(
By.xpath("//iframe[contains(@tabindex,'1') and contains(@frameborder,'0')]")));
webDriver.findElement(By.xpath("//body[@role='textbox']")).sendKeys("Test");
webDriver.switchTo().defaultContent();
sendMail.click();
}
allegedly has to work, but the record goes only in address and topic fields, and after that test is failed and in log:
" Unable to locate element: {"method":"xpath","selector":"//body[@role='textbox']"}"
But if I comment
//adressTo.sendKeys(adress);
//subjectOfMail.sendKeys(subject);
webdriver
switches to iframe
as planned and writes text in letter body and clicks a button "Send".
Please explain to me, why this code works fine as a simple java app, but not works correctly in when I use it in my test (TestNG) in my example?
No correct solution
OTHER TIPS
You're searching for a <body>
element with @role='textbox'
:
//body[@role='textbox']
but the element you're looking for actually is a <div>
container:
<div id=":c9" class="Am Al editable LW-avf" hidefocus="true" aria-label="Inhalt der Nachricht" g_editable="true" role="textbox" contenteditable="true" tabindex="1" style="direction: ltr; min-height: 416px;"><br></div>
So use following XPath instead:
//div[@role='textbox']