Object error Firefox when automating with eclipse - java

I am getting an [object Object] error when I try to automate a web page.I am using Firefox v39 as later version cause problems in online signature. My code did work properly for Firefox v63.
Code is simply to open a webpage, enter username and password, login, and then navigate to a page.
I have provided a Screenshot of the error. Please check.
enter image description here
CODE-
System.setProperty("webdriver.firefox.marionette","F:\\firefoxdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("LINK");
driver.manage().window().maximize();
driver.findElement(By.id("username")).sendKeys("USERNAME");
driver.findElement(By.id("password")).sendKeys("PASS");
driver.findElement(By.xpath("//button[#class='btn btn-success btn-logging']")).click();
WebElement kyc =driver.findElement(By.xpath("/html/body/div[1]/div/div/ul/li[2]/ul/li[6]/a"));
String href=kyc.getAttribute("Href");//get link to approval
driver.navigate().to(href);

This happens when the page is not completely loaded and hence an object is not found. This could happen due to slow net, slow site.
Solution-
Increase the wait, used static wait and keep increasing it unless it does not occur.

Related

Correct XPath but sendKeys() not working in Selenium

I'm trying to send text to the password field at https://signup.live.com/ however while all other fields are populated correctly (and the XPath for password field is also correct -- .//*[#id='Password']) the data doesn't show up for in the password and password confirmation fields.
I have a feeling it has to do with the fact that after sending email/user name the page (sort of) refreshes (it displays "this username is available" or "unavailable") but this only effects the password fields..
I am not sure how to resolve the issue as the XPath I'm using is correct..
Any ideas?
Thanks
P.S
Some people say the email validation is causing the error which is probably correct. However when I add:
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
it does not seem to fix the issue. Sometimes it works, most of the time it doesn't.
It causing problem due to email validation. Once you send an email the site validates and selenium tries to send password meanwhile but couldn't find the element focused and throws So it couldn't focus on password field and throws :
Exception in thread "main" org.openqa.selenium.WebDriverException:
unknown error: cannot focus element
So before sending password you have to put some wait until the emailid get validated (analyze how much time it takes to validate email) and then send your password. Here Thread.sleep() will help you to do so. but some time its not recommended because it pause your script for given time event your element intractable before given time.
Another recommendation is if you have element id then avoid to use xpath you can locate same like driver.findElement(By.id("Password")).
For your script sample code can be :
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://signup.live.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("FirstName")).sendKeys("test");
driver.findElement(By.id("LastName")).sendKeys("user");
driver.findElement(By.id("MemberName")).sendKeys("testuser#gmail.com");
Thread.sleep(10000);
driver.findElement(By.id("Password")).sendKeys("testone1#");
driver.findElement(By.id("RetypePassword")).sendKeys("testone1#");

Using Selenium on https web application that runs over vpn

First of all, I need to state that I am total beginner with Selenium.
I am trying to test an application in firefox browser using Selenium. Due to security issues the application works only over vpn.
My problem occurs with the following steps; I create the webdriver and navigate to the start (login) page of application. I get an "Authorization request" popup. If I cancel the popup, then I get to a page that states "Connection is not secure" (it's https address). After I get passed that, I lose the part where Selenium program should populate username and password and it just stays on the login page.
My question, is there a way to start Selenium testing on an application so that it is already opened and prepared (ie logged in) in browser? I am not happy that username and password are hard-coded in Selenium code.
If that is not possible, how do I skip that authorization popup and problem with non-secure connection? How do I populate username and password in safest way?
Thanks!
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Selenium-java-3.0.1\\geckodriver.exe");
// I tried also with this code bellow in comment, but it did not work, it did not even get to login page
//WebDriverWait wait = new WebDriverWait(driver, 10);
//Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//alert.authenticateUsing(new UserAndPassword("cfadmin", "20lipim18"));
driver.get("https://Application_login_page.com");
driver.findElement(By.xpath(".//*[#id='login']")).click();
driver.findElement(By.xpath("[#id='login']")).sendKeys("Username");
}
if it's possible, is there a way to start Selenium testing on application that is already opened and prepared (logged) in browser?
Try using firefox profile. Since selenium open fresh instance of browser by default. You can use your own firefox frofile.
This is a code to implement a profile, which can be embedded in the selenium code.
ProfilesIni profile = new ProfilesIni();
// this will create an object for the Firefox profile
FirefoxProfile myprofile = profile.getProfile("default");
// this will Initialize the Firefox driver
WebDriver driver = new FirefoxDriver(myprofile)
It will also maintain the session means you are already login to the application in firefox(default profile). Then if you execute the script, you will see that you are already logged in to the application.
There is no way to open already authorised page, you have to have to pass username and password through selenium script.
You may use below code to do the authentication
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(username, password));

Selenium Webdriver Continuing After Site from get() Prompts for Cert

I'm writing a Java app in which I'm doing a simple driver.get(url) in which the url will prompt for a cert selection. I want to try and automate the cert selection process in Firefox 33 using the AutoIt jar, but I can't even get my Java program to continue after it executes this get, since the site is in an indefinite state of loading until a cert is selected, so the execution indefinitely stays on the get itself. Is there any way around this?
I found a similar question: Make Selenium Webdriver Stop Loading the page if the desired element is already loaded?
Ths solution given in the above link is to change the firefox settings in case the webpage is taking too long to load.
FirefoxBinary firefox = new FirefoxBinary(new File("/path/to/firefox.exe"));
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("network.http.connection-timeout", 10);
customProfile.setPreference("network.http.connection-retry-timeout", 10);
driver = new FirefoxDriver(firefox, customProfile);

WebElement is stale when navigating to facebook/twitter then back to original page in Sauce Labs Selenium using AndroidDriver

I have a selenium test failing on Sauce Labs due to a stale element. The test opens up a page, clicks on a few links within the site, click on a link that takes you to Facebook, then back and that's where things stop working. For some reason after coming back to the original page, the driver is no longer able to find "fresh" elements. This seem to be only happening when using Android on Sauce Labs. The code is below, please let me know if you have any suggestions for how to work around it (I don't care if it is perfect at this point, just want to get it working).
#Test
public void testFooter() throws Exception {
driver.get(baseUrl + "/");
// these links are all on my domain, work just fine
driver.findElement(linkText("About Us")).click();
driver.findElement(linkText("Press")).click();
driver.findElement(linkText("Careers")).click();
driver.findElement(linkText("FAQ")).click();
driver.findElement(linkText("Industry Resources")).click();
driver.findElement(linkText("Share")).click();
// going out to facebook works
driver.findElement(linkText("Facebook")).click();
driver.navigate().back();
// coming back to the page and clicking the twitter link fails due to
// org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
driver.findElement(linkText("Twitter")).click();
driver.navigate().back();
}

Getting the URL of the current page using Selenium WebDriver

I'm attempting to get the URL of the currently open page. I am using Selenium WebDriver and Java.
I am accessing the current URL via:
WebDriver driver = new WebDriver();
String url = driver.getCurrentUrl();
however, the URL does not appear to actually reflect where I currently am.
My current test case involves going to the NYT website and then clicking on the "Technology" link. However, url appears to always be http://www.nytimes.com/, regardless of the URL that is displayed in the address bar.
How do I actually access the value of the URL that's in the address bar so I can tell what page I'm actually on?
Put sleep. It will work. I have tried.
The reason is that the page wasn't loaded yet.
Check this question to know how to wait for load - Wait for page load in Selenium

Categories