Write comment in span

659 views
Skip to first unread message

Noel Glez

unread,
Jan 27, 2016, 4:09:53 AM1/27/16
to Selenium Users
I'm found a problem fighting against following code. The problem is I have to write a comment and when I do it manually the comments is written in span. How can I do it? Is it possible?

<div class="_1mwp _1mwq _5bu_ _5yk1">
<div class="_5yk2" tabindex="-2">
<div class="_5rp7">
<div class="_1p1t">
<div class="_1p1v">Escribe algo...</div>
</div>
<div class="_5rpb">
<div class="_5rpu" contenteditable="true" aria-autocomplete="list" aria-expanded="false" aria-haspopup="false" aria-owns="js_18" role="combobox" spellcheck="true" title="Escribe algo...">
<div data-contents="true">
<div class="_45m_ _2vxa" data-block="true" data-offset-key="8g54i-0-0">
<span data-offset-key="8g54i-0-0">
<br data-text="true">
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Message has been deleted

Noel Glez

unread,
Jan 27, 2016, 4:33:42 AM1/27/16
to Selenium Users
The code after clicking and writing manually is:


<div class="_1mwp _1mwq _5bu_ _5yk1">
<div class="_5yk2" tabindex="-2">
<div class="_5rp7">
<div class="_5rpb">
<div class="_5rpu" contenteditable="true" aria-autocomplete="list" aria-expanded="false" aria-haspopup="false" aria-owns="js_1p" role="combobox" spellcheck="true" title="">
<div data-contents="true">
<div class="_45m_ _2vxa" data-block="true" data-offset-key="8g54i-0-0">
<span data-offset-key="8g54i-0-0">
<span data-text="true">example</span>

PeterJeffreyGale

unread,
Jan 27, 2016, 5:01:55 AM1/27/16
to Selenium Users
What does the screen actually look like, and what code are you executing to input the text? 

Noel Glez

unread,
Jan 27, 2016, 5:21:29 AM1/27/16
to Selenium Users

It is a fb page, see image below:

PeterJeffreyGale

unread,
Jan 27, 2016, 5:34:07 AM1/27/16
to Selenium Users
And your code?

Noel Glez

unread,
Jan 27, 2016, 5:51:19 PM1/27/16
to Selenium Users
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);

El miércoles, 27 de enero de 2016, 11:34:07 (UTC+1), PeterJeffreyGale escribió:
And your code?

PeterJeffreyGale

unread,
Jan 27, 2016, 7:33:06 PM1/27/16
to Selenium Users
Have allowed time for the ajax to ginidh after the click?

Xiang Dong

unread,
Jan 27, 2016, 11:29:54 PM1/27/16
to seleniu...@googlegroups.com
you are not tries to write comment to the span, instead of, the text should be write to parent <div>, <div class="_5rpu" contenteditable="true" aria-autocomplete="list" aria-expanded="false" aria-haspopup="false" aria-owns="js_1p" role="combobox" spellcheck="true" title="">this div is editable.

you can't use sendKeys method for a div because original div is not an editable element, instead of it, you can use Javascript to do it

JavascriptExecutor js = (JavascriptExecutor) webDriver;
String script = "if(document.createEventObject){arguments[0].textContent = '" + data
+ "';} else if(document.createEvent) { arguments[0].textContent='" + data + "';}";
js.executeScript(script, webElement);
Hope it works for you.

Best Regards,
--david


Date: Wed, 27 Jan 2016 14:51:19 -0800
From: noelgl...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Write comment in span
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6c8e380e-99fb-43e0-8316-88ca4370a992%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

PeterJeffreyGale

unread,
Jan 28, 2016, 5:12:59 AM1/28/16
to Selenium Users
What happens if you just use sendkeys to send your input text to the element identified by the xpath: //*[@title="What\'s on your mind?"]

You might have to sort out escaping for the combination of single and double characters when you put that into your Java code

Noel Glez

