I am trying to create a test that finds a specified link on a web page and clicks it. What I am attempting to do is search for the link by specifying the name/text of the link. Is there a way to create a hyperlink object solely by specifying this?
I can do the following to find the link by specifying the href property like this:
BrowserWindow browser = BrowserWindow.Locate("Window Title");
var hyperlink = new HtmlHyperlink(browser);
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Href, "link.com");
Mouse.Click(hyperlink);
But I want to do the same thing by specifying the text/name of the link.
Any help would be appreciated! Thanks
I solved it...
This finds the link based on the text:
hyperlink.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "text");
Related
I have an array of some URL links. And I need to open all theese links by clicking one HyperLink, located in cell of Telerik GridHyperLinkColumn.
Is there any method to do this?
Now I have a code which sets first way to my HyperLink:
HyperLink link = (HyperLink) item["documents"].Controls[0];
if (link.NavigateUrl.Contains(";"))
{
string[] linktext = link.NavigateUrl.Split(';');
link.NavigateUrl = linktext[0];
}
I would probably take a quick look at this SO answer here.
The takeaway is that this is not possible without using JavaScript functions:
Without JavaScript, it's not possible to open two pages by clicking
one link unless both pages are framed on the one page that opens from
clicking the link. With JS it's trivial.
The example is this:
<p><a href="#" onclick="window.open('http://google.com');
window.open('http://yahoo.com');">Click to open Google and Yahoo</a></p>
This would also work: (again see answer here)
Open Two Links With One Click
So, basically, I'm applying programmatically for jobs with Starbucks, so I don't open an browser. The problem every time is "click" (next) in the application they change the url for the next page, so my code doesn't know where to go. Help please
You can use this code snippet:
var page = new WebClient().DownloadString(url);
// search for the URL of the link with regular expression (see class Regex)
the problem i encounted concerns redirecting in the navigation menu. I'd like to dynamicly create a navmenu. Depending on what role the user has we get to see the required navigation menu items.
At the moment i use:
if (found)
{
if (admin == true)
{
NavigationMenu.Items.Add(new MenuItem("Agenda", "/AdminPages/Agenda.aspx"));
NavigationMenu.Items.Add(new MenuItem("Add Product", "/AdminPages/ProductToevoegen.aspx"));
}
else if (user == true)
{}
This code I have placed in my Site.master.cs, but I also have a control in my login.aspx.cs code which does a Response.Redirect("~/AdminPages/Agenda.aspx"); to a certain page depending on admin or user once logged in. Now the problem I have is that when I log in, a part works, so it controls the role and adds the required navigation menu items. But when I click for example on the Add Product link it doesn't redirect me to the page. It keeps redirecting me to :
http://localhost:52853/AdminPages/Agenda.aspx
In the url bar it actually shows the url followed with a # when clicked and than redirect to the Agenda page.
Any ideas on how to fix this problem? I tried finding a way to put the navigation links in the login.aspx.cs code aswell but couldn't find the correct way to refer to the NavigationMenu, don't know if that could be off any help. Thank you in advance.
Look at the parameters for creating a new MenuItem.
When only passing 2 parameters, you are filling the text and value parameters. What you want to pass is a navigateUrl parameter. This is only available when passing an imageURL parameter (which can be empty).
Something like the following should fix your problem.
NavigationMenu.Items.Add(new MenuItem("Agenda", "", "", "/AdminPages/Agenda.aspx"));
Hope this helps.
I would suggest using a standard siteMap object and RoleProvider. And read a little bit about security trimming (you can specify on your sitemap nodes which roles can access which sites).
Hm might not be the correct way but i fixed it making another menu in the site.master.aspx file called MenuAdmin for example. Than in the code behind i've set the
NavigationMenu.visible = false;
and
MenuAdmin.visible = true;
Seems to work fine, does what it needs to do for now ;)
This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.
You have to set the Text property of the HyperLink... link.Text = "whatever"
Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx
C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.