Not able to click on link obtain using findElements method - selenium

this is just a simple code to obtain all links from a particular section of a web page; click on those links get the title and return back to the parent page and repeat process for next one. But after clicking on 2 links I am getting error "stale element reference: element is not attached to the page document" Please refer code and let me know what I am missing.
I am able to open links in a new tab and get the title of child pages. But I want to open a link in the same tab.
package test;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LinkTest {
public static void main (String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\1\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.qaclickacademy.com/practice.php");
WebElement driverFooter = driver.findElement(By.xpath("//div[#id='gf-BIG']"));
WebElement footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
List<WebElement> links = footerC1.findElements(By.tagName("a"));
for(int i=0;i<links.size();i++){
links.get(i).click();
System.out.println(driver.getTitle());
driver.navigate().back();
}
}
}
It should open all links and get the title and return to parent page for next iterator. Below is output and error,
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1550488224.725][WARNING]: Timed out connecting to Chrome, retrying...
Practice Page
REST API Tutorial
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.16299 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 6 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'DS-SGH631RNL8', ip: '10.118.59.16', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, timeouts={implicit=0, pageLoad=300000, script=30000}, hasTouchScreen=false, platform=XP, acceptSslCerts=false, goog:chromeOptions={debuggerAddress=localhost:53075}, acceptInsecureCerts=false, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=ignore, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1), userDataDir=C:\Users\RUSHIK~1.KAD\AppData\Local\Temp\scoped_dir11700_2428}, takesHeapSnapshot=true, pageLoadStrategy=normal, strictFileInteractability=false, databaseEnabled=false, handlesAlerts=true, version=72.0.3626.109, browserConnectionEnabled=false, proxy={}, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: e1a6bdd0b525d1c6e1d940a2ec156e51
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at test.LinkTest.main(LinkTest.java:23)

You are getting StaleElementReferenceException which comes when the element you are trying to access is no longer available in the dom or has become stale, to correct it you need to pick the elements again and then operate on it. You can modify your code like:
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\1\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.qaclickacademy.com/practice.php");
WebElement driverFooter = driver.findElement(By.xpath("//div[#id='gf-BIG']"));
WebElement footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
List<WebElement> links = footerC1.findElements(By.tagName("a"));
for(int i=0;i<links.size();i++){
driverFooter = driver.findElement(By.xpath("//div[#id='gf-BIG']"));
footerC1 = driverFooter.findElement(By.xpath("//table/tbody/tr/td/ul[1]"));
List<WebElement> newLinks = footerC1.findElements(By.tagName("a"));
newLinks.get(i).click();
System.out.println(driver.getTitle());
driver.navigate().back();
driver.navigate().refresh();
}

Related

How to locate webelement?

I am not able to select departure city on spice jet website to check flight status.
Steps followed:
login to http://www.spicejet.com
click on flight status link
select departure city as goa from drop down>>
=====
Code
package TestProject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestFirefox {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
//driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver",
"C://Selenium Jars//Chrome//chromedriver_2.31//chromedriver.exe ");
driver = new ChromeDriver();
driver.get("http://www.spicejet.com/?utm_source=Brand%20Campaign%20_&utm_medium=cpc_google&utm_term=O%26D&utm_campaign=Spicejet%20all%20route%20campaign&gclid=CjwKCAiAtorUBRBnEiwAfcp_Y7LCureobkUcdJZY7dqfgx0nlxyYPQeMp6w4MKkaxBEF8QzpovceDBoCXKMQAvD_BwE");
driver.findElement(By.xpath("//*[#id=\'buttons\']/div/div/ul/li[4]/a/span[2]")).click();
Thread.sleep(3000);
WebDriverWait wait =new WebDriverWait (driver, 15);
WebElement test = wait.until(ExpectedConditions.elementToBeClickable(By.id("FlifoSearchInputCompactFlightStatus_originStation")));
//WebElement test=driver.findElement(By.id("FlifoSearchInputCompactFlightStatus_originStation"));
test.click();
//test.sendKeys("GOA(GOI)");
Select s1=new Select(test);
s1.selectByVisibleText("Agartala (IXA)");
}
}
================
error
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 45746
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for element to be clickable: By.id: FlifoSearchInputCompactFlightStatus_originStation
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'NPF0JVX9B', ip: '10.254.101.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir11036_19151}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.84, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: fcc55c734bff34b0f8d58fec53bd9c02
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:261)
at TestProject.TestFirefox.main(TestFirefox.java:27)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"FlifoSearchInputCompactFlightStatus_originStation"}
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 286 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'NPF0JVX9B', ip: '10.254.101.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir11036_19151}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.84, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: fcc55c734bff34b0f8d58fec53bd9c02
*** Element info: {Using=id, value=FlifoSearchInputCompactFlightStatus_originStation}
at sun.reflect.GeneratedConstructorAccessor9.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:899)
at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:897)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:653)
at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
... 1 more
Click on flight status -> opens in iframe..so you have to switchTo iframe first before locating further webelements. Modify your code like this, see I've added a line before wait...
driver.switchTo().frame(1); //this line switchto flight-status iframe
The following code worked for me:
driver.get("http://www.spicejet.com/");
driver.findElement(By.cssSelector("a[title='Flights']>span:nth-child(2)")).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement dropdown = wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));
dropdown.sendKeys("Goa (GOI)");
Some notes on identifying elements:
Start locating the elements with the HTML code the element itself instead of starting from parent HTML. This will reduce a lot of noise in locator. There are various ways of identifying element: id, name, linkText, css locator, xpath.
Prefer CSS locator over Xpath.
Avoid absolute xpaths, always use relative xpaths. and try to minimize the relativity i.e., as said in first point, start with the element HTML itself.
For selecting from a dropdown you don't have to click on it. Removetest.click();. And try the following , it should work.
WebElement element = driver.findElement(By.id("FlifoSearchInputCompactFlightStatus_originStation")));
Select departureCity = new Select(element);
departureCity.selectByVisibleText("Goa (GOI)");
or you can use
departureCity.selectByValue("GOI");
If exception thrown, please try putting a Thread.sleep(2000) before the WebElement declaration or use the wait methods.

