Extract URL using xpath - html

What xpath should i use to extract the "URL" and title="TEXT" from this html code:
<div class="VersionAnglaise"> <a href="URL" title="Version Anglaise"
class="LienVersionAnglaise"><strong>Version anglaise</strong></a> </div>
Thanks in advance.

To get title attribute :
//div[#class='VersionAnglaise']/a/#title
To get href attribute :
//div[#class='VersionAnglaise']/a/#href
You can combine both using XPath union (|) :
//div[#class='VersionAnglaise']/a/#title | //div[#class='VersionAnglaise']/a/#href

Related

I can't get an attribute to show up in my anchor tag

I need to get rel="" into this html. This is part of AEM, so I have an xml file doing this:
content.xml
<rel
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldDescription="HTML attribute to apply to the component."
fieldLabel="Rel"
name="./rel"/>
I've tried just duplicating how id is handled, along with a million other things...
button.html
<button
data-sly-use.button="com.adobe.cq.wcm.core.components.models.Button"
data-sly-element="${button.buttonLink.valid ? 'a' : 'button'}"
type="${button.buttonLink.valid ? '' : 'button'}"
id="${button.id}"
rel="${button.rel}" <--THIS DOES NOT WORK
class=""
data-sly-attribute="${button.buttonLink.htmlAttributes}"
aria-label="${button.accessibilityLabel}"
data-cmp-clickable="${button.data ? true : false}"
data-cmp-data-layer="${button.data.json}">
<span data-sly-test="${button.text}" class="">${button.text}</span>
</button>
You can use the properties object with a HTL context attribute.
<button rel=${properties.rel # context='attribute} </button>
this was the answer
rel=${properties.rel}

Extract class attribute using xpath

I have the following html:
<div class="g-recaptcha" data-sitekey="6LdWKrUUAAAAAP3b4V05YVzvFNJNAUrDb0RoJZf7" data-callback="reValidateP" data-expired-callback="reInvalidateP" style="clear:left;">
How can I extract sitekey value attribute via Xpath?
XPath 1.0 solution :
string(//div[#class="g-recaptcha"]/#data-sitekey)
Output : 6LdWKrUUAAAAAP3b4V05YVzvFNJNAUrDb0RoJZf7

How to extract something I want in html using 'xpath'

The html code is looking like this:
<img alt="Papa's Cupcakeria To Go!" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-old-hires="" class="a-dynamic-image a-stretch-vertical" id="landingImage" data-a-dynamic-image="{"https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L.png":[512,512],"https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SX425_.png":[425,425],"https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SX466_.png":[466,466],"https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SY450_.png":[450,450],"https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L._SY355_.png":[355,355]}" style="max-width:512px;max-height:512px;">
I want to get "https://images-na.ssl-images-amazon.com/images/I/814vdYZK17L.png" and now I'm using
extract_item(hxs.xpath("//img[#id='landingImage']/#data-a-dynamic-image"))
, what I got is all the content inside that tag.
How can I get the first url only?
If you just want the first URL:
full_content = extract_item(hxs.xpath("//img[#id='landingImage']/#data-a-dynamic-image"))
list_contents = full_content.split(";")
first_image = list_contents[1].replace("&quot","")
print first_image
Also, you can refer this for extracting URL using regex.

extracting "author" from a book in amazon with Jsoup

I am trying this for days now and it won't wort.
i want the name of the author of this book
"http://www.amazon.de/Weit-weg-ganz-Jojo-Moyes-ebook/dp/B00H07CB9O/ref=sr_1_1?s=books&ie=UTF8&qid=undefined&sr=1-1".
As firebug shows it is located in the following code.
<html>
...
<div class="buying">
<h1 class="parseasinTitle">
<span>
<span class="contributorNameTrigger" asin="B001HMNFPMB00H07CB9O">
<a id="contributorNameTriggerB001HMNFPMB00H07CB9O" href="http://www.amazon.de/Jojo-Moyes/e /B001HMNFPM/ref=ntt_athr_dp_pel_1" asin="B001HMNFPMB00H07CB9O">Jojo Moyes</a>
<a href="#" asin="B001HMNFPMB00H07CB9O">
</span>
...
</html>
i tryed to select the name with
doc.getElementsByClass("contributorNameTrigger")
but it doesn't work.then i used the class "buying" and tried to select the span and the span class but it doesn't work neither
doc.getElementsByClass("buying").select("span").select("span[class=contributorNameTrigger");
Any help is appreciated!
it finally works with the following code:
Element author = doc.getElementsByClass("buying").select("span").select("a").first();
Thanx for the answers!

Using html-tags within HTMTL::link_to_route()

In Laravel, how can I use html-tags when linking to a route via HTML::link_to_route()?
Example of what I have:
<li>
{{ HTML::link_to_route( "books_new", "New Book" ) }}
</li>
What I would like to do:
<li>
{{ HTML::link_to_route(
"books_new",
"<span class='icon-book'></span>New Book"
) }}
</li>
I know this is not the answer you want to hear - but you cannot pass html via link_to_route.
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author') and generate the link yourself. So make a helper a like this (not tested):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}
How about something like this?
<li>
<span class='icon-book'></span>New Book
</li>
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before". You might need a bit of adjustment in CSS; but it might be better in terms of semantic mark-up.
<a href="{{ URL::route('empdelete', array('id' => $employee->id)) }}">
<img src="{{ asset('images/tick-red.jpg') }}" alt="DRC" id="DRCS-logo" /></a>
You can not have HTML markup with HTML::.... (class) , in the documentation they say that anything that is passed as a parameter to the class is escaped with an HTML entity function to make front-end safer!
You can include font awesome or icon into Laravel Blade Template using this code, i already use and work perfect.
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before".
So this is working for me:
<li>
{{ HTML::link_to_route( "books_new", "New Book", null, ['class' => 'fa fa-edit'] ) }}
</li>
So far as I know, Laravel doesn't allow you to do that. To me, it seems out of standards.
Rather, apply a class called icon-book to your anchor tag, and then use the class to put the icon inside your anchor as a 'background-image`.
HTML::link_to_route('books_new', 'New Book', array('class' => 'icon-book'))
Alternatively:
Insert the span tag inside the li tag
Assign the icon-book class to the li tag