I am trying to use PhantomJS with Selenium Webdriver and got success but for a specific website I see that it does not navigate to the URL. I have tried it with both Python and C#.
Python Code:
dcap = dict(webdriver.DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
service_args = ['--load-images=false', '--proxy-type=None']
driver = webdriver.PhantomJS(executable_path="C:\\phantomjs.exe", service_args=service_args, desired_capabilities=dcap)
driver.get("https://satoshimines.com")
print driver.current_url
The output of this code snippet is: about:blank
Whereas it works fine for any other website.
Same code with C#:
IWebDriver driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("https://satoshimines.com");
Console.WriteLine(driver.Url);
The output of the C# program is also same.
I am stuck here and need help.
Following is a complete code solution for c# -
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.IgnoreSslErrors = true;
service.LoadImages = false;
service.ProxyType = "none";
driver = new PhantomJSDriver(service);
For me, the solution was as follows:
var service = PhantomJSDriverService.CreateDefaultService();
service.SslProtocol = "tlsv1"; //"any" also works
driver = new PhantomJSDriver(service);
I have no idea why the default sslv3 will not work. If you are sure the SSL certificates are valid, it is quite recommended not to ignore errors to protect against malicious certificates.
Update: For a very good explanation why SslProtocol should now be set to tlsv1 instead of the default sslv3, please take a look at the excellent cross link provided below by #Artjom B.
It seems I have found a solution to this. The problem was an SSL handshake problem.
By passing
'--ignore-ssl-errors=true' as a service_args to phantomjs solves the issue.
Thanks
this worked for me:
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes", "--ssl-protocol=tlsv1"});
driver = new PhantomJSDriver(capabilities);
Ran into this issue on an application quite abruptly after running phantomjs 1.9.7 for months without incident. The solution? Update phantomjs to 2.0.0.
Related
I am trying to learn how to write automated web tests in SauceLabs, and Visual Studio is telling me that DesiredCapabilities is deprecated in Selenium 3. I figured out how to use ChromeOptions for desktop tests, but what about mobile web tests? This works:
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("deviceName", "iPhone 8 Simulator");
caps.SetCapability("deviceOrientation", "portrait");
caps.SetCapability("platformVersion", "12.0");
caps.SetCapability("platformName", "iOS");
caps.SetCapability("browserName", "Safari");
caps.SetCapability("username", SauceUsername);
caps.SetCapability("accessKey", SauceAccessKey);
caps.SetCapability("name", TestContext.TestName);
_driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
caps, TimeSpan.FromSeconds(600));
But I don't want to use a deprecated class. I've used Selenium exensively in the past, but this my first time doing mobile web tests (no apps, just Safari/mobile Chrome). Should I be using an Appium Driver instead?
There will be a AppiumOptions() in a future release of Appium v4 that will replace this. You can pull it down now and give it a try.
It will look something like this:
public void SimpleTest()
{
var appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, "7.1.1");
appiumOptions.AddAdditionalCapability(MobileCapabilityType.FullReset, true);
appiumOptions.AddAdditionalCapability(MobileCapabilityType.NewCommandTimeout, 60);
appiumOptions.AddAdditionalCapability("testobject_api_key", "0D6C044F19D0442BA1E11C3FF087F6A6");
appiumOptions.AddAdditionalCapability("username", SauceUser.Name);
appiumOptions.AddAdditionalCapability("accessKey", SauceUser.AccessKey);
//TODO first you must upload an app to Test Object so that you get your app key
var rdcUrl = "https://us1.appium.testobject.com/wd/hub";
var driver = new AndroidDriver<IWebElement>(new Uri(rdcUrl), appiumOptions);
driver.Navigate().GoToUrl("https://www.ultimateqa.com");
Console.WriteLine("");
driver.Quit();
}
I think you should try this with appium driver. Just download appium c# client and start. You can use following link to start with.
http://appium.io/docs/en/writing-running-appium/web/mobile-web/
IDE: Visual Studio 2015
Geckodriver.exe (Version 0.19.0) - Published: 9/25/2017
Firefox version: 56.0b8 (64-bit)
Selenium webdriver version: 3.6.0
Using Selenium and C # I did as lines of code below:
using (FirefoxDriver = new FirefoxDriver () driver)
{
driver.Navigate (). GoToUrl("https://www.teste.gov.br/seguro/loginPortal.asp");
Thread.Sleep (1000 * 60);
}
The page opens a message of "Your connection is not private", error "Your connection is not secure".
To resolve this issue you have already used the following codes:
profile = webdriver.FirefoxProfile ()
profile.accept_untrusted_certs = True
driver = new FirefoxDriver (profile)
profile = webdriver.FirefoxProfile ()
profile.accept_untrusted_certs = True
driver = new FirefoxDriver (profile)
profile.setAcceptUntrustedCertificates (true);
profile.setAssumeUntrustedCertificateIssuer (false);
driver = new FirefoxDriver (profile)
ffProfile.setAcceptUntrustedCertificates (true)
ffProfile.setAssumeUntrustedCertificateIssuer (false)
driver = new FirefoxDriver (ffProfile)
and other codes ...
But the problem continues. How to solve this problem?
TL:DR; There is no reasonable approach to solving this problem.
If it's absolutely necessary to visit the site only, e.g. to create a cookie, use PhantomJS. Every browser driver I've tried gives the error, and it's impossible to bypass without some sort of security exploit.
The browser is literally letting you know the site is insecure. Albeit its a government site, it might be compromised.
On a separate note, its probably cleaner to do this:
FirefoxDriver() ffDriver = new FirefoxDriver();
ffDriver.Navigate("myCoolSite.url");
I try to make hidden FirefoxDriver. According to my research I must use PhantomJSDriver but when I use PhantomJSDriver driver.FindElement statement no longer does not work.
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/40.0.2214.94 Safari/537.36");
PhantomJSOptions p = new PhantomJSOptions();
var service = PhantomJSDriverService.CreateDefaultService();
service.SslProtocol = "any";
service.ProxyType = "http";
service.WebSecurity = false;
service.IgnoreSslErrors = true;
var driver = new PhantomJSDriver(service, options);
driver.Navigate().GoToUrl("https://www.google.com.tr/");
Thread.Sleep(5000);
driver.FindElement(By.XPath("//*[#id='lst-ib']")).SendKeys("edd");
string s = driver.Url;
Console.WriteLine(s);
Error message:
An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
Additional information:
{"errorMessage":"Unable to find element with xpath '//[#id='_fZl']/span/svg/path'","request":{"headers":{"Accept":"application/json, image/png","Connection":"Close","Content-Length":"57","Content-Type":"application/json;charset=utf-8","Host":"localhost:50454"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//[#id='_fZl']/span/svg/path\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/feab13f0-720f-11e7-80b3-452aee308158/element"}}
Is there any another way for hiding FirefoxDriver?
Could you help me please?
I solved it.
First of all
We can use PhantomJS without showing its console by this code:
IWebDriver driver;
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driver = new PhantomJSDriver(driverService);
Second for the error that I mentioned.
Google return different HTML pages for browsers so the Id or Xpath in PhantomJS browser will be different from that I export it when I was opening Firefox.
When I used
string html=driver.PageSource;
to know what the correct XPath or Id, findElement functiom is working well.
For example: For the Google site results
The first link's XPath in FirefoxDriver is
"//*[#id='rso']/div/div/div[1]/div/div/h3/a"
The first link's XPath in PhantomJSDriver is
"//*[#id='ires']//ol/div[1]/h3/a"
Since version 55+ for Linux and 56+ for Windows & OSX, Firefox supports the -headless command line option. It shall be used like this:
o = selenium.webdriver.FirefoxOptions()
o.set_headless()
driver=selenium.webdriver.Firefox(options=o)
The corresponding code in C# would be:
var o = new FirefoxOptions()
o.AddArgument('-headless')
var driver = new FirefoxDriver(o)
Because the .NET wrapper doesn't support the .headless property.
There is no way to hide FirefoxDriver per se. You could run it on a virtual machine and minimize the vm window but that's not practical for most people.
Let's take a look at your real problem though. It looks like Google is assigning the id of the search box with js to prevent scraping since it's against their terms of service.
You have a couple options here:
1) locate the element using the name 'q' since it's named that regardless of phantomjs or firefox.
2) just go directly to the search results page: https://www.google.com.tr/search?q=edd
I have an input element that opens a new popup window when clicked (where the user can select a value for the field).
Markup:
<html>
<input type="text" id="myPopup" readonly="readonly" name="myPopup">
</html>
c#:
var driver = new PhantomJSDriver(#"C:\PhantomJS");
driver.Navigate().GoToUrl(#"http://username:password#localhost/myUrl.aspx");
var popupField = driver.FindElementById("myPopup");
popupField.Click();
(I'm passing credentials in the URL for Windows Authentication)
I get a WebDriverException:
"The HTTP request to the remote WebDriver server for URL ...element/:wdc:1389663237442/click timed out after 60 seconds."
All other interactions I tried work except this particular element. Also tried with IE/Chrome drivers and it worked.
Any ideas?
PhantomJS 1.9.2,
C# / GhostDriver,
Selenium Webdriver 2.39,
Windows 7 x64.
Let me know if there's any other info I can provide.
I had a similar issue. Tests worked on FF but timed out on PhantomJs, as you describe. The pages I was testing used a lot of social media plugins which I think were using XHR. Removing most of the security restrictions on PhantomJs fixed it for me (see below).
service.IgnoreSslErrors = true;
service.WebSecurity = false;
service.LocalToRemoteUrlAccess = true;
service.DiskCache = true; // Dunno what this does but I thought it might help.
PhantomJSDriver driver = new PhantomJSDriver(service);
I've managed to handle a missing security certificate in IE8, however quite often the browser will hang while loading "Downloading picture res://ieframe.dll/background_gradient_red.jpg..." and any following IE tests on the node fail also.
I'm working with the ops team to fix the certificate issue, but in the meantime has anyone else seen this problem?
In case it helps here is how I'm creating the driver...
DesiredCapabilities capabilities = DesiredCapabilities.InternetExplorer();
capabilities.SetCapability(CapabilityType.AcceptSslCertificates, true);
capabilities.SetCapability(CapabilityType.HandlesAlerts,true);
capabilities.SetCapability("ignoreProtectedModeSettings",true);
driver = new RemoteWebDriver(new Uri(GridHubUrl), capabilities);
driver.Manage().Cookies.DeleteAllCookies();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60));
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(60));
And this bit handles clicking override...
public static void Handle()
{
if (driver.ToString() == "OpenQA.Selenium.IE.InternetExplorerDriver" ||
driver.Url.Contains("res://ieframe.dll/invalidcert.htm"))
{
try
{
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");
Today I faced the same issue, but resolved by doing this-
Browser setting:
In your browser go to:
Settings-> Internet Options->Security-> Trusted Sites ->"Sites" button -> Add your site
System.setProperty("webdriver.ie.driver","C:\\Users\\XXXXXX\\Desktop\\selenium jars\\Eclipse Jars\\IEDriverServer_x64_2.29.0\\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
driver = new InternetExplorerDriver();
driver.get(baseUrl + "/content/");
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
driver.findElement(By.id("edit-acct")).clear();