AutoIT in Selenium File upload locator cannot found - selenium

public class AutoITDemo {
public static void main(String[] args) throws IOException, InterruptedException {
test();
}
public static void test() throws IOException, InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://ps.uci.edu/~franklin/doc/file_upload.html");
driver.findElement(By.name("userfile")).click();
//calling AutoIT script
Runtime.getRuntime().exec("D:\\seleniumTests\\inprogress\\AutoIT\\FileUoloadScript.exe");
Thread.sleep(3000);
driver.close();
}
}
I am trying to go to this URL "https://ps.uci.edu/~franklin/doc/file_upload.html" and upload a file using AutoIT by clicking on the button. But the upload button is not clicked and the following Exception prints on the console. How to solve this?
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument
(Session info: chrome=89.0.4389.128)
screen shot

See if this works:-
WebElement browse = driver.findElement(By.xpath(".//input[#name='userfile']"));
browse.sendKeys("File path you want to upload");

Related

Two browsers are opening automatically in selenium

The following is my code. One browser opens blank and another opens google.
I am using the latest version of chrome driver. Thanks in advance.
public class Trials {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
Thread.sleep(3000);
driver.close();
}
}

How to resolve null pointer exception in selenium when page factory method is used

I have 3 classes - one for page locator, one for page action and the other one as script to execute the function. am getting nullpointer exception in main scripts where the function is called. Can anyone help me out, please!!!!.
The following are the code :
HomePageLocator.page
public class HomePageLocator{
WebDriver driver;
public HomePageLocator(WebDriver driver)
{
this.driver= driver;
}
#FindBy(xpath="//*[#id='header']/div[2]/div/div/nav/div[1]/a")
public WebElement signIn;
}
HomePageAction.page
public class HomePageAction{
public WebDriver driver;
public HomePageLocator homepageor;
public HomePageAction() {
this.homepage = new HomePageLocator(driver);
PageFactory.initElements(driver, this.homepage);
}
public void login() {
homepageor.signIn.click();
}
BaseTestCase.java
public class BaseTestCase {
public static Logger log = Logger.getLogger("devpinoyLogger");
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Home page validation
HomePageAction homepageaction= new HomePageAction();
homepageaction.login();
}
Note : Am getting exception in line (homepageaction.login();)
the following is the exception logs :
Exception in thread "main" java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy3.click(Unknown Source)
at com.way2.Pages.actions.HomePage.login(HomePageAction.java:31)
at com.way2.Testcases.BaseTestCase.main(BaseTestCase.java:35)
You are creating the driver in main class, but not passing it to homepageAction
public static void main(String[] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Try to pass driver as
HomePageAction homepageaction= new HomePageAction(driver);
this.driver =driver

Unable to open New tab in new its replacing the first tab rather than opening the new one in Firefox

public class TestTabs {
public WebDriver driver;
public WebDriver getDriver() {
return driver;
}
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver","/Users/Test/Downloads/geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND+ "t");
}
#Test
public void openSameUrlInMultipleTab() throws InterruptedException {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://seleniumlearn.com/selenium-tutorial");
{
driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND+ "t");
driver.get("https://www.facebook.com");
}
}
}
I am using the above code to open multiple tabs in one browser but When I am running this code in Firefox (iMac mini) I am not getting any error message and code is passed but 2nd link is opening in same tab rather than in new tab. How can I open multiple tabs in Firefox in Mac?
Each tab is considered as a new window. switch to the new tab using the switch.to().window().
driver.switchTo().window(winHandle);
driver.get("your new url goes here");
User below line if you want to switch to default browser (the base browser).
driver.switchTo().defaultContent();

How do I select package using selenium webdriver

public class chrome {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://neontv.co.nz");
Thread.sleep(2000);
driver.findElement(
By.xpath("//*[#class='nv-nav-freetrial-2 nv-nav-item']"))
.click();
Thread.sleep(200);
driver.findElement(
By.xpath("//ul[#id='yui_patched_v3_11_0_2_1529643447183_22']"))
.click();
Thread.sleep(2000);
driver.quit();
}
}
I am unable to select package on next page.. I tried changing the xpath but it didnt help.. is there any other way to select this. Could any one help please
Using Action classes, I was able to resolve this
driver.switchTo().defaultContent();
Actions action = new Actions(driver);
action.moveToElement(btnEnjoy).click().build().perform();

JUnit (Selenium WebDriver) to open www.google.co.uk in Chrome Browser

I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the
webdriver.chrome.driver system property; for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know,
Which PATH should I place that? or
How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
#Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
#Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "your\path\to\it");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();