Unable to Navigate using LinkText in Selenium C# - asp.net

I got the following C# code from Selenium IDE:
driver.FindElement(By.LinkText("Sub Link 1")).Click();
But as it wasn't working, I modified the code below to wait. It is still not working.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.LinkText("Main Link 1")));
if (element != null)
{
var innerElement = wait.Until(ExpectedConditions.ElementExists(By.PartialLinkText(
"Sub Link 1")));
}
What might be the possible issue?
My HTML:
<html>
Show navigation
Hide navigation
<div id="navlogo"><a title="HOME" href="url">
<span style="position:absolute;width:100%;height:100%;top:0;left: 0;">
</span></a></div>
<ul class="clearfix">
<li>MXLMain1
<ul>
<li>ML1
</li>
<li>ML2
</li>
</ul>
</li>
<li>MXLMain2
<ul>
<li>MK2
</li>
</ul>
</li>
<li>Main Link 1
<ul>
<li>Sub Link 1
</li>
<li>Sub Link 2
</li>
</ul>
</li>
..........................
</html>

How about using xpath?
driver.FindElement(By.XPath("//a[.='Sub Link 1")).Click();

It looks like the link that you are trying to use is from a navigation bar. You will need to use Seleniums Actions class to activate the menu dropdown.
Then use Actions class to move to element and click on it.
public void MouseHover(By locator)
{
element = driver.FindElement(locator);
Actions action = new Actions(driver);
action.MoveToElement(element).Perform();
}
To click same steps.
action.MoveToElement(element).Perform();
action.Click().Perform();
Move to initial element
Move to the second element and use click method

Related

Get li class into a custom google tag manager variable

<div class="post-share-bar">
<ul>
<li class="share-on">Share on</li>
<li class="facebook">
</li>
<li class="twitter">
</li>
<li class="google">
</li>
<li class="whatsapp">Whatsapp</li>
</ul>
</div>
I want to set a GA tag with a social track type and I need to pass the li class value for the network parameter when clicked, eg. Facebook, Twitter, Google, Whatsapp. The Action part of the social is fixed as article share.
The action target am going to pass in is the {{Page URL}}.
So how do I grab the li class value and push that into a custom variable?
One option would be to create a Custom JavaScript variable that takes the clicked element and returns the parent class, which would be the name of the social platform that you are looking for, something like this (assuming you have jQuery loaded):
$('.post-share-bar a').click(function(){
var platform = $(this).parent().attr('class')
// do something with 'platform'
)}

Orchard CMS: Modifying a menu item alternate and iterating over the items

