Thymeleaf: Parameters In URLs Not Being Replaced - java

I'm trying to learn Spring MVC & Thymeleaf. I've got the following HTML portion that prints out a link & a button:
<ul th:each="item : ${typesMap}">
<li>
Linky
<button type="button" th:text="${item.value}">Button Text</button>
</li>
</ul>
In the two examples, the parameters in the link are never replaced. I always end up with something along the lines of roomdetails/${item.key}/${item.value} in the HTML. The button works fine though & will show with the text that is found in ${item.value} for each iteration of the loop.
Does anyone know why I can't get URLs in the format I want? From what I can see, I'm doing what the documentation is telling me to.

This should work:
<a href="roomdetails.html" th:href="#{'/roomdetails/' + ${item.key} + '/' + ${item.value}}">

Answer:
<a href="roomdetails.html" th:href="#{'/roomdetails/{paramsKey}/{paramsValue}'(paramsKey=${item.key}, paramsValue=${item.value})}">
I hope that this might solve your issue.

Related

Extract Text from Span Element via XPath [Selenium]

I have the following HTML:
<div class="a-row a-spacing-small a-size-small">
<div class="a-row">
<a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":"false","url":"/gp/customer-reviews/widgets/average-customer-review/popover/ref=wl_it_o_cm_cr_acr_img_hz?ie=UTF8&a=B05555JQP&contextId=wishi&link=1&seeall=1","name":"review-hist-pop.B075555RJQP","max-width":"700","position":"triggerBottom","data":{"itemId":"I2555555554GT","isGridViewInnerPopover":""},"header":"","cache":"true"}">
<i id="review_stars_I2J55555554GT" class="a-icon a-icon-star a-star-4-5">
<span class="a-icon-alt">4.5 out of 5 stars</span>
</i>
<i class="a-icon a-icon-popover"/>
</a>
<a class="a-link-normal g-visible-no-js" href="/product-reviews/B075555JQP/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=2K4U5555551D&coliid=I2J5555555T&showViewpoints=1">
<span class="a-letter-space"/>
<a id="review_count_I2J55555555GT" class="a-link-normal" href="/product-reviews/B05555555P/ref=wl_it_o_cm_cr_acr_txt_hz?ie=UTF8&colid=255555555D&coliid=I2555555GT&showViewpoints=1">(68)</a>
</div>
<div class="a-row">
<div class="a-row a-size-small itemAvailability">
<div class="a-row itemUsedAndNew">
</div>
I'm trying to extract the value 4.5 out of 5 stars via one of the following XPath:
.//*[contains(#id,'review_stars')]/span[#class='a-icon-alt']
.//*[contains(#id,'review_stars')]
However, everything that I've tried so far has failed (returns empty String)
The funny thing is that all of these XPaths actually work in Firebug so I'm not sure why it isn't working in my program (I suspect it has something to do with the fact that the rating isn't actually visible in browser unless you hover over a specific element but I'm not sure if/why/how this would cause the above mentioned problem and how to fix it)
Thanks!
You failed to include the image between the anchor and span. The span is inside the image, not a sibling of the anchor.
try:
.//*[contains(#id,'review_stars')]/i/span[#class='a-icon-alt']
I will attempt to answer my own question although I do not entirely understand why my previous code isn't working. If someone could provide me with an in depth explanation I will accept their answer as the final answer.
For now this is what works for me:
Instead of calling element.getText(); call element.getAttribute("innerHTML");
This returns the correct result but I would like to understand why getText() does not work in this case. Again, if someone could provide an XPath that works or could provide explanation to all this I will accept it as the final answer.
Thanks
To extract the value 4.5 out of 5 stars through XPath you can use :
//a[#class='a-link-normal a-declarative g-visible-js reviewStarsPopoverLink']/i[starts-with(#id,'review_stars_') and #class='a-icon a-icon-star a-star-4-5']/span[#class='a-icon-alt']
Update :
As you mentioned This does not work either. I just tried it. you must have missed out a part from the xpath which I have provided. My Answer was a proven one. See the snapshot below :
Note : Though your question was related to xpath you have pulled out your answer with respect to getText() method and getAttribute("innerHTML") method. How ever my Answer will be working with both getText() and getAttribute("innerHTML") method.

