I was wondering if anybody knew how to change the contents of a HTML TextBox with C# I have tried to use body.SetAttribute("value", string) but I did not get anywhere. An example of what I am trying to do is <textarea rows="2" cols="20" id="body" class="messages-reply-box text-box text new-message-body">String I need changed</textarea> Here is what I got so far:
HtmlDocument doc = webBrowser1.Document;
HtmlElement bod = doc.GetElementById("body");
bod.SetAttribute("value", "text");
If you want change the Text of a Element inside your html page you should use InnerText property. This should work
webBrowser1.Document.GetElementById("body").InnerText ="text";
Related
I'm trying to get the text value of checkbox label. Here is an HTML code
<div id="content" class="large-12 columns">
<div class="example">
<h3>Checkboxes</h3>
<form id='checkboxes'>
<input type="checkbox"> checkbox 1</br>
<input type="checkbox" checked> checkbox 2
</form>
</div>
So What I have tried so far
Driver.FindElement(By.XPath("//input[#type='checkbox']")).Text;
Driver.FindElement(By.XPath("//input[#type='checkbox']")).GetAttribute("value");
Driver.FindElement(By.XPath("//input[#type='checkbox']")).GetAttribute("name");
Driver.FindElement(By.XPath("//input[#type='checkbox']")).GetAttribute("innerText");
Driver.FindElement(By.XPath("//input[#type='checkbox']")).GetAttribute("innerHTML");
Here is a screenshot
All this attempts return "".
Any ideas how to get it or Javascript is my only option ?
The xpath for a text following an input tag is //input[1]/following-sibling::text()[1] but there are serious limitations for Selenium to run expressions like this. It only can handle tag elements. Try to get the parent and retrieve texts from there.
string[] texts = Driver.FindElement(By.XPath("//form[#id='checkboxes']"))
.GetAttribute("innerText")
.Split("\r\n".ToCharArray()
);
Then texts[0] returns:
checkbox 1
Add an class or id attribute to the checkboxes and then try to find the element by css selector like: Driver.FindElement(By.CssSelector(""))
You can try following to get the required text from the two checkboxes:
Driver.FindElement(By.XPath("//form[#id='checkboxes']/input[#type='checkbox'][1]")).Text
Driver.FindElement(By.XPath("//form[#id='checkboxes']/input[#type='checkbox'][2]")).Text
Let me know, if above code does not work for you.
You can also use CSS Selector to get the checkbox label:
Element CheckBox = Driver.FindElement(By.CssSelector("input[type='checkbox']"));
string firstChkBoxTxt = CheckBox.FirstOrDefault().GetAttribute("innerText");
string secondChkBoxTxt = CheckBox.LastOrDefault().GetAttribute("innerText");
I need to append a div (with is content), after all tags with a specific css class (or data- tag) in code behind of asp.net c#.
Someone can help me? Basically it should work as "append" in jquery. I searched online but could not find anything, especially to "append" to a tag with a specific CSS class.
Thank you.
You can add a diva as an html generic control from the code behind
for example i have a div like this
<div id="test" runat="server">Some Content</div>
As i specified runat server it will be available there and i can add contents like this
HtmlGenericControl div= HtmlGenericControl();
div.TagName = "div"; //specify the tag here
//then specify the attributes
div.Attributes["height"] = "100%";
div.Attributes["width"] = "100%";
div.Attributes["class"] = "someclass";
div.Attributes["id"] = "someid";
test.Controls.Add(div);
I'm trying to insert some text after the first tag:
<body id="tinymce" spellcheck="false">
<p>
// I want to insert text here
<br>
</p>
</body>
My attempt so far hasn't worked:
IElement tinymice;
string testText = "some text here"
string xPath = string.Format("//body[#id='{0}']/p", "tinymce");
tinymice = GetElementByXPath(xPath);
tinymce.SendKeys(string.Format("{0}", testText ));
Put the text into a span. Paragraph tags are intended to be seperators.
From the docs here: http://jwebunit.sourceforge.net/apidocs/net/sourceforge/jwebunit/api/IElement.html
I infer that you can call:
tinymice.setTextContent("text to insert");
I googled "getelementbyxpath selenium" I hope these are the right docs, it does list selenium at the bottom of the page: http://jwebunit.sourceforge.net/apidocs/net/sourceforge/jwebunit/api/class-use/IElement.html
I realize this is probably a javascript package, but this may be the same methods for the C# dll as well.
I don't think the above person was aware that tinyMCE is tinyMCE and not Tiny Mice..
I'm using the HTML Agility Pack to manipulate and edit a HTML document. I want to change the text in the field such as this:
<div id="Div1"><b>Some text here.</b><br></div>
I am looking to update the text within this div to be:
<div id="Div1"><b>Some other text.</b><br></div>
I've tried doing this using the following code, but it doesn't seem to be working because the InnerText property is readonly.
HtmlTextNode hNode = null;
hNode = hDoc.DocumentNode.SelectSingleNode("//div[#id='Div1']") as HtmlTextNode;
hNode.InnerText = "Some other text.";
hDoc.Save("C:\FileName.html");
What am I doing wrong here? As mentioned above, the InnerText is a read only field, although it's written in the documentation that it "gets or sets". Is there an alternate method through which this can be done?
The expression is used here: //div[#id='Div1'] selects the div, which is not a HtmlTextNode, so the hNode variable holds null in your example.
The InnerText property is realy read-only, but HtmlTextNode has property Text which could be used to set the necessary value. But before this you should get that text node. This could be easily done with this expression: //div[#id='Div1']//b//text():
hNode = hDoc.DocumentNode
.SelectSingleNode("//div[#id='Div1']//b//text()") as HtmlTextNode;
hNode.Text = "Some other text.";
I want to create a textbox on the fly (in the comment section im creating). Now I would like your opinion on whats the best solution. I was thinking about using a webmethod and add a textbox control dynamically, but since this requires a call to the server I'm not sure if that's the best option. Or can i also spawn a textbox using plain old javascript and still getting it's value on postback?
Thanks again guys
Kind regards,
Mark
Put the TextBox using ordinary HTML input element on page and set its visibility property to collapsed using style tag then in code behind make it visible.
HTML Tag:
<input id="myTextBox" type="text" style="visibility: collapse;" />
Javascript:
var txt = document.getElementById("myTextBox");
txt.style.visibility = "visible";
Hope this helps!
You can create a new element using plain old JavaScript:
var textbox = document.createElement("textarea");
textbox.className = "my-textarea"; //Styling with CSS
document.getElementById("myelement").appendChild(textbox);