How to print values present in <nobr> tag using selenium webdriver - html

it would be really helpful if someone could point out to me in the right direction.
I am trying to print values which are present in nobr tag.
this is my code.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class appannieuserrating {
public static WebDriver driver;
#Test
public void f() throws Exception{
driver.findElement(By.id("login-button")).click();
driver.findElement(By.id("username")).sendKeys("sample.test#gmail.com");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login_submit_button")).click();
Thread.sleep(5000);
driver.navigate().to("https://www.appannie.com/apps/ios/app/storage-hunters-uk-the-game/reviews/?date=2015-07-06~2015-07-06");
Thread.sleep(5000);
String s1 = driver.findElement(By.cssSelector(".ngCellText.cell-inner.ng-scope")).findElement(By.tagName("img")).getAttribute("src");
System.out.println(s1);
}
#BeforeTest
public void beforeTest() {
driver = new FirefoxDriver();
driver.get("https://www.appannie.com/");
}
#AfterTest
public void afterTest() {
}
}
<div class="ngCanvas" ng-style="canvasStyle()" style="height: 467px;">
<div class="ng-scope ngRow even" ng-row="" ng-class="row.alternatingRowClass()" ng-click="row.toggleSelected($event)" ng-repeat="row in renderedRows" ng-style="rowStyle(row)" style="top: 0px; height: 58px;">
<div class="ngCell col0 colt0" ng-class="{'sorted': col.sortDirection}" ng-repeat="col in renderedColumns" style="height: 58px;">
<div class="ngVerticalBar ngVerticalBarVisible" ng-class="{ ngVerticalBarVisible: !$last }" ng-style="{height: rowHeight}" style="height: 58px;"> </div>
<div ng-cell="">
<div class="ngCellText cell-inner ng-scope">
<span stars="row.getProperty(col.field)" aa-stars="">
<nobr>
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
</nobr>
</span>
</div>
</div>
</div>
Here am trying to count the number of stars present in the "nobr" tag somehow its printing null value.
kindly point me in the right direction guys.

