C# .NET 4.0 Get the Value of an ID of a web element by reading the .aspx page with filestream or streamreader - filestream

So here's the problem I am writing a program that is going to look through the folder on the localhost before it is rendered. I want to be able to get the value of the ID of a web element and store that in a list object.
For example I have a page:
<html>
<body>
<CustomControl:MyPhoneValidationControl ID="PhoneValidator" validationgroup="PageValidation" />
<CustomControl:MyEmailValidationControl ID="EmailValidator" validationgroup="PageValidation" />
</body>
</html>
The program will go to C:\WebFolder\Page.aspx and then will read the file and find every CustomControl on the page and then grab the Control Type (MyPhoneValidationControl OR MyEmailValidationControl) and then assign the value of the ID (PhoneValidator, EmailValidator) as a property of the Control object.
I am using C#.NET 4.0. Getting the ID of the element is easy after the page is rendered but it doesn't show that it is a custom control. To see the custom control you need to look at the .aspx file without it being rendered by a web server (IIS, etc.)

I resolved myself this by stuffing the page data into a string and then using
string page = new StreamReader(page).ReadToEnd();
List<string> control = new List(string)();
MatchCollection matchControl = Regex.Matches(text, "expression");
foreach (Match match in matchControl)
{
control.Add(match.Value)
}
This catches the control to a list and then I can parse the string list to get the type and ID using Regex in the properties after passing the whole control List to the controls constructor.

Related

How to use XPATH to access an attribute/property of an iframe?

As you can see I'm not trying to access anything from inside the actual iframe's contentWindow but rather the iframe tag itself.
Example iframe element:
<iframe id="placeholder"
data-ids="1J2cSrn6ox4,BUQzSn85NMs,Fzav6plKfr8,MsNJTTP3LMM,hTZLXnY7Gc0"
style="100%; 330px;" frameborder="0" allowfullscreen="1"
allow="accelerometer; autoplay; encrypted-media; gyroscope;
picture-in-picture" width="100%" height="100%"></iframe>
I'm trying to retrieve the string that is assigned to the property data-ids
how can I retrieve this using XPath?
//iframe[1].data-ids
//*[#id="placeholder"]/#data-ids
If I use JavaScript getAttribute function works to retrieve:
var frame = document.querySelector('iframe');
var val = frame.getAttribute('data-ids');
alert(val);
but how to do it in Xpath?
Update 1
I'm using the inspector in Chrome, under elements, CTRL-F doing search for XPATH; it won't let me access the specific string I'm trying to reference:
//*[#id="placeholder"]//#data-ids
When I use this it doesn't work it still highlights the entire iframe.
Do you know how to access only the string between quotes assigned to "data-ids" attribute of the iframe tag?
Is there a command similar to the JS command getAttribute() in Xpath?
I want to return only "1J2cSrn6ox4,BUQzSn85NMs,Fzav6plKfr8,MsNJTTP3LMM,hTZLXnY7Gc0".
Update 2
Now I'm now using JavaScript "snippet" inside chrome's inspector to evaluate the Xpath reference like so:
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
test = getElementByXpath("//*[#id="placeholder"]//#data-ids")
console.log(test);
and it now outputs the following to the console:
data-ids="1J2cSrn6ox4,BUQzSn85NMs,Fzav6plKfr8,MsNJTTP3LMM,hTZLXnY7Gc0"
which is great, but how do I get just the text? Not the name/value pair, not the attrName="value" but rather just what's in between the quotes?
I'm not asking how to do it with JavaScript; I'm asking how to target the string value of the attribute without converting it to a string.
Ideally //iframe/#data-ids should return the value.
Can you please elaborate what message you are getting and where you are trying the xpath.

How can I access a form-Element, which has neither a name or an id?

How can I access a form-element, which has neither a name or an id?
I didn't find any useful methods for the class HtmlForm.
You can use getElementsByTagName but you should know the structure of html.
For example :
HtmlPage page = webClient.getPage("http://www.tizag.com/phpT/examples/formex.php");
HtmlElement form = (HtmlElement) page.getElementsByTagName("form").get(0);
System.out.println(form.asText());
The code above lets you access the first form element and prints its content as text to console .

AEM: How to dynamically get the current page path and pass it to the request parameter for drop down

