How to retrieve data from the Content Picker with Razor? - razor

I try to use the Content picker w/o success..
I created a doc type which the properties are Content pickers, so what I try to do is along the magazine to use this page to pick up the features news / etc and display them on home page. w/o success..
I used something like:
dynamic feature_news_item = Library.NodeById(1111); (tried also the same but with Model: Model.NodeById(1111); Let's say for the example that 1111 is the id of this page which store the content pickers.
So again, the property of the page is "Content Picker" and the alias is featureItemNews
Now, I try to get / display the ID which the Content Picker (as much as I know..) should retrieve but just get errors (or nothing...) - tried this for example:
var node = #Library.NodeById(feature_news_item.contentPicker);
#node.Name
Doesn't show the Url nor the Name
So I tried
var node = #Library.NodeById(feature_news_item.featureItemNews);
int story1 = #feature_story.featureItemNews
Nothing works
Again featureItemNews is the Alias
and feature_news_item is just to get the Node with the Content picker (see at the beginning of the question)
All I try now is just to get the ID of the selected node which store in the content picker and display it / use it
What's the magic?
Thanks a lot for your help!

I've added Content Picker with alias (and name): myTestContentPicker
In razor code
:
var current = umbraco.NodeFactory.Node.GetCurrent();
int id = current.GetProperty<int>("myTestContentPicker");
< h1>#id< /h1>
It shows the ID of node I have selected. Then I can do whatever I want with this node:
var node = new Node(id);

Related

Get A Google Doc Element By ID with Google App Script

Question:
I am trying to avoid looping through the document until I find the tag and then grabbing the parent. Does anyone know if there is a way to find a google doc element by an id or handle?
Context:
I have a script that opens a defined template doc; copies it into a new document element by element, and then replaces any mustache tags with the values from a passed in object using the replaceText function. This part works fine, but there is one section where I would like to repeat a table row based on the object value being an array. Unfortunately I can not seem to figure out how to locate the table in the template doc other than something like:
if('{{ some tag }}' in aElement){
var repeatableElement = aElement.getParent();
if(repeatableElement){
goRunRepeatFunction();
}
}
Just seems like there should be a way to jump directly to an element by id.
Your task can be accomplished by using this snippet:
function getElement() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var numOfElements = body.getNumChildren();
var element = body.getChild(INDEX_OF_THE_CHILD).asText();
console.log(element.getText());
}
This gathers the body of the document and retrieves the number of children it has. This number represents the elements the document has. So, in order to access a certain element, you just have to make use of the getChild(index) method
Note
Please bear in mind that the getNumChildren() method will end up returning the number of all the elements in the document, including empty lines - these will be considered elements as well.
Usually, when working with a document in Apps Script, the elements are retrieved by their types in order to preserve them. So for example, if you have images in your document, it'd be best to retrieve those by using the getImages() and this will end up returning a list of containing all the images in the document, with each image being accessible by their index.
Another method would be to retrieve all the paragraphs in the body by using the getParagraphs() method, but if you know the exact structure of your document, you can retrieve all the elements by their type, and then access them as such.
Reference
Class ContainerElement - getNumChildren();
Class ContainerElement - getChild(Integer);
Class Body - getParagraphs().

Chrome Driver: where is dynamic div element?

I'm trying to scrape the price from this page: https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2
The price is in a div that I can see in the element using Dev Tools but it's not in the page source so my research tells me that it's probably a dynamically generated div element. More reading recommended Chrome Driver which returns the data with the dynamic div element but I'm finding it's not there. It's just like viewing page source from the browser.
ChromeDriver driver = new ChromeDriver(#"C:\chromedriver");
driver.Url = "https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2";
driver.Navigate();
var source = driver.PageSource;
var pathElement = driver.FindElementById("Price-group");
Is there another step for ChromeDriver to retrun the data with the dynamic divs?
Thanks
Price-Group is not an id. it is a class. Try with FindElementByClassName. Hope that club value is selected before findind the price. Coz, I see price value is displayed only fater selecting club
var pathElement = driver.FindElementByClassName("Price-group");

Setting the content of a span element,populated from a list using selenium

I am trying to set the content of a span element using selenium but seems it is not getting set as the value. The span field is a value which is filled in from a list as the user types in.
Further details on the problem
1)Go to http://www.goeuro.es.
2)Tried setting the destination field as under:
a)If the user types berlin in the destination field,it gets replaced by the text (this is getting populating from their js)
Berlín Alemania
I tried setting this using the following code.
#FindBy(xpath = ".//*[#id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[1]")
private WebElement fromSearchFieldSpan1;
#FindBy(xpath = ".//*[#id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[2]")
private WebElement fromSearchFieldSpan2;
.
.
.
.
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Berlín'", fromSearchFieldSpan1);
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Alemania'", fromSearchFieldSpan2);
When I tried with the above provided code,it seemed as it set the text as the title of the said fields as I was getting a input validation error upon submitting the page(as if these values were not entered at all).
Screenshot attached.
I can't set the value of this field using sendKeys() since this is not a text field.
Any thoughts how this can be set using selenium web driver.?
You're trying to handle span element while you should handle input. Try below code:
#FindBy(xpath = "//input[#id='$city']")
private WebElement fromSearchFieldSpan;
fromSearchFieldSpan.sendKeys('Berlin Alemania')

