Is there a way to find all div's on a page via their css class using a regexp if the classes match the regex? Below is an example snippet:
<div class="runtime">...</div>
<div class="runtime2">...</div>
<div class="runtime3">...</div>
I was hoping there is a way to get all divs via a regex because there could be more div's I want to find following that class format on the page but they change on a page by page basis.
For that format you can use a starts-with, and contains attribute selector
all('div[class^="runtime"], div[class*=" runtime"]', minimum:1)
The second selector is for the case where there's another class preceeding runtime... in the element. For a more general case there is no built in way to use a regex for class matching, although you could get an array of all the divs and then filter that based on the class attribute yourself (not going to be very performant)
Related
I think I might just be over complicating things instead of keeping it simple.
My question is: I want to capture the title of a blog post into a prop variable, and the author who wrote it into another prop variable.
My thought would be to create a page load rule focusing only on the path of /blog. From there I would scrape the page looking for the class that defines it, and then pass it into my prop through DTM.
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
I create a page rule pick my prop and set it as: div.field.field-name-title.innerText But when I set it, all I'm seeing being passed is the "div.field.field-name-title.innerText"
Am I tackling this in the wrong way?
The values you enter in a text field are literal, with the exception of %data_element% syntax, which signifies a reference to a Data Element (there are a couple of other built-in variable references, as well).
Point is, if you want to populate your Adobe Analytics variable from scraping page content, you need to create a Data Element that returns the desired value, and then reference the Data Element in the text field for the Adobe Analytics variable.
That aside, your selector is wrong. What you've done is some weird mix of css selector and javascript syntax.
Below is an example of what you can do, based on your posted HTML:
<div class="field field-name-title">
<h2>Online Education</h2>
<div class="field field-name-body">
<p>
<em> by Author Name</em>
</p>
</div>
</div>
Data Element: Article Title
First, create a Data Element to get the article title from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_title
Type: CSS Selector
CSS Selector Chain: div.field-name-title h2
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Data Element: Article Author
Next, create another Data Element to get the article author from the page, based on your html structure.
Go to Rules > Data Elements > Create New Data Element
Fill out the fields with the following:
Name: article_author
Type: CSS Selector
CSS Selector Chain: div.field-name-body em
get the value of: text
[X] Scrub whitespace and linebreaks using cleanText
Then, click Save Changes
Page Load Rule: Populate Variables
Finally, within the various form fields of your Page Load Rule, you can now reference your Data Elements with %data_element_name% syntax.
Tip: Once you start typing the Data Element name out (starting with % prefix), DTM will show an auto-complete dialog, listing Data Elements matched.
If you need to reference the Data Element within a javascript custom code box within the Page Load Rule, you can use the following syntax:
_satellite.getVar('data_element_name');
Where 'data_element_name' is the name of your Data Element.
Example:
s.prop1 = _satellite.getVar('article_title');
Note: Unlike the form field syntax, you should not wrap your Data Element's name with %
With a grails app and from a local database, I'm returning some text in a xml format.
I can return it well formed in a <textarea></textarea> tag with the correct indenting (tabulation, line return,...etc.)
I want to go a bit further. In the text I'm returning, there are some <img/> tags and I'd like to replace those tag by the real images themselves.
I searched around and found no solution as of now. I understood that you can't add an image to a textarea (other then in a background), and if I choose a div tag, I won't have the indenting anymore (and therefore, harder to read)
I was wondering if using a <g:textField/> or an other tag from the grails library will do the trick. And if so, How can I append them to a page using jquery.
For example, how to append a <g:textField/> in jquery. It doesn't interpret it and I get this error
SyntaxError: missing ) after argument list [Break On This Error]...+doc).append("<input type="text" id="FTMAP_"+nb_sec+"" ...
And in my javascript file, I have
$("#FTM_"+doc).append("<g:textField id='FTMAP_"+nb_sec+"' ... />
Any possible solutions ?
EDIT
I did forget to mention that my final intentions are to be able to modify the text (tags included) and to have a nice and neat indentation so that it is the easiest possible for the end user.
You are asking a few different questions:
1. Can I use a single HTML tag to include images inside pre-formatted text.
No. You will have to parse the text and translate it into styled text yourself.
2. Is there a tag in the grails standard tags to accomplish this for me?
No.
3. How can I add grails tags from my javascript code.
Grails tags are processed on the server-side, and javascript is processed on the client. This means you cannot directly add grails tags via javascript.
There are a couple methods that can accomplish the same result, however:
You can set a javascript variable to the rendered content of a grails tag. This solution is good for data that is known at the time of the initial request.
var tagOutput = "${g.textField(/* etc */)}";
You can make an ajax request for the content to be added. Then your server-side grails code can render the tags you need. This is better for realtime data, or data that will be updated more than once on a single rendered page.
I am using Capybara to write test in my application, but now i have a situation in which i need to read id of an element within capybara like
myid = page.find("#parentNode").first(".childClass").id
Consider i have the below HTML structure
<div id="parentNode">
<div id="childNode1" class="childClass">1</div>
<div id="childNode2" class="childClass">2</div>
</div>
Please Note : I am not trying to read the content of the child node, but the id. The above shown is for example.
Expected Output : childNode1 (id of first element with class childClass
You are almost near the answer. The only change is instead of calling id as method, you have to call it as attribute as follows
page.find("#parentNode").first(".childClass")[:id]
I would use some xpath instead of css in this case.
Note I am not that skilled in xpathing so I use css first to find parentNode.
find(#parentNode).find(:xpath, div[1]).id
Try that and see if it works.
optionally you can use css in the second find as well and use the class as criteria since it finds the first element anyway.
Got the answer..!!
We can use page.evaluate_script to achieve this. I used the below code
page.evaluate_script('$("#parentNode .childNode").first().attr("id")')
Hope this will help some one :)
Using RSpec and Capybara to test for the existence of an element within a div with class 'foo'.
<div class="foo">
<p>Text zzz</p>
Looking for element here
</div>
<div class="foo">
<p>Text aaa</p>
Element should not exist within this div.
</div>
There are many divs with class 'foo' on the page, and I can give them different ID's based on foo's ID in the database.
But I don't know foo's ID from within the test. And, I don't want to test the parent of the divs because an element should be present in one div and absent in another.
What is the best way to test for an element in this case?
If I understand the question correctly (and I'm not 100% confident I do), I think this should work:
el1 = find(:xpath, '//div[#class="foo"][./p[contains(.,"Text zzz")]]')
el2 = find(:xpath, '//div[#class="foo"][./p[contains(.,"Text aaa")]]')
There's probably a slightly simpler way to do this using css instead of xpath, but I've found that this works for this type of situation. (Note: I haven't actually tested this code.)
parent = find("p[text()='zzz']").find(:xpath,"..")
within parent do
...
https://github.com/jnicklas/capybara/pull/505
I use Orbeon Forms and I want to set a dynamic class name on a div or span (or any non-XForms tags like xforms:output) so that group of controls can be set with that CSS class.
<div class="color-with-{if(/fetch/data1='Yes') then 'green' else 'red'}">
But this is not working. When I look at the HTML source code after the form is rendered it seems it did not processed that dynamic part.
<div class="color-with-{if(/fetch/data1='Yes') then 'green' else 'red'}"
id="xf-279">
However this dynamic assignment works pretty good with Orbeon tags.
<xforms:output value="if ((xxforms:valid(instance('account-opening-setup'),true()))
and (xxforms:valid(instance('form-variables'),true())))
then 'Complete' else 'Incomplete'"
class="validation-{if ((xxforms:valid(instance('account-opening-setup'),true()))
and (xxforms:valid(instance('form-variables'),true()))) then 'complete'
else 'incomplete'}" />
Please advise which HTML tag I can use to dynamically assign a class name so that group of controls follow that class CSS.
Those expression that use the {...XPath...} syntax are called attribute value templates (AVT), and Orbeon Forms support them in a number of XForms attributes and HTML attributes, including class and style. So your code above should be working.
It might be the case that the AVT is not interpreted because your div isn't in the XHTML namespace. My recommendation is to always use a prefix on your elements. Define the xmlns:xhtml="http://www.w3.org/1999/xhtml" on the root element, and then use xhtml:div instead of div. If you find this too inconvenient, make sure you have the XHTML namespace as your default namespace declared on the root element, with xmlns="http://www.w3.org/1999/xhtml".