Unable to click the element. Error: no such element: Unable to locate element:

I am practicing on flipkart.com. i am unable to click the product after the search result. The xpath is correct. I tried using scroll function,visible, find by partial linktext, wait, sleep still i am unable to do. Please find the below code.
package Flipkart;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TC2
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath(".//*[#id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
}
}
Error:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 17575
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]"}
(Session info: chrome=58.0.3029.81)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 76 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'D90T0CQ1', ip: '192.168.162.83', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_102'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\SALUNK~1\AppData\Local\Temp\scoped_dir12552_16506}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.81, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a8c3432c9b1e8cce3365db9407fc310f
*** Element info: {Using=xpath, value=.//*[#id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Flipkart.TC2.main(TC2.java:15)
Here is the Answer to your Question:
I don't see any such errors/issues in your code. A few words about the solution:
Chrome browser sometimes doesn't opens maximized so you need to maximize the Chrome browser using ChromeOptions class.
If the browser is not maximized certain elements doesn't appers within the Viewport and and may throw NoSuchElementException
Add the argument to disable the infobars through ChromeOptions class.
Your xpath looks vulnerable to me, try to construct logical xpath for the elements on the HTML DOM.
Your own code works at my end with some simple tweaks which opens the Chrome
Browser, navigates to the URL https://www.flipkart.com/, searches for Bag, clicks on Skybags Pixel Plus 03 30 L Backpack and Skybags Pixel Plus 03 30 L Backpack (Grey) is opened in a new tab.
Here is the modified code block:
System.setProperty("webdriver.chrome.driver","C:\\your_directory\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath(".//*[#id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
Let me know if this Answers your Question.
I have tried the same lines of code which you have mentioned and things work for me. Maybe you should try again.
Please try to use small xpath:
Suggestion:
Replace Xpath: html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button with the different Xpath:
//form[#action="/search"]/div/div[2]/button
You should use relative path instead of absolute path because if there are any changes made in the path of the element then that XPath gets failed.
I got the same exception and added implicitly wait. It is working fine now. The code
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath("//*[#class='_2cLu-l']")).click();`
Final solution and I can't suggest more than this,
//openChromeBrowser();
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("//form[#action='/search']/div/div[2]/button")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Puma PUMA Zipper Backpack 26 L Laptop Backpack")).click();
1> Give some wait time and it works
2> Use LinkText for locators for any of the item shown.
And follow what #Monika Singhal has said for quick and efficient results in Automation.

Why I am not able to handle the password field using selenium web driver in new gmail login page?

I am new to selenium web driver, I am writing script to login in to the gmail below is the test script that I created.
public class gmailEmail {
public void login() {
ChromeDriver driver=new ChromeDriver();
driver.get("https://www.gmail.com");
driver.findElementById("Email").sendKeys("XXXXX#gmail.com");
driver.findElementByName("signIn").click();
driver.findElementById("Passwd").sendKeys("XXXX");
//driver.findElementByXPath("//*[#id='Passwd']").sendKeys("XXXXX");
driver.findElementByXPath("//*[#id='signIn']").click();
}
When I run this the error is triggering at "driver.findElementById("Passwd")" following error message is displaying
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Passwd"}
(Session info: chrome=49.0.2623.112)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 40 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'Rameshs-MacBook-Pro.local', ip: '192.168.0.105', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=/var/folders/vx/07vnt7mn2jx4fpm983_tq6gm0000gn/T/.org.chromium.Chromium.S1jNgs}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=49.0.2623.112, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: f5790b0589eb054c4e11283c80b82f0e
*** Element info: {Using=id, value=Passwd}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at MobilousLogin.gmailEmail.login(gmailEmail.java:15)
at MobilousLogin.Registration.main(Registration.java:10)
If I debug by inserting a breakpoint at "driver.findElementById("Passwd")" then the whole script is working fine.
Can some one please help me out in understanding what I am doing wrong here?
The error says it isn't able to identify the element that means by the time selenium has executed the command to enter password, the element wasn't loaded or not visible in the UI.
You may want to use ImplicitWait or ExplicitWaits. You can read about them here
You can try ExplicitWait to wait for the Passwd element:
public void login() {
ChromeDriver driver=new ChromeDriver();
driver.get("https://www.gmail.com");
driver.findElementById("Email").sendKeys("XXXXX#gmail.com");
driver.findElementByName("signIn").click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Passwd")).sendKeys(""What ever");
driver.findElementByXPath("//*[#id='signIn']").click();
}
try with below code:
public void login() {
ChromeDriver driver=new ChromeDriver();
driver.get("https://www.gmail.com");
driver.findElementById("Email").sendKeys("XXXXX#gmail.com");
driver.findElementByXPath("//*[#id='signIn']").click();
driver.findElementById("Passwd").sendKeys("XXXX");
driver.findElementByXPath("//*[#id='signIn']").click();
}
Add chrome driver capabilities
Try it once.
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
driver.manage().timeouts().impliciltyWait(30,TimeUnit.SECONDS);
driver.findElement(By.id("email-display").sendKeys("XXXXX#gmail.com");
driver.findElement(By.id("next"").click();
driver.findElement(By.id("Passwd")).sendKeys("xxxxxx");
driver.findElementByXPath("//*[#id='signIn']").click();

Selenium Webdriver - findElement atom issue

SELENIUM WEBDRIVER - IE
I'm trying to click on a link by using the following command:
driver.findElement(By.linkText("Previous orders")).click();
and I'm getting this error ... I could make it work some times but most of the time I get this error, it's intermitent, any ideas?
> Started InternetExplorerDriver server (32-bit)
2.50.0.0
Listening on port 25630
Only local connections are allowed
Exception in thread "main" org.openqa.selenium.WebDriverException: A JavaScript error was encountered executing the findElement atom. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 46 milliseconds
Build info: version: '2.50.1', revision: 'd7fc91b', time: '2016-01-29 19:04:49'
System info: host: 'ABC598-L1BCDL9', ip: 'xxx.xxx.xxx.xxx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, pageLoadStrategy=normal, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:25630/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 9865fcae-fa06-46b5-a621-d8390978c515
*** Element info: {Using=link text, value=Previous orders}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:428)
at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at Test.main(Test.java:27)
Main Code:
import java.util.concurrent.TimeUnit;
import javax.swing.JOptionPane;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.security.UserAndPassword;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) throws InterruptedException {
String cartId = JOptionPane.showInputDialog("Type the Cart ID:");
System.setProperty("webdriver.ie.driver",
"C:\\Users\\USER_ADMIN\\Downloads\\Selenium Webdriver\\IEDriverServer.exe");
DesiredCapabilities returnCapabilities = DesiredCapabilities.internetExplorer();
returnCapabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, false);
WebDriver driver = new InternetExplorerDriver(returnCapabilities);
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.get("https://k06proxy012.sby.com/procurement/buyondemand/common/enUS/index.html");
driver.findElement(By.linkText("Log in to Buy on demand (Bond)")).click();
login(driver);
// START
driver.findElement(By.linkText("Previous orders")).click();
driver.findElement(By.linkText("Search previous orders")).click();
new Select(driver.findElement(By.id("FIELD1"))).selectByVisibleText("Cart ID");
driver.findElement(By.id("VALUE1")).clear();
driver.findElement(By.id("VALUE1")).sendKeys(cartId);
driver.quit();
}
public static void login(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("user", "pass"));
}
}
Try to turn protected mode on IE
Click/tap on tools on the menu bar, and Internet Options
In the Security tab, select every zone and turn off protected mode
else can you write the main's code
I don't think the error is caused by erroneous code on your part. I tried upgrading to IEDriverServer 2.50 today and started receiving the same error. I returned to using 2.48 as a temporary workaround. Other newer versions may work between the two, this is just what I used previously.
Also see this recent issue on the project's Github page: https://github.com/SeleniumHQ/selenium/issues/1590
It seems that i found the reproducable case.
Check this issue https://github.com/SeleniumHQ/selenium/issues/2421
The point is in this javascript instruction:
var Number="рпоп";
Note, that if you write "number" (first small letter) - it will be working.
It seems the problem is in incorrect javascript code parsing.

i got the error while running javascript for IE11

below is my code :
package project1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class SearchInGoogle {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Packt Publishing");
searchBox.submit();
}
}
output:
Started InternetExplorerDriver server (32-bit)
2.29.1.0
Listening on port 17311
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with name == q (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 296 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'GNRSEZDT00035', ip: '10.210.6.68', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 00659ae5-b108-4fc4-b59c-390b1c15c879
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, elementScrollBehavior=0, javascriptEnabled=true, enablePersistentHover=true, ignoreZoomSetting=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=11, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, allowAsynchronousJavaScript=false, initialBrowserUrl=, handlesAlerts=true, nativeEvents=true, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:425)
at org.openqa.selenium.By$ByName.findElement(By.java:299)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at project1.SearchInGoogle.main(SearchInGoogle.java:13)
I m getting the above error. The above code is able to open the page in IE11 but it is not finding any elements. I tried all elements and I already set the IE11's security settings. So do u have any other solution for this problem?