0

Selenium, NUnit testing, C#, Visual Studio.

How, in Selenium WebDriver, can I locate element in a page source that looks like following, and set some text in its <p> tag:

<body contenteditable="true" class="cke_editable cke_editable_themed cke_contents_ltr cke_show_borders" spellcheck="false">
<p></p>
</body>

This is body tag from CKEditor component present on a page (not a main page <body> element ). Actually, I need to set some text in <p> element. What is confusing to me , is that class attribute is complicated, contains from several strings. I am aware of command: driver.findElement( By.className( "some_class_name" )); but how to use it in this case and to set some text in <p> element?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Vladimir
  • 1,624
  • 7
  • 25
  • 56
  • 1
    This answer http://stackoverflow.com/a/24061054/3124333 uses a slightly different editor and Java, but the approach should be the same. – SiKing Oct 10 '14 at 15:38

3 Answers3

0

If you give the p tag an ID like so

<p id="derp">Text here</p>

You can send text to it using Selenium like this

driver.find_element_by_id("derp").sendKeys("herp");

Hope this helps!

EDIT: Without adding an ID to the element, you might be able to do something like this

driver.findElement(By.className("some_class_name")).findElement(By.tagName("p")).sendKeys("herp");
Blutzer
  • 11
  • 2
0

If you want the p elelement then this relative xpath should work.

//body[@class='cke_editable cke_editable_themed cke_contents_ltr cke_show_borders']/p

That is assuming that there is only a single body element with this class attribute.

CynicalBiker
  • 579
  • 5
  • 14
0

As you are saying, there is no id usable for location, so you have to come up with a different solution.

Selenium is capable of using css selectors - it's the same scheme found in CSS files to specify to which elements the following styling rules should apply.

One possible locator would be the following:

body.cke_editable.cke_editable_themed.cke_contents_ltr.cke_show_borders > p

Advantage over XPath: CSS selectors are aware about groups, so they don't handle them only as strings. Using just an XPath expression against the exact class attribute, your recognition would fail if there would be another, new class withing the attribute. Using CSS selectors, it's possible to really just identify per class.

Simplified and boiled down to the classes that really describe your editable element, the following should be sufficient:

body.cke_editable.cke > p
stuXnet
  • 4,309
  • 3
  • 22
  • 31