How to copy url from the next webpage programmatically? - c#

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)

Related

Finding CSS selector path for Selenium C#

I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.

Find and click a link with text - Coded UI

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");

C# Webbrowser navigating links in order

I'm trying to teach myself C# and to start I'm trying to convert a program I originally wrote in Autoit.
I'm using a Windows Application Form and the program is suppose to take one or two links as input. Navigate to those to pages, grab some links from a table, then visit each of those pages to grab some content.
If only one link is entered it seems to go to that page and grab the links from a table like it is suppose to. If two links are entered it seems to only grab the links from the second table.
So if two links are passed this method
private void getPageURLList(string site1, string site2)
{
getPageURLList(site1);
getPageURLList(site2);
}
Calls the same method that gets called when there is only one link
private void getPageURLList(string site)
{
webBrowser.DocumentCompleted += createList;
webBrowser.Navigate(site);
}
I'm pretty sure the issue is "Navigate" is getting called a second time before createList even starts the first time.
The reason I am using WebBrowser is because these pages use Javascript to sort the links in the table so HTTPRequests and the HTMLAgilityPack don't seem to be able to grab those links.
So I guess my question is: How can I keep my WebBrowser from navigating to a new page until after I finish what I'm doing on the current page?
You have to make a bool variable to know when the first proccess has completed. And then start the other. Application.DoEvents() will help you.
Note that all this events run in the main thread.
In your documentcompleted event you do your link processing. At the end of the link processingyou tel the browser to navigate to the next url

ASP.Net Hyperlink navigation URL not including server name

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

Getting url of a page once making a form submission

I am designing a winforms based testing app (which is based upon WatiN). I specify a page to test and the value to insert into a textbox, and then click the corresponding button.
Is it possible to add a query string to the request I make (when clicking button) and then get the URL of the next page? Based on this, I need to screen scrape it.
Not sure about Watin syntax, but in Watir (Ruby version) you can get URL of the page displayed in browser with
browser.url
Or do you need to get URL of the next page before you open it?
Based on your comment to AmitK, Ċ½eljko's answer is right.
In WatiN (and C#), the syntax is:
Console.WriteLine("The current page is:" + ie.Url.ToString());
(just in case: 'ie' is the browser reference, use whatever object you use to enter text and click buttons)
What exactly do you mean by "next page" ? Does the form when submitted redirect to another page? If so, you will receive a HTTP 302/303 status code with the URL of the next page.

Categories