jQuery condition not matching - c#

I have the following HTML
<div id='cssmenu'>
<ul>
<li class= 'has-sub'>
<a href=store.aspx?id=dLYTWvt8EsHOq7Ps2wJA9A%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat</span></a>
<ul>
<li class= 'has-sub'>
<a href=store.aspx?id=xEDEzZWDRkX8%2fbXoMX2pSQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-2</span></a>
<ul>
<li class= 'active'><a href=store.aspx?id=TxlSRrnSZ6HDgj%2b2ZSxRhg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance -1</span></a></li>
<li class= 'active'><a href=store.aspx?id=Y7JxNAXSuG1T%2f0cpjQXSwA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance - 2</span></a></li>
<li class= 'active'><a href=store.aspx?id=%2fcoJMcJTjWp3%2bPuimR5AhA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-5</span></a></li>
</ul>
</li>
<li class= 'has-sub'>
<a href=store.aspx?id=KC2g4igBzXisbsbKu%2fKrzw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-3</span></a>
<ul>
<li class= 'active'><a href=store.aspx?id=8DCHnP%2b4KQVYDTGxVt9snQ%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death -1</span></a></li>
<li class= 'active'><a href=store.aspx?id=nGEm4rbNMQ%2bQfyO44ECmpA%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Death - 1</span></a></li>
</ul>
</li>
<li class= 'has-sub'><a href=store.aspx?id=feki%2bXDs66obnO1e2dxHWg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-4</span></a></li>
<li class= 'has-sub'><a href=store.aspx?id=5O394Lww9oxD1vHbIY7LWw%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Armed Combat 1-6</span></a></li>
</ul>
</li>
<li class= 'active'><a href=store.aspx?id=RIe63RBggHzj76SUwDHKMg%3d%3d&digest=tNy7s/jOrynR4pvMVN6d6Q==><span>Dance</span></a></li>
</ul>
</div>
And in reqid
var reqid = "<%=Request["id"]%>";
In reqid I will get urlencoded based on menu selection
My jQuery code here
$(function () {
var str = $("#cssmenu").find('li').find('a').attr('href');
if ($(str).has(reqid)) {
var str2 = $("#cssmenu").find('li').find('a').text();
$('#selectedmenuitem').html(str2);
}
});
If the href attribute encoded url contains selected menu item encodedurl i want to get the matched url .text() to the #selectedmenuitem label id.
I checked with contains, am not getting. Please suggest me what are other possibilities for this.

use indexOf function of Javascript
http://www.w3schools.com/jsref/jsref_indexof.asp

Use following jquery code
$(function () {
var str2= $("#cssmenu").find("li").find("a[href*='" + reqid + "']").text();
$('#selectedmenuitem').html(str2);
});

Related

Selenium browser c# click