options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?locale='some_locale'
if I put locale=en-us it works fine,
How can I dynamically get the current page locale and pass it to the request parameter ?
So, your servlet requires locale as one of the parameters to retrieve dropdown values. I can think of 2 options to do this.
Invoke the servlet with $Path as one of the parameters. $Path will give you the complete page path, all the way till jcr:content. options=/bin/services/myservlet.GET_DROPDOWN_VALUES.json?compNode=$PATH.
In your servlet, construct page object using $PATH, then as #jwepurchase mentioned, get the locale using page.getLanguage(false).
String compNodePath = (String) request.getParameter("compNode");
String pagePath = StringUtils.substringBefore(compNodePath, "jcr:content");
PageManager pageMgr = request.getResourceResolver().adaptTo(PageManager.class);
Page page = pageMgr.getContainingPage(pagePath);
Locale pageLocale = page.getLanguage(false);
getLanguage will look for jcr:language property in currentpage or its ancestors. This property gets set when you set the Language field in page properties (advanced tab) of your page(usually set in the root locale page) to an appropriate value. eg: If you set the language to english(us), jcr:language will be en_us.
Similar steps as option 1, if value in page property is not set, you can use currentPage.getAbsoluteParent(DEPTH_VAL) to retrieve the locale value. Not a recommended soln though.
com.day.cq.wcm.api.Page.getLanguage(false) will return the value of the jcr:language property on the page or the first parent page where it is sent. Generally this isn't set on every page.
If you have included Adobe's global.jsp or used the <cq:defineObjects/> tag in your JSP, you should find that "currentPage" is already in scope, providing access to a Page object.
I generally have a custom taglib function to make it easier to access via EL. But as a scriptlet I expect the following would work:
<%= ((Page)getPageContext().getAttribute("currentPage")).getLanguage(false)%>

Calling template snippets stored in a DB

I am using Play and its templating engine to generate web pages. The content of each page is (partially) stored in an SQL database, either as markup text or as plain HTML. Is it possible to include template snippets (without arguments) within this content?
Here is a small example. Let's say that I have some template #printText() and that this template has been imported at the top of the current view. The following content is stored in the database:
<div>
#printText()
</div>
Is it possible to pass this String to the view and render it properly (including the call to #printText())?
You can easily create for an example static method which will fetch snippet from the database by some key and use it in the view like:
<div id="footer_snippet">
#Html(fetchSnippet("footer"))
</div>
It just should find your snippet in the DB and return the markup HTML as a String

Need to find the tags under a tag in an XML using jQuery

I have this xml as part of the responseXml of an Ajax call:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
When I used this jQuery(responseXml).find("title").text(); the result is "Title".
I also tried jQuery(responseXml).find("title:first-child") but the result is [object Object].
I want to get the result:
<span style="color:#ffff00;"><strong>Title</strong></span>
Please let me know how to do this in jQuery.
Thanks in advance for any help.
Regards,
Racs
Your problem is that you cannot simply append nodes from one document (the XML response) to another (your HTML page). The issue is two-fold:
You can use jQuery to append nodes from the XML document to the HTML page. This works; the nodes appear in the HTML DOM, but they stay XML nodes and therefore the browser ignores the style attribute, for example. Consequently the text will not be yellow (#ffff00).
As far as I can see, jQuery offers no built-in way to get the XML string (i.e. a serialized node) from an XML node. jQuery can handle XML documents quite well, but there is no equivalent to what .html() does in HTML documents.
So to make this work we need to extract the XML string from the XML document. Some browsers support the .xml property on XML nodes (namely, IE), the others come with an XMLSerializer object:
// find the proper XML node
var $title = $(doc).find("title");
// either use .xml or, when unavailable, an XMLSerializer
var html = $title[0].xml || (new XMLSerializer()).serializeToString($title[0]);
// result:
// '<title><span style="color:#ffff00;"><strong>Title</strong></span></title>'
Then we have to feed this HTML string to jQuery so new, real HTML elements can be created from it:
$("#target").append(html);
There is a fiddle to show this in action: http://jsfiddle.net/Tomalak/QWHj8/. This example also gets rid of the superfluous <title> element.
Anyway. If you have a chance to influence the XML itself, it would make sense to change it:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
Just XML-encode the payload of <title> and you can do this in jQuery:
$("#target").append( $(doc).find("title").text() );
This would probably work:
$(responseXml).find("title").html();