Find id of element on already made google form?

I have a form made on google docs, and I have a script that I've just created (that isn't complete) that will analyze the responses on the form, and validate them based on info from a different google doc. Through trial and error I have figured out the id's for all of the elements on said form. I used:
var body = ""
var bodyTitle = ""
for (var i = 1; i < its.length; i ++)
{
body = body + " " +its[i].getId()
bodyTitle = bodyTitle + " " + its[i].getTitle()
}
and then by logging that out and basically just counting, the 5th id matches the 5th title, I have all of the id's. I'm just wondering for next time, is there a way I can look at the form and find it'd ID without doing this?
With HTML forms (I use chrome fyi) you can right click, inspect element, and you can see the id for page elements, and refer to them by that id in javascript. Is there a way to do this in Google Forms? that way I don't have to screw with just logging out all the id's and titles and matching them up.
Keep in mind: these are all forms created in Google Forms by users, they insist on making them. None of them are code made so I haven't been able to code the id's myself.
Something like this will show you the ids in the logger... Open the form edit menu, load the script editor, paste the below inside a function and run it.
var form = FormApp.getActiveForm();
var items = form.getItems();
for (var i in items) {
Logger.log(items[i].getTitle() + ': ' + items[i].getId());
}
To see the IDs manually in Chrome >> View Form >> right-click on the question Label >> Inspect element >> look for the for="entry_XXXXXXX" attribute. That 'xxxxxxx' will be the id.
2021 update:
To find the ID manually:
Inspect question element
Find [data-params] attribute
The second number will be the right one
Example:
data-params="%.#.[860146445,"Question",null,0,[[305915825,[],true,[],[],null,null,null,null,null,[null,[]]]],null,null,null,[]],"i1","i2","i3",false]"
305915825 - this is the field ID that you can use like ?entry.305915825=<your-predefined-content>
An update to Bryan P's answer:
To find the ID:
view the form (as if you were taking it)
right click and inspect the question name
look for either:
data-item-id="##########", or
aria-describedby="i.desc.########## i3"
You want the ##########.

Get page selection including HTML?

I'm writing a Chrome Extension, and I was wondering if it was possible to get the selected text of a particular tab, including the underlying HTML? So if I select a link, it should also return the <a> tag.
I tried looking at the context menu event objects (yes, I'm using a context menu for this), and this is all that comes with the callback:
editable : false
menuItemId : 1
pageUrl : <the URL>
selectionText : <the selected text in plaintext formatting, not HTML>
It also returns a Tab object, but nothing in there was very useful, either.
So I'm kind of at a loss here. Is this even possible? If so, any ideas you might have would be great. Thanks! :)
Getting the selected text of a page is fairly easy, you can do something like
var text = window.getSelection().toString();
and you'll get a text representation of the currently selected text that you can pass from a content script to a background page or a popup.
Getting HTML content is a lot more difficult, mostly because the selection isn't always at a clean HTML boundary in the document (what if you only select a small part of a long link, or a few cells of a table for example). The most direct way to get all of the html associated with a selection is to reference commonAncestorContainer, which is a property on a selection range that corresponds with the deepest node which contains both the start and end of the selection. To get this, you'd do something like:
var selection = window.getSelection();
// Only works with a single range - add extra logic to
// iterate over more ranges if needed
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
var html = container.innerHTML
Of course, this will likely contain a lot of HTML that wasn't actually selected. It's possible that you could iterate through the children of the common ancestor and prune out anything that wasn't in the selection, but that's going to be a bit more involved and may not be necessary depending on what you're trying to do.
To show how to wrap this all up into an extension, I've written a short sample which you can reference:
http://github.com/kurrik/chrome-extensions/tree/master/contentscript-selection/
If you don't want all of the siblings, just the selected HTML, use range's other methods like .cloneContents() (to copy) or .extractContents() (to cut).
Here I use .cloneContents():
function getSelectedHTML() {
var range = window.getSelection().getRangeAt(0); // Get the selected range
var div = document.createElement("div");
div.appendChild(range.cloneContents()); // Get the document fragment from selected range
return div.innerHTML; // Return the actual HTML
}