I'm trying to find opened webpage by URI and to launch JS on it. I found some samples and wrote simple method. That's how it looks:
private void GetHtmlCode()
{
string uri = GetTargetURI();
if(!string.IsNullOrEmpty(uri))
{
IE ie = IE.AttachTo<IE>(Find.ByUrl(uri));
htmlCode = ie.Eval(JavaScriptToRun);
}
else
{
MessageBox.Show("Target page is not opened",
"Notification", MessageBoxButtons.OK);
}
}
And there's a method for getting URI:
private string GetTargetURI() //проверка URL
{
Regex reg;
Match match;
foreach(SHDocVw.InternetExplorer ie in shellWindows)
{
reg = new Regex(patternURL);
match = reg.Match(ie.LocationURL.ToString());
if (!string.IsNullOrEmpty(match.Value))
{
pageURL = ie.LocationURL.ToString();
return pageURL;
}
pageURL = string.Empty;
}
return pageURL;
}
- so URI is completely correct or empty.
The problem is IE ie = IE.AttachTo<IE>(Find.ByUrl(uri)); always throws
WatiN.Core.Exceptions.BrowserNotFoundException: Could not find an IE window matching constraint: Attribute 'href' equals uri '%my_target_URI%'. Search expired after '30' seconds.
I've googled a lot, but still didn't find any solution :(
Can anybody help please?
Thanks.
Try to use:
Browser.AttachTo<IE>(Find.ByUrl(u => u.Contains("NewMapping")))
And make sure your function is not returning empty, since this should be the reason why the windows is not being captured.
Regards!
Related
I am working on a cefsharp based browser and i am trying to implement a search engine into the browser, but the code I have tried docent work, it doesn't really have any errors but when i star the project and type something i the text field nothing happens and it dosent load the search engine i entered into the code, the only time the textbox loads anything is when a url is typed.
This is the code used in the browser that docent work
private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
WebUI.Load(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + WebUtility.HtmlEncode(url);
WebUI.Load(searchUrl);
}
}
i have also tried
void LoadURl(String url)
{
if (url.StartsWith("http"))
{
WebUI.Load(url);
}
else
{
WebUI.Load(url);
}
}
i was also suggested to try
private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
WebUI.LoadUrl(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
WebUI.LoadUrl(searchUrl);
}
}
We have here really few Information on how your code works. But what I notice is that you use WebUtility.HtmlEncode for the search query. WebUtility has also a WebUtility.UrlEncode Method, that how I understand your question makes more sense it the context. This is the documentation for the method: https://learn.microsoft.com/de-de/dotnet/api/system.net.webutility.urlencode
The Url you are generating is invalid. You need to use Uri.EscapeDataString to convert the url param into a string that can be appended to a url.
// For this example we check if a well formed absolute Uri was provided
// and load that Url, all others will be loaded using the search engine
// e.g. https://github.com will load directly, attempting to load
// github.com will load the search engine with github.com as the query.
//
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
chromiumWebBrowser.LoadUrl(url);
}
else
{
var searchUrl = "https://www.google.com/search?q=" + Uri.EscapeDataString(url);
chromiumWebBrowser.LoadUrl(searchUrl);
}
nothing happens and it dosent load the search engine
You need to subscribe to the LoadError event to get actual error messages. It's up to you to display errors to the user. The following is a basic example:
chromiumWebBrowser.LoadError += OnChromiumWebBrowserLoadError;
private void OnChromiumWebBrowserLoadError(object sender, LoadErrorEventArgs e)
{
//Actions that trigger a download will raise an aborted error.
//Aborted is generally safe to ignore
if (e.ErrorCode == CefErrorCode.Aborted)
{
return;
}
var errorHtml = string.Format("<html><body><h2>Failed to load URL {0} with error {1} ({2}).</h2></body></html>",
e.FailedUrl, e.ErrorText, e.ErrorCode);
_ = e.Browser.SetMainFrameDocumentContentAsync(errorHtml);
}
For testing purposes you can also copy and paste the searchUrl string you've generated and try loading it in Chrome to see what happens, you should also get an error.
I tried to write the auto-test with Selenium using C# and asserts the text in Google Search Button.
However the test got failed.
How to do it correctly and what's wrong here?
enter code here
[Test]
public void TestIfButtonNameIsGoogleSearch()
{
Driver.Navigate().GoToUrl("https://www.google.com/?gws_rd=ssl");
var btnSearch = Driver.FindElements(By.Name("btnK"));
if(btnSearch.Count==2)
{
Assert.That(true);
}
string expName = btnSearch.LastOrDefault().Text;
Assert.AreEqual(expName, "Google Search");
}
Use .GetAttribute("value") instead of .Text
you can try the following code. By default inputs are holding the text in 'value' attribute. Normaly its hidden.
Code Example:
var btnSearch = Driver.FindElements(By.Name("btnK"));
var btnFeelingLucky = Driver.FindElements(By.Name("btnI"));
var searchBtnText = btnSearch.GetAttribute("value");
var feelingLuckyBtnText = btnFeelingLucky.GetAttribute("value");
Assert.AreEqual(searchBtnText , "Google Search");
Assert.AreEqual(feelingLuckyBtnText , "I'm Feeling Lucky");
If the 'value' does not return anything or its empty, you can try with:
string btnText = javaScriptExecutor.ExecuteScript("return arguments[0].value", searchBtnText) as string;
I currently have a button in a table view. I have a custom cell class and an update cell method.
public void UpdateCell (string subtitle2)
{
call.SetTitle(subtitle2, UIControlState.Normal);
call.TouchUpInside += (object sender, EventArgs e) => {
UIApplication.SharedApplication.OpenUrl(new NSUrl("tel:" + subtitle2));
};
}
However, when i run this segment of code, i get an error.
Could not initialize an instance of the type
'MonoTouch.Foundation.NSUrl': the native 'initWithString:' method
returned nil. It is possible to ignore this condition by setting
MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.
Try this:
public void UpdateCell (string subtitle2)
{
//Makes a new NSUrl
var callURL = new NSUrl("tel:" + subtitle2);
call.SetTitle(subtitle2, UIControlState.Normal);
call.TouchUpInside += (object sender, EventArgs e) => {
if (UIApplication.SharedApplication.CanOpenUrl (callURL)) {
//After checking if phone can open NSUrl, it either opens the URL or outputs to the console.
UIApplication.SharedApplication.OpenUrl(callURL);
} else {
//OUTPUT to console
Console.WriteLine("Can't make call");
}
};
}
Not sure if phone calls can be simulated in the iPhone Simulator, otherwise just connect your device :-)
As mentioned in other answers, NSUrl cannot handle spaces (and some other symbols) in URL string. That symbols should be encoded before passing to the NSUrl constructor. You can do the encoding with the NSString.CreateStringByAddingPercentEscapes method.
Another way is using of the .NET Uri class:
new NSUrl(new Uri("tel:" + subtitle2).AbsoluteUri)
The issue you might encounter here is parsing of the schema by the Uri class. To avoid it, don't use ://, so, URL strings should look as "tel:(123) 345-7890", not as "tel://(123) 345-7890"
Try
UIApplication.SharedApplication.OpenUrl(new NSUrl("tel://" + subtitle2));
This error means that calling initWithString: returned nil. ObjC init* methods can return nil while .NET constructors can't (and throws).
In general ObjC returns nil on init* selectors when the input parameters are invalid. In this case I suspect your URL format is invalid.
What's the content of your subtitle2 variable ?
I resolved the same problem, after trying various ways, by finally figuring out that there should not be any spaces (instead of e.g. 1800 111 222 333, should be like 1800111222333) between numbers to make call in iOS.
number = number.Replace(" ", "");
NSUrl url = new NSUrl(string.Format(#"telprompt://{0}", number));
return UIApplication.SharedApplication.OpenUrl(url);
I'm developing a website in ASP.Net 4. One of the requirements is to log search queries that people use to find our website. So, assuming that a URL parameter named "q" is present in Referrer, I've written the following code in my MasterPage's Page_Load:
if (!CookieHelper.HasCookie("mywebsite")) CookieHelper.CreateSearchCookie();
And my CookieHelper class is like this:
public class CookieHelper
{
public static void CreateSearchCookie()
{
if (HttpContext.Current.Request.UrlReferrer != null)
{
if (HttpContext.Current.Request.UrlReferrer.Query != null)
{
string q = HttpUtility.ParseQueryString(HttpContext.Current.Request.UrlReferrer.Query).Get("q");
if (!string.IsNullOrEmpty(q))
{
HttpCookie adcookie = new HttpCookie("mywebsite");
adcookie.Value = q;
adcookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(adcookie);
}
}
}
}
public static bool HasCookie(string cookiename)
{
return (HttpContext.Current.Request.Cookies[cookiename] != null);
}
}
It seems ok at the first glance. I created a page to mimic a link from Google and worked like a charm. But it doesn't work on the host server. The reason is that when you search blah blah you see something like www.google.com/?q=blah+blah in your browser address bar. You expect clicking on your link in the results, will redirect to your site and you can grab the "q" parameter. But ,unfortunately, it is not true! Google, first redirects you to an address like:
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCgQFjAA&url=http%3A%2F%2Fwww.mywebsite.com%2F&ei=cks5Uof4G-aX0QXKhIGoCA&usg=AFQjCNEdmmYFpeRRRBiT_MGH5a1x9wUUlg&bvm=bv.52288139,d.d2k&cad=rja
and this will redirect to your website. As you can see the "q" parameter is empty this time! And my code gets an empty string and actually doesn't create the cookie (or whatever).
I need to know if there is a way to solve this problem and get the real "q" value. The real search term user typed to find my website. Does anybody know how to solve this?
Google stopped passing the search keyword:
http://www.searchenginepeople.com/blog/what-googles-keyword-data-grab-means-and-five-ways-around-it.html
I am trying to use a browser object to get some data from a website. the problem is that for one site i have to redirect, get some other info and then go back to this site.
my coed so far is
private void getInfo(cEXWB browser, string url)
{
if (url == "www.specificwebsite.com")
{
browser.navigate2("www.mywebsite.com");
int myAnswer = getData(browser);
}
browser.navigate2(url);
}
the problem is that i can NEVER get my browser to navigate 2 times. That is - if i need to navigate to "www.mywebsite.com" - it doesn't navigate to url.
What am i doing wrong?
Thanks!
Try that:
private void getInfo(cEXWB browser, string url)
{
if (url == "www.specificwebsite.com")
{
browser.navigate("www.mywebsite.com");
int myAnswer = getData(browser);
}
browser.navigate(url);
}
I think that browser.navigate2("www.mywebsite.com");
the 2 is messing it up
You should wait till first navigation is completed.
It should be something like:...
browser.Navigated += (sender, webBrowserNavigatedEventArgs) =>
{
int myAnswer = getData(browser);
browser.Navigate(secondurl);
};
browser.Navigate(firsturl);