I would do smth like this:
List<WebElement> elements = driver.findElement(By.xpath("//span[#aa-stars]/nobr")).findElements(By.tagName("img"));
int counter = 0;
for(WebElement we: elements) {
if("*".equals(we.getAttribute("alt"))) {
counter++;
}
}
System.out.println(counter);
Unfortunately, your password is incorrect so I cannot try myself :D :D Tried locally with the same html - should work.
UPD:
I forgot to tell - in your attempt to find img element - you ignored the nobr. Yes. It is in DOM too. If you want to find ANY next img - do like this:
.findElement(By.xpath("//img"));
If you use by tagName - it is assumed that you search for direct child.
UPD 2
If you rely on src and believe that this img should be a star, consider this:
for(WebElement we: elements) {
String text = we.getAttribute("src");
if(text!=null) {
if(text.contains("star"){
counter++;
}
}
}
UPD 3
List<WebElement> elements = driver.findElement(By.xpath("//span[#aa-dtars]/nobr")).findElements(
By.tagName("‌​img"));
int counter = 0;
String starPicLink = "/media/pictures/star-whole.png";
for (WebElement we : elements) {
String text = we.getAttribute("src");
if (starPicLink.equals(text)) {
counter++;
}
}
System.out.println(counter + " Star");
UPD 4
pastie.org/10292596#34 - working version

I tried something link below to identify a input tag inside nobr tag:
xpath=//div[#id='div0_17']/span[#id='outer0_366']/nobr/input

Related

Angular TypeError when the page opens

In my code, I'm displaying a list of elements on the main page. When you click one of those elements, a side-pannel opens and you are able to see the details of that element. Whenever the page first opens I get the error below. The code doesn't crash or anything, but I don't get why the error keeps appearing. What should I do to fix it?
StickerListComponent.html:167 ;
<fuse-sidebar class="sidebar centra details-sidebar fuse-white" name="sticker-preview-sidebar" position="right">
<sticker-preview [StickerData]="previewStickerData"></sticker-preview>
</fuse-sidebar>
sticker-preview-component html ;
<div class="h-100-p" fxLayout="column" fusePerfectScrollbar>
<div class="group mt-32">
<div fxLayoutAlign="start center">
<header class="purple-fg" style="font-size:18.72px"><strong>Sticker Data:</strong></header>
</div>
<p>
<span id="sticker" contenteditable [textContent]="_stickerData?.StickerData" (input)="onStickerDataChange($event.target.innerHTML)">
{{_stickerData?.StickerData}}
</span>
</p>
</div>
<div fxLayout="row" fxLayoutAlign="start center">
<button mat-button matRipple class="purple-500 fuse-white-fg mr-12" (click)="imageSource()"> Display </button>
</div>
<div>
<img [src]="url" *ngIf="hidden" />
</div>
</div>
sticker-preview-component ts:
export class StickerPreviewComponent implements OnInit {
EditIndex: number;
public _stickerData: IStickerData = {};
confirmDialogRef: MatDialogRef<FuseConfirmDialogComponent>;
Filter: IFilter = {};
hidden = false;
url;
#Input()
set StickerData(prm: IStickerData) {
if (this._stickerData != prm) {
this._stickerData = prm;
}
33 this.url = "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/" + this._stickerData.StickerData;
}
get StickerData(): IStickerData {
return this._stickerData;
}
dataSource: MatTableDataSource<IStickerData>;
ngOnInit() {
this._productionService.getStickerDataList(this.Filter)
.subscribe((response: any) => this._stickerData = response);
}
imageSource(): void{
this.hidden = !this.hidden;
}
it seems the _stickerData is undefined at the time when it assigns to the url, try to check whether the _stickerData has the proper value and then assign it.
if(this._stickerData)
this.url = "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/" + this._stickerData.StickerData;

Show or Hide Prev and next buttons based on first div and last div in angular2

I have a requirement where i want to hide or show prev and next buttons based on div tag. Basically i am creating div tags for each value of list using ngfor loop in angular2.
List I have used
appUlist:string[] = ["Calculus-1","Geometry-1","Algebra-1","Trignometry-1","Statstics-1","Arithmetic-1"]
Now i am creating a div tag for each value in the list using ngFor in html. Since there are more than 5 values in the list, i have used next and prev button so that i can see other div tags as soon as i click on next button. This part works pretty well. but i want hide prev button if i am showing the first div tag and hide next button if there are no further div tags.
Please advice me on this
Below i have posted both html and ts code
export class AppComponent{
#ViewChild('panel', { read: ElementRef }) public panel: ElementRef<any>;
public selectedDiv;
public u;
title = 'app';
name:string = 'Creating new screen for student';
public appUlist:string[] = ["Calculus-1","Geometry-1","Algebra-1","Trignometry-1","Statstics-1","Arithmetic-1"]
}
//scrolls back when clicked on prev
public onPreviousSearchPosition(): void {
this.panel.nativeElement.scrollLeft -= 20
}
//scrolls forward when clicked on next button
public onNextSearchPosition(): void {
this.panel.nativeElement.scrollBy(20,0);
}
My html code
<div #panel class="row" style="width:700px;height:300px;overflow-x: scroll;overflow: hidden;" >
<div class="col-lg-1">
<i class="fa fa-angle-double-left" style="font-size:36px;color:#1092B0" id="left" (click)="onPreviousSearchPosition()"></i>
</div>
<div class="col-lg-1 cardhover" *ngFor="let u of appUlist">
<h1>test</h1>
</div>
<div class="col-lg-1">
<i class="fa fa-angle-double-left" style="font-size:36px;color:#1092B0" id="right" (click)="onNextSearchPosition()"></i>
</div>
</div>
Below image represents the output of mycode
You can put a variable to 0 in your appComponent and increment/decrement it each time you click prev and next. If this variable is == 0 you don't show prev button, if it is equal to your list lenght - 3 you don't show the next button :
export class AppComponent{
#ViewChild('panel', { read: ElementRef }) public panel: ElementRef<any>;
public selectedDiv;
public u;
public index = 0;
title = 'app';
name:string = 'Creating new screen for student';
public appUlist:string[] = ["Calculus-1","Geometry-1","Algebra-1","Trignometry-1","Statstics-1","Arithmetic-1"]
}
//scrolls back when clicked on prev
public onPreviousSearchPosition(): void {
this.panel.nativeElement.scrollLeft -= 20
this.index--;
}
//scrolls forward when clicked on next button
public onNextSearchPosition(): void {
this.panel.nativeElement.scrollBy(20,0);
this.index++;
}
and
<div #panel class="row" style="width:700px;height:300px;overflow-x: scroll;overflow: hidden;" >
<div class="col-lg-1">
<i *ngIf="idx != 0" class="fa fa-angle-double-left" style="font-size:36px;color:#1092B0" id="left" (click)="onPreviousSearchPosition()"></i>
</div>
<div class="col-lg-1 cardhover" *ngFor="let u of appUlist">
<h1>test</h1>
</div>
<div class="col-lg-1">
<i *ngIf="idx < list.lenght - 3" class="fa fa-angle-double-left" style="font-size:36px;color:#1092B0" id="right" (click)="onNextSearchPosition()"></i>
</div>
</div>

Select tag always returns null

I am trying to create a search module to search shop details from database based on the criteria in select that user chooses
index.scala.html
#import helper._
#import helper.twitterBootstrap._
#main(Html("Home")) {
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
<h1>Shop Directory</h1>
<p>Lets you search a nearby Shop and get to know their location</p>
<p>Search Shop by Product or Shop name</p>
<form class="form-inline" action="#routes.SearchController.search()" method="post">
<input type="text" class="form-inline input-lg" placeholder="Product/Shop name" name="keyword" required="keyword required">
<select class="form-inline input-lg" id="Select1" name="criteria">
<option value="">-:Select Criteria:- </option>
<option value="shop">Shop</option>
<option value="product">Product</option>
</select>
<button class="btn btn-lg btn-primary" role="button">Search</button>
</form>
</div>
}
Search.java
package viewmodels;
public class Search {
public String keyword;
public String criteria;
}
SearchResult.java
package viewmodels;
import models.Shop;
import play.mvc.Controller;
import java.util.ArrayList;
import java.util.List;
public class SearchResult extends Controller {
public String criteria;
public String keyword;
public List<Shop> shops;
public SearchResult() {
shops = new ArrayList();
}
}
SearchController.java
package controllers;
import models.Product;
import models.Shop;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.Controller;
import viewmodels.Search;
import viewmodels.SearchResult;
import java.util.List;
import play.mvc.Result;
import static play.data.Form.*;
public class SearchController extends Controller {
public static Result search() {
Form<Search> requestData = form(Search.class).bindFromRequest();
Search datatosearch = requestData.get();
// String criteria="shop";
String criteria = datatosearch.criteria;
SearchResult result = new SearchResult();
result.criteria = criteria;
result.keyword = datatosearch.keyword;
if (criteria == "shop") {
List<Shop> shops = Shop.findByShopName(datatosearch.keyword);
result.shops.addAll(shops);
}
else if (criteria == "product") {
List<Shop> shops = Product.findByShopName(datatosearch.keyword);
result.shops.addAll(shops);
}
return ok(views.html.search.results.render(result));
}
}
if I do String criteria="shop" or String criteria="product" in my SearchController.java then it works fine, meaning my model query is correct, but if I execute the above code with String criteria = datatosearch.criteria it shows a blank screen.
I am using play framework, I am really stuck at this and any help would be appreciated.
You are comparing strings with the == operator which is a no-no. Change your string comparisons to use String.equals so you are actually comparing the values instead of object references.
if (criteria.equals("shop") {
...
}
else if (criteria.equals("product") {
...
}
You probably also want to add some validation to check that criteria isn't NULL.

Apache Wicket - Combining tutorials

actually i have to learn wicket for a practical course and I have some problems. i tried to combine two of the wicket examples from the wicket library, but it failed and i dont know why.
The error message says:
Last cause: Unable to find component with id 'form' in [BorderBodyContainer [Component id = navomaticBorder_body]]
Expected: 'navomaticBorder:navomaticBorder_body:form'.
Found with similar names: 'form'
There are two examples i want to combine: the navomatic side-bar to have some links between my sites and a simple password validator.
NavomaticBorder.java
public NavomaticBorder(final String id) {
super(id);
addToBorder(new BoxBorder("navigationBorder"));
addToBorder(new BoxBorder("bodyBorder"));
}
NavomaticBorder.html
<html>
<head>
<title>Wicket Examples - navomatic</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<wicket:border>
<p>
<table height = "100%">
<tr>
<td height = "100%" valign = "top">
<div wicket:id = "navigationBorder">
<b>Navigation Links</b>
<p>
<wicket:link>
Seite1<br/>
Seite2<br/>
Seite3
</wicket:link>
</p>
</div>
</td>
<td valign = "top">
<span wicket:id = "bodyBorder">
<wicket:body/>
</span>
</td>
</tr>
</table>
</p>
</wicket:border>
</body>
</html>
Page1.html
<html>
<head>
<title>Wicket Examples - navomatic</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<span wicket:id = "navomaticBorder">
<h1>Registration 1</h1>
<form wicket:id="form">
<p><label>Username</label>:<input wicket:id="username"
type="text" size="20"></p>
<p><label>Password</label>:<input wicket:id="password1"
type="password" size="20"></p>
<p><label>Confirm Password</label>:<input wicket:id="password2"
type="password" size="20"></p>
<input type="submit" value="Register">
</form>
<div wicket:id="feedback"></div>
</span>
</body>
</html>
Page1.java
public class Page1 extends WebPage {
/**
*
*/
private static final long serialVersionUID = 8070623982828503750L;
public Page1() {
add(new NavomaticBorder("navomaticBorder"));
add(new FeedbackPanel("feedback"));
final TextField<?> username = new TextField<String>("username",
Model.of(""));
username.setRequired(true);
FormComponent<String> password1 = new PasswordTextField("password1",
Model.of(""));
password1.setLabel(Model.of("Password"));
password1.add(StringValidator.minimumLength(8));
password1.add(new PasswordPolicyValidator());
FormComponent<String> password2 = new PasswordTextField("password2",
Model.of(""));
Form<?> form = new Form<Void>("form") {
private static final long serialVersionUID = -8653853765358238769L;
#Override
protected void onSubmit() {
info("Form submitted");
}
};
form.add(new EqualPasswordInputValidator(password1, password2));
add(form);
form.add(username);
form.add(password1);
form.add(password2);
}
}
I think thats the code parts who may cause the error, when i let all of them working alone theres no problem, just when i combine this in the last html file, its throwing an error.
I have no real experience with Html, maybe thats the cause but i hope its enough data to help me :)
Richie
It's looking for your form in the element navomaticBorder. So you should add the form to the navomaticBorder.
Try changing your code to this:
public class Page1 extends WebPage {
/**
*
*/
private static final long serialVersionUID = 8070623982828503750L;
public Page1() {
NavomaticBorder nav = new NavomaticBorder("navomaticBorder");
add(nav);
add(new FeedbackPanel("feedback"));
final TextField<?> username = new TextField<String>("username",
Model.of(""));
username.setRequired(true);
FormComponent<String> password1 = new PasswordTextField("password1",
Model.of(""));
password1.setLabel(Model.of("Password"));
password1.add(StringValidator.minimumLength(8));
password1.add(new PasswordPolicyValidator());
FormComponent<String> password2 = new PasswordTextField("password2",
Model.of(""));
Form<?> form = new Form<Void>("form") {
private static final long serialVersionUID = -8653853765358238769L;
#Override
protected void onSubmit() {
info("Form submitted");
}
};
form.add(new EqualPasswordInputValidator(password1, password2));
nav.add(form);
form.add(username);
form.add(password1);
form.add(password2);
}
}

Load html from returned string

I have the string and its returning function in the code-behind:
string xmlContents = "<ul><li>Installation<br /><ul><li>Startup</li><li>Getting there</li><li>Steps</li>" +
"<li>Pictures</li></ul></li><li>Usage<br /><ul><li>Tilbud pane</li><li>Report pane</li>" +
"</ul></li></ul>";
public String returnXml()
{
return xmlContents;
}
Then I call it in the aspx file:
<div id="treeviewMenu">
<%returnXml(); %>
</div>
When I simply write the html code (of the list) directly in the div - it's ok. But by passing the string - it doesn't work.
What am I doing wrong and how to fix it ?
Note: = sign whithout ; sign
Replace with this code:
<div id="treeviewMenu">
<%=returnXml() %>
</div
You can easily assign html to div by making div server accessible by adding runat="server"
HTML
<div id="treeviewMenu" runat="server"></div>
In code behind
treeviewMenu.InnerHTML = xmlContents;