I am currently developing an application in php / js / html and I would like to launch an application made in C # via a html button (no download) however I use Mozilla Firefox and the only solution I found is a script featuring ActiveXObject which is only used by IE apparently.
You can use the Firefox command line options of -url
"C:\Program Files\Mozilla Firefox\firefox.exe" -url "https:\\www.stackoverflow.com"
or
Use selenium Webdriver, this can launch the browser to any url you specify and can fill any form on the page with any details you specify and press any buttons that you can find.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
class HelloSelenium
{
static void Main()
{
using (IWebDriver driver = new FirefoxDriver())
{
WebDriverWait wait = new WebDriverWait(driver);
driver.Navigate().GoToUrl("https://www.stackoverflow.com/ncr");
driver.FindElement(By.Name("q")).SendKeys("Vote Up" + Keys.Enter);
}
}
}
Related
I have loaded an html file in a webkit view inside my app. The html file acts like a search engine and has the following code which gets triggered when the users hits the search action:
function search() {
var input = document.getElementById("search_form_input_homepage").value;
window.open("https://duckduckgo.com/?q=" + input);
}
However, I need the result url to open in Safari. As of now, window.open loads up the url in the default browser. How do I achieve this?
I found this link that talks about different url schemes in Safari. But I am very new to JavaScript. This is in fact the first time I am working with JS. So I could not understand how to implement this. Can anybody help me with this, please?
I managed to do this using Swift. I added the following webkit delegate method.
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
NSWorkspace.shared.open([navigationAction.request.url!], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
return nil
}
You can't control which browser to open via JavaScript code, it's related to the operating system / user default selection.
I have testing environment which is perfectly working with chrome driver in desktop mode. I am using some javascript injections (everything works) f.e.:
public static void ForceFillInput(this Driver driver, string selector, string value)
{
var javaScriptExecutor = (IJavaScriptExecutor)driver.webDriver;
javaScriptExecutor.ExecuteScript($"$(\"{selector}\").val(\"{value}\")");
}
but when i want to run it in headless mode
AddArguments("--headless")
it will just fail on
"$ is not defined"
Can somebody help me how to inject js/jquery into headless solution?
M.
your Javascript snippet used jQuery api. In modern web development, we put Javascript at the end of HTML page to let browser to load javascript at last, so that static resources (like picture/image/text content) can display earlier as possible, withing this way to improve user experience when user open website.
I think your page also put jQuery at the end to load, try add some wait/sleep before ExecuteScript to wait browser complete load jQuery.
It looks like the shorthand for JQuery is not yet created at the time your script is executed.
Use a waiter to wait for JQuery and for the selector to be found:
public static void ForceFillInput(this Driver driver, string selector, string value)
{
string JS_SET_VALUE =
"var e; return !!window.$ && (e = window.$(arguments[0])).length > 0 && (e.val(arguments[1]), true);";
new WebDriverWait(driver, TimeSpan.FromSeconds(60))
.until(ctx => (bool)((IJavaScriptExecutor)ctx).ExecuteScript(JS_SET_VALUE, selector, value));
}
I have added a plugin in chrome but, how can i access it through webdriver
File addonpath = new File("path of .crx file");
ChromeOptions chrome = new ChromeOptions();
chrome.addExtensions(addonpath);
WebDriver driver = new ChromeDriver(chrome );
Hi please do it like below
public class ChromeProfileWithAddOn {
public static void main(String[] args) {
// TODO Auto-generated method stub
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
// i have added this add on on chrome = https://chrome.google.com/webstore/detail/pdf-converter/dmgnkfgleaamgbhhojkfijjmjmngokkb
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
########paste the location of .crx file you get in step 6 here########
options.addExtensions(new File("C:\\Users\\###\\Desktop\\hgmloofddffdnphfgcellkdfbfbjeloo.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.getProperty("webdriver.chrome.driver","D:\\eclipseProject\\###\\src\\com\\###\\chromedriver_win32 (1)\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver(capabilities);
// call chrome driver
driver.navigate().to("chrome-extension://ldlmdngominhfffemgnfpoifladkpple/RestClient.html");
How to get above url :
1.open the mainfest.json file there you will find "local_path":"RestClient.html"
2. make sure your extension is installed in chrome and then
3. go to chrome://extensions/ (follow steps-3 and 4 below)
4. you will get an ID value as shown in the image just below step 4 copy that
5. now u can make your url as "chrome-extension://ID/local_path"
6. now open it in chrome browser
} }
Code will be as above but we have to follow Some basic steps :
NOTE : i am talking example of Advanced REST client for ** CHROME** url below : https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
Step 1 :
`Please download or if in-case already present go to the extension's manifest.json file`
if you want to download the add-on on your local drive please follow like this :
a.> download this extension https://chrome.google.com/webstore/detail/chrome-extension-source-v/jifpbeccnghkjeaalbbjmodiffmgedin
b.> install it (on top right hand side of the chrome browser a CRX button will appear)
c.> Now search Advanced REST client in the chrome web Store or simply copy and paste the link in the browser
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
d.> Click on the CRX image/button, you will get two options
Select download as zip (save it at your preferred location).
e.> unzip it
f.> inside unzipped folder you will find manifest.json file
Step 2 :
Copy the location of mainfest.json file
Step 3 : go to Chrome browser and in the url please type
chrome://extensions/
here all installed add on/extensions will be shown
Step 4 :
please check the Developer option
option for Pack Extension will be visible now
Step 5 :
Click Pack Extension and under Extension root directory:(First option) paste the location of manifest.json file.
Step 6 : if u have followed everything as above u will get this
Hope this solves your query.
I'm trying to use the HtmlUnitDriver and WebElement classes of Selenium in Java to click the "Download as CSV" button on Google trends.
The problem I'm having is that that button is hidden (not displayed) until you click a different settings menu button, but I can't click that settings menu button with WebElement.
Here is my code:
/**
* #args String, the term to search on Google Trends
*/
public static void main(String[] args)
{
//instantiate an HtmlUnitDriver
HtmlUnitDriver hud = new HtmlUnitDriver();
//navigate to the 90-day Google Trends page of the input term in args
hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");
//set element to the first button to press
WebElement element = hud.findElement(By.id("settings-menu-button"));
//click the element
element.click();
}
The error I am getting is: org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements
But the settings menu button is visible?
This is my first time making a program like this and using this library, so thanks for any help. I'm still learning.
Can you try this
public static void main(String[] args)
{
//instantiate an HtmlUnitDriver
HtmlUnitDriver hud = new HtmlUnitDriver();
wait = new WebDriverWait(hud , 120);
//navigate to the 90-day Google Trends page of the input term in args
hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("settings-menu-button")).click();
}
Switch to the real browser (e.g. Firefox, Chrome):
ChromeDriver hud = new ChromeDriver();
Reasons:
https://code.google.com/p/selenium/wiki/HtmlUnitDriver
None of the popular browsers uses the javascript engine used by
HtmlUnit (Rhino). If you test javascript using HtmlUnit the results
may differ significantly from those browsers.
https://gist.github.com/evandrix/3694955
Headless browsers that have JavaScript support via an emulated DOM
generally have issues with some sites that use more advanced/obscure
browser features, or have functionality that has visual dependencies
(e.g. via CSS positions and so forth)
I don't want to open the browser but the actual store in my Windows 8 phone.
I am developing an app using PhoneGap and so I want to do this with Javascript.
I haven't submitted my app so I don't yet have a package name. How do I test this without an actual package name?
Also, I can't seem to be able to use:
Windows.System.Launcher.LaunchUriAsync(new Uri(appStoreURL));
I get:
Error:["'Windows' is undefined file:x-wmapp0:www\/js\/......
Any ideas?
SOLUTION:
Using Benoit's answer and some other stuff I found I managed to link straight to the review section by adding the following Plugin to my cordovalib:
LaunchReview.cs
using WPCordovaClassLib.Cordova.Commands;
using Microsoft.Phone.Tasks;
namespace Cordova.Extension.Commands
{
public class LaunchReview : BaseCommand
{
public void launchReview(string options)
{
// Use the Marketplace review task to launch the Store or Marketplace and then display the review page for the current app.
MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();
}
}
}
Note sure what value you are using for appurl but here is something which should work:
Windows.System.Launcher.LaunchUriAsync(new Uri("zune:reviewapp"));
or you can use:
MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();
To call it from javascript just create a plugin:
namespace Cordova.Extension.Commands
{
public class LaunchReview: BaseCommand
{
public void launchReview(string options)
{
// all JS callable plugin methods MUST have this signature!
// public, returning void, 1 argument that is a string
MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();
}
}
}
that you can use it like this from javascript:
cordova.exec(win, fail, "LaunchReview", "launchReview", [""]);
Here is the link to the plugin dev guide for windows phone
If you want to use window.open then you will need to modify the PhoneGap source code to use LAunchUri because currently it's just using WebBrowserTask instead of LaunchUri. The function to modify is Plugin/InAppBrowser.cs>ShowSystemBrowser
I used InAppBrowser cordova plugin.
cordova plugin add org.apache.cordova.inappbrowser
To open wp8 store i call from javascript:
window.open(UrlToMyApp, '_blank', 'location=yes');