Extract an image url from a LI in an UL

I am trying to extract the href value <img ng-src= from the following HTML.
Where everything after //images.mycar.com... is dynamic.
It is a <ul> with many <li>, it will be the first item in the list. I want to be able to capture it so I can construct a URL to click on the image.
Sorry if my post is confusing .. i will try and clarify .
In the example //images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg
Is the item i want to extract, so i can then click on the image.
<!-- Begin Results List -->
<ul id="resultsLists" class="o-media-list c-animate c-animate--show" ng-show="rc.results.vehicles.length" style="">
<li id="BJ11TFZ" class="c-animate c-animate--repeat u-pb ng-scope" ng-repeat="car in rc.results.vehicles track by car.registration" style="">
<div class="o-media c-card c-card--hover c-card__block u-p-0 u-shadowed u-shadowed--hover">
<div class="o-media__left o-grid__col-sm-5 o-grid__col-xs-12 u-p-0 u-no-float--sm">
<a ng-href="/used-car/FORD/FIESTA/BJ11TFZ" class="o-embed-responsive o-embed-responsive--16by9 o-media__object c-rollover" ng-click="rc.viewCar(car, $index)" href="/used-car/FORD/FIESTA/BJ11TFZ">
<img ng-src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg" alt="FORD FIESTA" class="o-embed-responsive__item c-rollover__image" cs-src-responsive="[ [ 'small', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ], [ 'retina', '//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg' ] ]" src="//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-M-01.jpg">
</a>
</div>
If I understand your question right you are trying to extract this link below from the code above.
//images.mycar.com/BJ/11/BJ11TFZ/BJ11TFZ-used-FORD-FIESTA-DIESEL-HATCHBACK-1-6-TDCi-95-Titanium-3dr-Diesel-Manual-RED-2011-HR-S-01.jpg
That's simple enough. A pure javascript solution to this would be:
var imgUrl = document.getElementById("resultsLists").getElementsByTagName('img')[0].getAttribute('ng-src');
The code starts by selecting the main UL of the document. Then it selects the image itself. Then it gets the "ng-src" attribute of the image and saves it to the variable imgUrl.
Let me know if this is what you were trying to do.
Okay bro, so in what's problem? If you need to use webdriver selenium and java 8
use next approach
driver.findElements(By.xpath("//ul[#id='resultsLists']/li//img")).stream()
.map(e -> e.getAttribute("ng-src"))
.collect(Collectors.toList())
You could get it like this below.
String href = driver.findElement(by.xpath("//ul/li/div/div/a")).getAttribute("href")

Having trouble locating text within span tags

I'm specifically searching for "Leg 0" and the other text below it like BOS and MAD, etc. I've played around with xpath and I seem to find text above this section and text below it but I can never find the "Leg 0" text and that below no matter what I do. I've been trying all morning to figure this out and no dice.
Here is the HTML.
<div>
<span class="debug-section ng-binding">Leg 0</span>
<span class="debug-value ng-binding">BOS</span>
-
<span class="debug-value ng-binding">MAD</span>
<span class="debug-value ng-binding">BACAT</span>
/
<span class="debug-value ng-binding">BA</span>
<span class="debug-value ng-scope" ng-if="!leg.excludeDynamic">Dynamic</span>
</div>
Any help is appreciated here, I am beyond frustrated.
First of all, provide some info about your code and exception you get as #Anderrson suggested.
And second, you can't find element based on text within span tags (innerHTML) using cssSelector, you should use xpath in that case. Or some javascript or jquery, but that would probably be overkill in this case.
The answer my friend is blowing in the wind. Brought up Firefox. Enabled the Firebug plug-in. Click the inspection tool. It highlights the HTML line you click on on the web page. Right click. Choose the Xpath copy to clipboard option.
Walla.