Trying to pick value in the dropdown on form.
Current HTML looks like
<div class="listing-editor__input--half d--ib va--t">
<div aria-haspopup="true" tabindex="-1" class="dropdown form__text--select d--b dropdown--expanded" aria-expanded="true">
<div class="dropdown__selector dropdown__selector--select-tag dropdown__selector--select-tag--large">
<p data-et-name="category" class="tc--lg">
Select Subcategory (optional)
</p>
</div>
<div>
<ul class="dropdown__menu">
<li class="dropdown__menu__item"><a class="dropdown__link">Belts</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Glasses</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Gloves & Mittens</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Hair Accessories</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Hats</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Hosiery & Socks</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Key & Card Holders</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Laptop Cases</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Phone Cases</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Scarves & Wraps</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Sunglasses</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Tablet Cases</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Umbrellas</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">Watches</a></li>
<li class="dropdown__menu__item"><a class="dropdown__link">None</a></li>
</ul>
</div>
</div>
<p class="form__error-message" style="display: none;">
</p>
</div>
And im trying to pick by string "Phone"
Tried with
// select the drop down list
var education = driver.FindElementByCssSelector("#content > div > div > div:nth-child(2) > section:nth-child(4) > div > div.col-x24.col-l20 > div:nth-child(1) > div > div.dropdown__selector.dropdown__selector--select-tag.dropdown__selector--select-tag--large");
//create select element object
var selectElement = new SelectElement(education);
// select by text
selectElement.SelectByText("Phone");
Output
OpenQA.Selenium.Support.UI.UnexpectedTagNameException: 'Element should have been select but was div'
Edit>
SelectElement can be used only with HTML select tag. Steps to select the dropdown in HTML you provided:
click on dropdown to expand it
find and click on "option" element.
using (IWebDriver driver = new ChromeDriver())
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
driver.Navigate().GoToUrl("https://poshmark.com/create-listing");
driver.FindElement(By.Id("login_form_username_email")).SendKeys("username");
driver.FindElement(By.Id("login_form_password")).SendKeys("password");
driver.FindElement(By.TagName("button")).Click();
wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[data-vv-name='title']"))).SendKeys("Title controled by t3cho");
driver.FindElement(By.CssSelector("[data-vv-name='originalPrice']")).SendKeys("22");
var categoryCombobox = driver.FindElement(By.XPath("//span[#data-et-name='category']/ancestor::div[contains(#class,'isting-editor__input--half')][1]"));
js.ExecuteScript("arguments[0].scrollIntoView(false)", categoryCombobox);
categoryCombobox.Click();
var category1 = driver.FindElement(By.LinkText("Accessories"));
js.ExecuteScript("arguments[0].scrollIntoView(false)", category1);
category1.Click();
var category2 = driver.FindElement(By.LinkText("Glasses"));
js.ExecuteScript("arguments[0].scrollIntoView(false)", category2);
category2.Click();
}
Download SeleniumExtras.WaitHelpers NuGet Package for ExpectedConditions.
Try this:
new SelectElement(driver.FindElement(By.Xpath("//ul[#class='dropdown__menu']"))).SelectByText("Phone Cases");

Hide Header(Li section inside) with TRUE or FALSE result

I have this HTML header with <nav><ul><li> sections , the problem is that I'm trying to hide the LI section with id recfAct when in my CodeBehind brpt gets false
HTML
<header class="header" id="header1">
<h2>LISTADO tabla [TParUsuPerfil]</h2>
<nav class="menu">
<ul>
<li id="refAct">Actualizar </li>
<li id="refSalir">Salir </li>
</ul>
</nav>
</header>
C#
Boolean bRpt;
bRpt = wsObj.ValidarExiste(Session["LoginUsername"].ToString()); //gets false or true in brpt
Update :
Just need to add the tag runat ="server" and then i can call it in code behind
Html:
<li id="refAct" runat="server">Actualizar </li>
<li id="refSalir">Salir </li>
CodeBehind:
refAct.Visible = true;

Set a page Item Active or Disable with bootstrap 4 pagination, using ASP.NET

I used this code with bootstrap 3, and it worked correctly, when clicking on the page, it appeared as active, but when switching to bootstrap 4, it no longer calls the active class, when I pass the page number inside the "for"
Any ideias how to resolve that?
<ul class="pagination">
<li class="page-item ">
#{
if (ViewBag.pageNumber> 1)
{
<a class="page-link" href="#Url.Action("Index", "Barcos", new { search= ViewBag.Search, pageNumber= ViewBag.pageNumber- 1 })">before</a>
}
else
{
<a class="page-link" href="#">
before
</a>
}
}
</li>
#{
var page = ViewBag.pageNumber;
for (var i = 1; i <= ViewBag.Count; i++)
{
<li #(page== i ? "class=page-item active" : "")>
<a class="page-link" href="#Url.Action("Index", "Barcos", new {search= ViewBag.Search, pageNumber= i})">#i</a>
</li>
}
}
<li>
#if (ViewBag.pageNumber< ViewBag.count)
{
<a class="page-link" href="#Url.Action("Index", "Barcos", new { Search= ViewBag.Search, pageNumber= ViewBag.pageNumber+ 1 })">Next</a>
}
else
{
<a class="page-link disabled" href="#">
Before
</a>
}
</li>
</ul>
You need class="page-item" on all list items, not just for your current page
Corrected bode below.
#for (var i = 1; i <= ViewBag.Count; i++)
{
var page = ViewBag.pageNumber;
string classes = "page-item";
if (page == i)
{
classes += " active";
}
<li class="#classes">
#Html.ActionLink(i.ToString(), "Index", "Barcos", new { search = ViewBag.Search, pageNumber = i }, new { #class = "page-link" })
</li>
}
This generates the following HTML
<ul class="pagination">
<li class="page-item ">
<a class="page-link" href="/Barcos/Index?pageNumber=1">before</a>
</li>
<li class="page-item">
<a class="page-link" href="/Barcos/Index?pageNumber=1">1</a>
</li>
<li class="page-item active">
<a class="page-link" href="/Barcos/Index?pageNumber=2">2</a>
</li>
<li class="page-item">
<a class="page-link" href="/Barcos/Index?pageNumber=3">3</a>
</li>
<li>
<a class="page-link" href="/Barcos/Index?pageNumber=4">Next</a>
</li>
</ul>