Let's say I created a menu titled "Main Menu" from the admin panel and I need to customize the default markup, e.g. let's say I want to modify/replace the <ul>, <li> as well as the surrounding <article> and <nav> tags.
I assume that I have to come up with something like this in my Parts.MenuWidget.cshtml alternate template.
<ul class="a b c">
for each item in menuItems:
display("<li>" + item + "</li>")
end
</ul>
How do I do that in Orchard?
That's quite simple.
You could first create an alternate for you MenuWidget like so:
Parts.MenuWidget-MyZoneName.cshtml
<nav>
<div class="logo">
#Html.Partial("_Logo")
</div>
<ul>
#*Display all the stuff added in the admin*#
#DisplayChildren(Model.Menu)
#*Add additional stuff. Of course you could also add it as MenuItem to the Menu-Model*#
#if ( Request.IsAuthenticated )
{
if ( Authorizer.Authorize(StandardPermissions.AccessAdminPanel) )
{
<li>
<a href="/Admin">
Admin
</a>
</li>
}
<li>
<a href="~/Example1">
Extra1
</a>
</li>
}
else
{
<li>
<a href="~/Example2">
Extra2
</a>
</li>
}
</ul>
</nav>
And then go on and do something similar for your MenuItems. For example:
MenuItemLink.cshtml
#Model.Text
Another thing that's worth mentioning is that you can either only provide an alternate for a specific shape like the MenuItemLink above or just provide an alternate for the base MenuItem shape which will then be used for every MenuItem type that exists.
(Maybe those are not the best examples, but I guess they'll do the job ;) )
Update:
In order to remove/modify the tags you can create an alternate for MenuItem.cshtml and look at this part:
if (HasText(renderedMenuItemLink))
{
var tag = Tag(Model, "li");
#tag.StartElement
#renderedMenuItemLink
if (items.Any())
{
<ul>
#DisplayChildren(Model)
</ul>
}
#tag.EndElement
}

Get selected li

hi i have master page and menu like this
<ul id="ulhotel" runat="server">
<li class="userprof active"><a href="AddAdminDetail.aspx">
<p class="activenav">
<span class="entypo activenav">+</span>User Profile</p>
</a></li>
<li class="hoteldetails"><a href="AddHotelDetails.aspx">
<p>
<span class="entypo">j</span>Hotel Details</p>
</a></li>
<li class="hoteldirectory"><a href="AddHotelDirectory.aspx">
<p>
<span class="entypo">l</span>Hotel Directory</p>
</a></li>
<li class="appconfig"><a href="Appconfiguration.aspx">
<p>
<span class="entypo">#</span>App Configurate</p>
</a></li>
<li class="featoffers"><a href="OfferDashboard.aspx">
<p>
<span class="entypo">C</span>Offer Dashboard</p>
</a></li>
</ul>
now right now User Profile Is seleted now when i Click HotelDetail link I want to change it same as User Profile
i am trying this but its not working
$('#ulhotel li').click(function()
Thanks in advance.
You have
AddAdminDetail.aspx
AddHotelDetails.aspx
AddHotelDirectory.aspx
Appconfiguration.aspx
OfferDashboard.aspx
five different pages so you can also add active class to appropriate li tag on related page.
Comment Response
$('#ulhotel li').on('click',function(e)
and then set cookies for clicked li now when page redirected use that previously set cookies and add active class to appropriate li.
It is due to when you click link each time master page is loading.
you have to use script to change class in AddHotelDetails.aspx page to active related li.
In AddHotelDetails.aspx page you can use
$(document).ready(function(){
$('#ulhotel li').removeClass("active");
$('#ulhotel li.hoteldetails').addClass("active");
});
Hope this help you.

check for condition in site.master page

I have a site.master page in an asp application which contains a div menu and it looks like this.
<div id="menu">
<ul>
<li class="first">home</li>
<li>manager services</li>
<li>employee services</li>
<li>my projects</li>
</ul>
</div>
What i want is that the manager services portion should be visible only if a particular employee has his 'Is_Manager' field set to True in the database. For the rest this tag should be invisible. How do I achieve this?? How do i check for this contidion over here in this site.master page? please help.
Do it inside of the mark up. In the code behind on page load, set the Is_Manager flag and look it up from the database. Then, in the markup, do this:
<% if(Is_Manager){%>
<li>Manager Services</li>
<%}%>
Given what you've provided so far ...
in the cs file for the master:
protected bool IsVisible;
protected void Page_Load(object sender, EventArgs e){
IsVisible = (bool)Session["isVisible"];
}
in the aspx for the master page
<style>
.manager { visibility = <%= (IsVisible) ? "visible" : "hidden" %>;}
</style>
<div id="menu">
<ul>
<li class="first">home</li>
<li class="manager">manager services</li>
<li>employee services</li>
<li>my projects</li>
</ul>
</div>

Logic for displaying infinite category tree in nested <ul>s from Self Join Table

Please help me solve my big problem.
in my on-line shopping project i created a dynamic Category List (with Infinite Level Depth) Implemented in a Single Table in DB with Self join.
the schema is like below:
(source: aspalliance.com)
Update
I want to use a JQuery plugin to make a Multi Level Menu bar. this plugin uses <ul> and <li> elements so I should transform the DB table to <ul> and <li>. the result should like this:
<ul>
<li>Clothing 1
<ul>
<li>Trousers 2
<ul>
<li>Mens trousers 3</li>
<li>Ladies trousers 3</li>
</ul>
</li>
<li>Jackets 2</li>
<li>Shirts 2</li>
<li>Shoes
<ul>
<li>Mens shoes 3
<ul>
<li>Mens formal shoes 4</li>
<li>Mens casual shoes 4</li>
</ul>
</li>
<li>Kids shoes 3</li>
<li>Ladies shoes 3</li>
</ul>
</li>
</ul>
</li>
<li>Cars 1
<ul>
<li>Small cars 2</i>
</ul>
</li>
</ul>
I can use a nested data control(like repeater control) but you know, with this solution i just can implement a list with non-infinite hierarchical tree structure.
please help me! any suggestion?? I googled the web but not a suitable way found. I use ASP.net 3.5 and LINQ.
what is the best way?
Use this recursive method
private string GenerateUL(IQueryable<Menu> menus)
{
var sb = new StringBuilder();
sb.AppendLine("<ul>");
foreach (var menu in menus)
{
if (menu.Menus.Any())
{
sb.AppendLine("<li>" + menu.Text);
sb.Append(GenerateUL(menu.Menus.AsQueryable()));
sb.AppendLine("</li>");
}
else
sb.AppendLine("<li>" + menu.Text + "</li>");
}
sb.AppendLine("</ul>");
return sb.ToString();
}
like this
DataClasses1DataContext context = new DataClasses1DataContext();
var s = GenerateUL(context.Menus.Where(m => m.ParentID == null));
Response.Write(s);
I think ASP.NET TreeView is your friend.
Take a look here too.
Also, you might want to use a dynamically created nested DataGrid or Repeater, here is an example (you can make it dynamic, so the nested (or even the parent) repeaters are genenerated programmatically.
void GenerateChildren(Menu menu)
{
//Create DataGridRow/RepeaterRow/TreeViewNode/whatever for this menu
menu.Children.Load();
foreach (var child in menu.Children)
{
GenerateChildren(child);
}
}
Update
See example 5 on this page.
I would recommend generating the jQuery code by server, so it's easier for you to control both the menus and the jQ generated menus.

Resources