How to check for second element with class name and click it if it exists in Selenium Java

So I am writing automation tests using selenium and I am having a lot of trouble selecting the second element in a list of divs with the same class names
Boolean isExists2Accounts = driver.findElements(By.xpath("(//div[contains(#class, 'item-name')])[2]")).size() < 0;
if(isExists2Accounts)
{
//Finds second div element that has classname of item-name and clicks on it
driver.findElement(By.xpath("(//div[contains(#class, 'item-name')])[2]")).click();
}
else
{
driver.get("javascript:alert('There isn't a second account or you don't know how to select it!');");
Thread.sleep(5000);
org.testng.Assert.fail("transferTest6() Failed due to There isn't a second account or you don't know how to select it!");
}
HTML structure looks like this:
<div class="item-list">
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area</div>
<div class="button-wrap"></div>
</div>
<div class="item-name">
<div> clickable area </div>
<div class="button-wrap"></div>
</div>
</div>
Not really sure what I am doing wrong here, I looked at the html and there are 5 divs with the specified class name. Very new to selenium in general, using eclipse/junit/webdriver.
I have seen several questions similiar to this, and trying solutions people have posted have not worked. I have seen some suggestions to use .get(2) and I will try and implement that in the mean time.
Any help you could give would be good.
get(2) is THIRD element, not the second, as the countage begins from 0.
So:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
OR depending on where is yr clickable
driver.findElements(By.cssSelector(".item-name div:not(.button-wrap)")).get(1).click();
Hey all the answer that was given by Stanjer works, I tested it with different markup, the developer that built the system I am testing through a random mousedown event (not click) for the html I am trying to interact with which was causing the problem.
So final solution with problem if it was a click event would be:
driver.findElements(By.cssSelector(".item-name")).get(1).click();
Just like he said.
However in this case I am instead going to send Javascript to the console to work with functions that have already been created by the developer.

Rendering a Play Framework 2 View

I'm just kind of confused as a whole when it comes to play framework. I've gotten most of everything figured out, but it seems like every day something new comes up.
Anyway, the thing of the day is this: my scala template code is showing up as plain text in my rendered document. Maybe its the wrong syntax, maybe its play 1.0 syntax, I dunno. The template looks like this:
#(model : models.Menu)
#main("Bearings") {
<div id="bearings_container">
<div id="menu">
<ul id="firstLevel">
#{list items:model.items,as:'menuItem'}
<li id="${menuItem.name}" class="firstLevel">${menuItem.name}</li>
<ul id="${menuItem.name}- submenu">
#{list items: menuItem.subMenu, as:'subMenuItem'}
<li id="${subMenuItem.name}" class="secondLevel">${subMenuItem.name}</li>
<ul id="${subMenuItem.name}- submenu">
#{list items: subMenuItem.subMenu, as:'subSubMenuItem'}
<li id="${subSubMenuItem.name}" class="thirdlevel">${subSubMenuItem.name}</li>
#{/li}
</ul>
#{/li}
</ul>
#{/li}
</ul>
</div>
</div>
}
I'm sure it's something simple. Any ideas?
Edit: here's the controller action:
public static Result bearings()
{
Menu menu = BuildMenu();
return ok(views.html.bearings.render(menu));
}
And the result is what a straight HTML result of the code above would look like.
That's Play 1.0 template syntax. The template syntax in Play 2 is completely different.
To be fair the play documentation does make this confusion easy. I've found myself reading a Play 1.0 documentation page that I get to from a Google search for a while before I realize that the URL says 1.0.
If the output looks like straight HTML I'd try checking the template referred by #main. Please provide the main template source.

Categories