unread,
Jan 29, 2016, 8:30:28 PM1/29/16
to Selenium Users
When I run this script I get:
arguments[0] is undefined

This is my 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();

Noel Glez

unread,
Jan 29, 2016, 8:54:19 PM1/29/16
to Selenium Users
I try easy thing like: Hello, Good night... but it's still not working...

msgValue = "Hello";

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


Xiang Dong

unread,
Jan 31, 2016, 8:13:57 AM1/31/16
to seleniu...@googlegroups.com
Make sure you find the element properly, you can check does the return element is null or not. BYW, my fault, you can find the <span> and using following script. <span> inherit contenteditable attribute from the div

Best Regards,
--david


Date: Fri, 29 Jan 2016 17:30:28 -0800
From: noelgl...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Write comment in span

Noel Glez

unread,
Feb 2, 2016, 6:41:22 PM2/2/16
to Selenium Users
This is part of the code:
<div id="PageComposerPagelet_1547686802184107" data-referrer="PageComposerPagelet_1547686802184107">
<div class="_5ay5">
<div class="_4-u2 _4-u8">
<div id="u_jsonp_3_l">
<div id="u_jsonp_3_l" class="_36bx _4-u2 _4-u8" data-reactroot="" data-testid="react-composer-root">
<div class="_4zoz _4-u3">
<div id="js_1t">
<div class="clearfix">
<div class="_4bl9">
<div class="_1mwp _1mwq _5bu_ _5yk1">
<div class="_5yk2" tabindex="-2">
<div class="_5rp7">
<div class="_5rpb">
<div class="_5rpu" contenteditable="true" aria-autocomplete="list" aria-expanded="false" aria-haspopup="false" aria-owns="js_1s" role="combobox" spellcheck="true" title="">
<div data-contents="true">

This is the code I tried to get the right element to write in:

//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"));

Any idea of what I'm doing wrong? 

Thanks for your time,
Noel

PeterJeffreyGale

unread,
Feb 2, 2016, 7:03:11 PM2/2/16
to Selenium Users
Did you try my last suggestion?

PeterJeffreyGale

unread,
Feb 2, 2016, 7:03:12 PM2/2/16
to Selenium Users

Noel Glez

unread,
Feb 3, 2016, 5:46:06 PM2/3/16
to Selenium Users
Yes, I did. It's a script tag. Check please the source code of this page:

https://www.facebook.com/Galicia-Alive-1405896593013485

My code is:
        // 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.

PeterJeffreyGale

unread,
Feb 3, 2016, 5:56:24 PM2/3/16
to Selenium Users
That's not what I suggested. You shouldn't need to use JavaScript.

Noel Glez

unread,
Feb 3, 2016, 6:21:42 PM2/3/16
to Selenium Users
That one?

"Make sure you find the element properly, you can check does the return element is null or not. BYW, my fault, you can find the <span> and using following script. <span> inherit contenteditable attribute from the div"

Could you be more specific, please?

PeterJeffreyGale

unread,
Feb 3, 2016, 6:28:52 PM2/3/16
to Selenium Users
You are quoting David ... who are you expecting to answer?

Noel Glez

unread,
Feb 7, 2016, 9:10:02 PM2/7/16
to Selenium Users
Sorry Peter!

You are right! I did what you said:
"What happens if you just use sendkeys to send your input text to the element identified by the xpath: //*[@title="What\'s on your mind?"] You might have to sort out escaping for the combination of single and double characters when you put that into your Java code"
But it doesn't work:
- Code:
// 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.

PeterJeffreyGale

unread,
Feb 8, 2016, 1:52:11 AM2/8/16
to Selenium Users
Your xpath is invalid, as the error message is telling you.

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.

youness faceboky

unread,
May 20, 2018, 7:43:09 AM5/20/18
to Selenium Users
bonjour 

avez vous trouvez la solution de ce problème ? 
 je suis bloquer sur cette point  
Reply all
Reply to author
Forward
0 new messages