PhantomJSDriver- How to initialize the driver? - selenium-webdriver

Can someone tell me how I begin to test with PhantomJS? I have downloaded the PhantomJS exe file and also set my "path" variable.
And I do something similar for chrome like
if(config.getProperty("browser").equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:drivers\\chromedriver.exe");
driver= new ChromeDriver();
}
which is
if(config.getProperty("browser").equals("phantom"))
{
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver= new PhantomJSDriver();
}
But, this does not work. Please help. I do not want to use RemoteWebDriver or GhostDriver

I figured it out:
if(config.getProperty("browser").equals("phantom"))
{
System.setProperty("phantomjs.binary.path", "C:\\drivers\\chromedriver.exe");
driver= new PhantomJSDriver();
}

PhantomJSDriver accepts path to executable as a constructor argument:
if (config.getProperty("browser").equals("phantom"))
driver= new PhantomJSDriver("C:\\drivers\\chromedriver.exe");

Find the phantomjs code below and set the property key and value:
//System.setProperty("phantomjs.binary.path","phantomjs.exe path");
System.setProperty("phantomjs.binary.path", System.getProperty("user.dir")+"/ExternalLibraryFiles/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
driver =new PhantomJSDriver();

Related

Running Selenium-java automation within gitpod.io

New to SO & test automation & selenium. I got introduced to gitpod while attempting https://www.lambdatest.com/certifications/.
I'm stuck trying to run the below simple code snippet in side Gitpod.
package project.maven.selenium;
import org.openqa.selenium.WebDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class WebDriverManagerDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = WebDriverManager.chromedriver().create();
// Navigate to the demoqa website
driver.get("https://www.lambdatest.com/selenium-playground/checkbox-demo");
Thread.sleep(3000);
driver.quit();
}
}
Couldn't find a way pass the error below,
Exception in thread "main" io.github.bonigarcia.wdm.config.WebDriverManagerException: There was an error creating WebDriver object for Chrome at io.github.bonigarcia.wdm.WebDriverManager.instantiateDriver(WebDriverManager.java:1775) at io.github.bonigarcia.wdm.WebDriverManager.create(WebDriverManager.java:425) at project.maven.selenium.WebDriverManagerDemo.main(WebDriverManagerDemo.java:9) Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
Can someone please point out what I'm doing wrong here?
Researched hours but Selenium on Gitpod is not much of a topic, read the getting started guide with Gitpod too, to find a resolution to my problem but no luck before posting here.
Below line of code is incorrect:
WebDriver driver = WebDriverManager.chromedriver().create();
Try the below code:
public static void main(String[] args) throws InterruptedException {
// below line is used to setup chromedriver using WDM
WebDriverManager.chromedriver().setup();
//initialize the driver object
WebDriver driver = new ChromeDriver();
// Navigate to the demoqa website
driver.get("https://www.lambdatest.com/selenium-playground/checkbox-demo");
Thread.sleep(3000);
driver.quit();
}

selenium c# edge browser : 'Unexpected error. Unknown error'

OS Build Number:- 18363.900
Edge Version: 83.0.478.54
MicrosoftWebDriver Version: 83.0.478.54
Here is the code:
System.Environment.SetEnvironmentVariable("webdriver.edge.driver", "E:\\edgedriver_win64\\msedgedriver.exe");
IWebDriver driver = new EdgeDriver();
I am getting error as: OpenQA.Selenium.WebDriverException: 'Unexpected error. Unknown error'
Which version of Selenium WebDriver are you using? I suggest you to use the latest version 4.0.0-alpha05 and use the code below:
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var msedgedriverDir = #"D:\webdriver83.0.478.54";
var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
driver.Navigate().GoToUrl("https://bing.com");
Thread.Sleep(3000);
driver.Close();
Note: Change the paths in the code to your owns.
Result:

Selenium "Cannot login"

I am unable to access through this lock icon and get to login. I have used different locator options.
The current code I am using:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("example.com");
driver.manage().window().maximize();
driver.findElement(By.id("login-trigger")).click();
driver.findElement(By.id("username")).sendKeys("User1");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("submit")).click();
Instead of writing driver.findElement(By.id("submit")).click();
try this : driver.findElement(By.id("submit")).sendKeys(Keys.ENTER);

Getting error "java.lang.IllegalStateException:The driver executable does not exist"

I have imported my selenium code from windows to mac and installed chromedriver for mac and updated systemproperty accordingly.but it is throwing exception and showing previously set path:
java.lang.IllegalStateException: The driver executable does not exist: /Users/Preet12/Desktop/Selenium/AddaStore/C:\Users\cp\Downloads\chromedriver_win32\chromedriver.exe
however the path in mac is : /Users/Preet12/Desktop/Selenium/AddaStore/chromedriver 2
it is showing path of window also i.e. C:\Users\cp\Downloads\chromedriver_win32\chromedriver.exe
Code:
public class WithTestNG
{
WebDriver driver;
#BeforeClass
public void StartBrowser()
{
System.setProperty("webdriver.chrome.driver","/Users/Preet12/Desktop/Selenium/AddaStore/chromedriver 2" );
driver = new ChromeDriver();
driver.manage().window().maximize();
}
Try Navigating to the folder containing chromedriver and run
"chmod +x chromedriver"

Files not found error when tried to upload files using AutoIT and Selenium

When I am trying to run the AutoIT.exe script or my Selenium script, it is throwing 'File Not found error'. Please suggest!
Selenium code snippet: ===================================
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\IBM_ADMIN\\Desktop\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.automationtesting.in/Register.html");
driver.manage().window().maximize(); Thread.sleep(2000);
driver.findElement(By.xpath("//input[#id='imagesrc' and #type='file']")).click();
Runtime.getRuntime().exec("C:\\Users\\IBM_ADMIN\\Desktop\\Selenium practise\\SeleniumAssignment\\AutoIT\\MultipleFileUpload.exe"+" "+"C:\\Users\\IBM_ADMIN\\Desktop\\0234_005.pdf"); Thread.sleep(2000);
driver.findElement(By.xpath("//input[#id='imagesrc' and #type='file']")).click();
Runtime.getRuntime().exec("C:\\Users\\IBM_ADMIN\\Desktop\\Selenium practise\\SeleniumAssignment\\AutoIT\\MultipleFileUpload.exe"+" "+"C:\\Users\\IBM_ADMIN\\Desktop\\BRP_SujataChakraborty.pdf"); driver.quit(); }
AutoIt Code Snippet: ============================
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1",$CmdLine[0])
ControlSetText("Open","","Edit1",$CmdLine[1])
ControlSetText("Open","","Edit1",$CmdLine[2])
ControlClick("Open","","Button1")
Is brower file dialog active window? If not auto it script will enter value in window whichever is active. Do make sure file dialog active?. Do interact with brower once it is started. And one more thing if you input type is file then you can use send keys instead of auto it to enter value.

Resources