Confusing creating HTML List base on array

I has a problem to creating HTML-list base on array in c#.
I tried using split.string, foreach, and etc,. but still can't figured out the logic... :(..
Anyone can help me to solve my problem ?
Here is my Array
List<string> listMenu = new List<string>();
listMenu.Add("Dashboard~View1");
listMenu.Add("Dashboard~View2");
listMenu.Add("Customer");
listMenu.Add("Part");
listMenu.Add("Part~Part1~Part11");
listMenu.Add("Part~Part1~Part12");
listMenu.Add("Part~Part2~Part21");
listMenu.Add("Part~Part2~Part22");
listMenu.Add("Part~Part3~Part31~Part311");
listMenu.Add("Part~Part3~Part31~Part312");
listMenu.Add("Branch");
And I want to create HTML list like this :
<div id=menu>
<ul>
<li>Dahboard
<ul>
<li> View1 </li>
<li> View2 </li>
</ul>
</li>
<li> Customer
</li>
<li> Part
<ul>
<li> Part1
<ul>
<li> Part11
</li>
<li> Part12
</li>
</ul>
</li>
<li> Part2
<ul>
<li> Part21
</li>
<li> Part22
</li>
</ul>
</li>
<li> Part3
<ul>
<li> Part31
<ul>
<li> Part 311
</li>
</ul>
</li>
<li> Part 312
</li>
</ul>
</li>
</ul>
</li>
<li> Branch
</li>
</ul>
</div>
There are two ways you can use to create this list dynamically:
You can use HtmlGenericControl structure as below:
HtmlGenericControl c = new HtmlGenericControl("div");
c.Attributes.Add("id", "menu");
HtmlGenericControl ul1 = new HtmlGenericControl("ul");
c.Controls.Add(ul1);
HtmlGenericControl li1 = new HtmlGenericControl("li");
li1.InnerText = "Dashboard";
ul1.Controls.Add(li1);
mainDiv.Controls.Add(c);
You can create a string with StringBuilder and assign this string as InnerHtml to your main div, as below:
StringBuilder s = new StringBuilder();
s.Append(#"<div id=menu>
<ul>
<li>Dahboard
<ul>
<li> View1 </li>
<li> View2 </li>
</ul>
</li>
</ul>
</div> "); //you can alter this string dynamically
mainDiv.InnerHtml = s.ToString();
You should add System.Web.UI.HtmlControls as using to use
HtmlControls
You should add System.Text as using in order to use StringBuilder.
Bonus: You can find from here why you should use StringBuilder
instead of adding string with '+'.

CsQuery - find element with concrete name

I have to parse html like this:
<div class="navig-nav">
<ul class="navig" id="navigmenu">
<li class="navig-expanded navig-selected">Company</li>
<li class="navig-sub"><div>Summary</div></li>
<li class="navig-sub">Some</li>
<li class="navig-sub">Interesting</li>
<li class="navig-sub">Information</li>
<li class="navig-item"><div>From</div></li>
<li class="navig-item"><div>Web</div></li>
<li class="navig-item"><div>Page</div></li>
</ul>
</div>
In this menu (as you can see) all classes has more than one child. How can I get "any_url_2" using csQuery if I have just the name "Interesting"?
What about
dom["a:contains('Interesting')"];
Well here's a more complete query:
CQ dom = "your html here";
var result = dom["a[href='any_url_2']:contains('Interesting')"];
if (result.Length == 1)
{
Console.WriteLine(result[0].Attributes["href"]);
}
Is this:
dom[".navig-sub"].Find("a:contains('Interesting')")
a good answer?

Categories