selenium test throws org.openqa.selenium.NoSuchElementException - java

I have written a selenium test for yahoo website. In this test I am testing Yahoo News. But this code is throwing an Exception. So please solve the problem?
I am using Eclipse IDE, and Firefox as browser.
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Tests {
WebDriver driver;
Wait<WebDriver> wait;
boolean result;
Tests() {
driver = new FirefoxDriver();
wait = new WebDriverWait(driver, 30);
driver.get("http://www.yahoo.com/");
}
public static void main(String arg[]) {
new Tests().news();
}
public boolean news() {
try {
System.out.print("Testing News... ");
driver.findElement(By.linkText("https://www.yahoo.com/news/")).click();
driver.findElement(By.partialLinkText("/news/world/")).click();
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
return webDriver.findElement(By.id("th-title")) != null;
}
});
return driver.findElement(By.id("th-title")).getText().contains("World");
}
catch(Exception exp) {
exp.printStackTrace();
return false;
}
}
}
Exception:
Testing News... org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
Command duration or timeout: 21.55 seconds
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 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
*** Element info: {Using=link text, value=https://www.yahoo.com/news/}
Session ID: 7576b452-dcb2-448f-844c-6c8b499561f1
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=46.0.1}]
FAILED
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 Tests.news(Tests.java:72)
at Main.main(Main.java:14)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"https://www.yahoo.com/news/"}
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 16:57:40'
System info: host: 'Jahanzeb', ip: '10.99.14.207', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_79'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:10779)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/JAHANZ~1/AppData/Local/Temp/anonymous2826618494991255784webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)

Please examine Yahoo page.
Please press Ctrl-F and try to find a text: "https://www.yahoo.com/news/">
There is no such a text there.
But if you look into a html source of this page, you will find this A tag with href attriibutte = ""https://www.yahoo.com/news/":
<a class="C(#fff) Td(n) Td(u):h" href="https://www.yahoo.com/news/"
data-reactid=".pgwo63s1xy.$tgtm-UH-0-Header.1.0.0:$news.0">News</a>
But a "link text" of this link is not "https://www.yahoo.com/news/", but "News" . The By#linkText method is looking for the link text (in this case "News"), not for href attributte.
You need to replace this command:
driver.findElement(By.linkText("https://www.yahoo.com/news/")).click();
with this one:
driver.findElement(By.linkText("News")).click();

Related

Not able to setup Selenium 3 on Eclipse Oxygen

Code Snippet -
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxDriverLogLevel;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumClientClass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","D:\\YoutubeVideos\\Selenium\\geckodriver.exe");
/* DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setCapability("marionette", true); */
try {
FirefoxOptions opts = new FirefoxOptions().setLogLevel(FirefoxDriverLogLevel.TRACE);
WebDriver driver = new FirefoxDriver(opts);
driver.get("http://www.google.com");
Thread.sleep(10);
System.out.println("Application title is ============>>>>>>> "+driver.getTitle());
driver.quit();
} catch(Exception e) {
System.out.println(e);
}
}
}
Error I am getting:
org.openqa.selenium.WebDriverException:
java.net.ConnectException: Failed to connect to
localhost/127.0.0.1:38558 Build info: version: '3.12.0', revision:
'7c6e0b3', time: '2018-05-08T15:15:03.216Z' System info: host:
'MPL-CJ08HM2', ip: '192.168.56.1', os.name: 'Windows 10', os.arch:
'amd64', os.version: '10.0', java.version: '9.0.4' Driver info:
driver.version: FirefoxDriver
I got this issue resolved by having latest version of geckodriver.exe (v0.20.1) as I was earlier using older version of geckodriver(v0.9.0)...

SeleniumError : org.openqa.selenium.SessionNotCreatedException

On doing a driver.close();driver.quit(); during the execution of java code, the following error is thrown:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Tried to run command without establishing a connection
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'
System info: host: 'ADMIN-PC', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\admin\AppData\Local\Temp\rust_mozprofile.ENTBvl2aDbSs, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0.2, platformVersion=10.0, moz:processID=5004, browserName=firefox, javascriptEnabled=true, platformName=XP}]
Session ID: 82e7dabd-c178-4d90-a3f8-84dc3f6ff14f
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:643)
at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:482)
at yahoo.main(yahoo.java:34)
Sharing code that throws the above exception :
//package basicSeleniumScripts;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class yahoo {
public static void main(String[] args) {
String Firefoxdriverpath = "C:\\Marionette\\geckodriver_1.exe";
WebDriver driver;
System.setProperty("webdriver.gecko.driver",Firefoxdriverpath);
//create a new instance of Firefox driver
driver = new FirefoxDriver();
//Open the page we want to open
driver.get("http://www.yahoo.com");
//Defining expected title
String expectedTitle = "Yahoo";
//Getting the actual title
String actualTitle = null;
actualTitle = driver.getTitle();
//Validating the TestCase
if (actualTitle.contentEquals(expectedTitle))
{
System.out.println("Test Passed");
}
else
{
System.out.println("Test Failed!!!");
}
driver.close();
driver.quit();
}
}
Update the gecko driver version to v0.19.0 as you are using 3.6.0 jars of selenium.
Also use quit method only

Issue in handling JS popup throwing an exception:org.openqa.selenium.UnhandledAlertException: Modal dialog present

I am new to selenium web-driver. I am facing a issue 'handling a javascript popup' I click on a button and after clicking that button a popup comes it has a ok button.when I recorded in selenium ide, it shows a (COMMAND) assertAlert and (TARGET) *A copy of your e-Quote #514106617764 has been generated successfully and sent to your e-mail id.\nPlease continue with your online purchase process. in table(recorded script).so i wrritten following code to handle this popup. i am trying on getQoute page of igaruntee of https://buyonline.aegonreligare.com/cordys/buyonlineesales/startiGuarantee.htm?key=websitedirect#
try{
Actions aa=new Actions(dd);
aa.sendKeys("ENTER");
}
catch(Exception e)
{System.out.println(e)}
try
{
dd.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("check1")));
WebElement a=dd.findElement(By.id("check1"));
if(a != null)
{
status = 1;
System.out.println("iGurantee_Premium Qoute Page " + status);
}
else
{
status = 0;
System.out.println("iGurantee_Premium Qoute Page " + status);
}
Monitoring_FrameWork.SaveResult(tstartTime,"iGurantee_Premium QoutePage ", status,120);
}
catch(Exception e)
{
status = 0;
System.out.println(e);
System.out.println("iGurantee_Premium Qoute Page " + status);
}
but I am getting an exception as:
org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: db7cdba6-b905-49e0-9947-947d18761929
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
iGurantee_Premium Qoute Page 0
Oct 29, 2014 3:46:38 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id: check1)
org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: db7cdba6-b905-49e0-9947-947d18761929
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
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:187)
at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:168)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:302)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:331)
at org.openqa.selenium.By$ById.findElement(By.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:294)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:523)
at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:521)
at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:204)
at aegonreligare.test.main(test.java:224)
Please tell me how to sovle this.
I even tried using
Alert a=driver.switchTo.alert;
a.accept(); or a.getText();
but it throws a exception :There is no alert...
You need to switch your window, set focus on the pop up and accept or deny.
var alert = Driver.SwitchTo().Alert();
alert.Accept();
written in c#

Getting NoSuchElementException on webelement (Same webelement is getting found but sometimes i got the Exception)

I have created this test script to test a website. The problem is that sometime i am able to click edit button but sometime i am getting NoElementFoundException for the same button. I searched for the same type of problem on the forum and did the code changes as per recommended but non of them worked for me. In the attached picture i circled the button in red colour for which i am getting the exception.
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class MonsterUpdateNewProfile {
static WebDriver window=new FirefoxDriver();
static WebElement wb;
static String[] data;
#Test(priority=2)
static public boolean isAlertExist(){
boolean alert=false;
try{
Alert alt= window.switchTo().alert();
alert=true;
System.out.println("Text from alert is "+alt.getText());
alt.dismiss();
}catch(Exception e){
System.out.println("No Alert");
e.printStackTrace();
}
return alert;
}
#Test
static void dropdown() throws InterruptedException {
wb=window.findElement(By.xpath("//div[#class='ns_menu_item_wrap ns_lt active']/a"));
Actions mouse=new Actions(window);
mouse.moveToElement(wb).perform();
Thread.sleep(4000);
Select select=new Select(wb);
select.selectByIndex(1);
}//dropdown
#Test
public static void monster() throws InterruptedException {
try{
//connecting to web site by sending address url
window.get("https://my.monsterindia.com/login.html?src=http://my.monsterindia.com/my_monster.html&rand=5257");
window.manage().window().maximize();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//finding username by using xpath
wb=window.findElement(By.id("BodyContent:txtUsername"));
//sending username
wb.sendKeys("sshantanunandan#gmail.com");
//finding and sending password
window.findElement(By.id("BodyContent_txtPassword")).sendKeys("Ferrari2738");
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//finding and clicking on login button
window.findElement(By.name("submit")).click();
//grtting the title of current page using getTitle() method
System.out.println(window.getTitle());
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
window.findElement(By.name("cancel")).click();
window.findElement(By.className("flyout_close")).click();
//locating and clicking on update button which will open a new window
window.findElement(By.xpath("//img[#src='http://media.monsterindia.com/monster_2012/btn_update2.gif']")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
//capturing the title of oldwindow or the window which we go after loging
//using getWindowHandle() method
String oldwindow=window.getWindowHandle();
//by using for statement getting the title of new pop upwindow
//here we can use if or while statement as well using getWindowHandles() method
for(String newwindow : window.getWindowHandles()){
//swithching to the new pop up using window.switchTo().window(passing newwindow as argument)
window.switchTo().window(newwindow);}
//getting title of new window using getTitle() method
System.out.println("NewWindow Title"+ window.getTitle());
window.findElement(By.xpath(".//*[#id='edit_resume_section3_open' and not(#disabled)]")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
System.out.println(window.findElement(By.xpath("//input[#name='title']")).getAttribute("value"));
window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
window.findElement(By.id("update")).click();
window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
window.close();
//switching to oldwindow or main window
//using switchTo().window(passing oldwindow url to it)
//url which we captured in String oldwindow=window.getWindowHandle() method
window.switchTo().window(oldwindow);
//getting the output of old window
System.out.println("OldWindow Title" + window.getTitle());
//isAlertExist();
//dropdown();
}//try
catch(NoSuchElementException e){
window.findElement(By.className("flyout_close")).click();
window.findElement(By.xpath("//a[#href='http://my.monsterindia.com/view_resume.html?resid=51706134&mode=edit']/img")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
window.findElement(By.id("edit_resume_section3_open")).click();
window.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
System.out.println(window.findElement(By.xpath("//input[#name='title']")).getAttribute("value"));
window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
window.findElement(By.id("edit_resume_section3_close_other")).click();
window.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//isAlertExist();
dropdown();
}//catch
catch(Exception e){
e.printStackTrace();
System.out.println("Got Acception");
window.close();
}//catch
finally{
//dropdown();
System.out.println("Finally Closing");
window.close();
window.quit();
}//finally
}//main
}//class
Exception got for this code-
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[#name='title']"}
Command duration or timeout: 40.06 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_45'
Session ID: f4dce968-0425-4ba1-8a62-4ebdff91edc8
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
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:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
at org.openqa.selenium.By$ByXPath.findElement(By.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at MonsterUpdateNewProfile.monster(MonsterUpdateNewProfile.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"//input[#name='title']"}
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:12:12'
System info: host: 'omega-d52b83806', ip: '10.0.0.5', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_45'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous591356915052066864webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:8860)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/DOCUME~1/OMEGA/LOCALS~1/Temp/anonymous591356915052066864webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:396)
Posting an answer as I couldn't work out how to place code in comments...
The error says it can't find the element, so either the XPath has an issue, or the element doesn't exist. You could try making the element your wait condition.
....
WebDriverWait wait = new WebDriverWait(webDriver, 30)
window.findElement(By.id("...")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#name='title']")))
webDriver.findElement(By.xpath("//input[#name='title']"))
....
Please check your XPath ".//*[#id='edit_resume_section3_open' and not(#disabled)]" correctness.
If it is correct then try to wait for this element and if it is available then only click on it.
You can use below code to wait for element:
(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(".//*[#id='edit_resume_section3_open' and not(#disabled)]"));
}
});

Selenium Webdriver unable to locate element from webtable

I am trying to access a webtable which is quite static and get the values from each row.
I am getting an error, the code is:
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TS1_TC2 {
WebDriver driver=null;
String btn1="html/body/form/div[1]/center/table/tbody/tr/td[1]/input";
String table="html/body/form/table/tbody/tr[2]/td/div/center/table";
#BeforeTest
public void browserCheck()
{
driver=new FirefoxDriver();
driver.get("http://demo.borland.com/gmopost/");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void TS1_TC2()
{
driver.findElement(By.xpath(btn1)).click();
//html/body/form/table/tbody/tr[2]/td/div/center/table/tbody
//html/body/form/table/tbody/tr[2]/td/div/center/table
WebElement table=driver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table"));
List<WebElement> rows=table.findElements(By.tagName("tr"));
//WebDriverWait wait = new WebDriverWait(driver, 100, 15);
System.out.println(rows.size());
for(int rownum=2;rownum<rows.size();rownum++)
{
List<WebElement> cols=rows.get(rownum).findElements(By.tagName("td"));
System.out.println(cols.size());
for(int colnum=1;colnum<cols.size();colnum++)
{
//System.out.println(cols.get(colnum).getText());
//wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")));
System.out.println("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]");
System.out.println(cols.get(colnum).findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText());
System.out.println("--");
}
System.out.println();
}
}
}
The Exception thrown is as follows:
FAILED: TS1_TC2
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[1]"}
Command duration or timeout: 20.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:55:11'
System info: host: 'Vihaan-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 15f51aea-04cd-4ea4-bdae-0bdd47312bd7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
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:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:171)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:244)
at org.openqa.selenium.By$ByXPath.findElement(By.java:357)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:167)
at TS1_TC2.TS1_TC2(TS1_TC2.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[1]"}
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:55:11'
System info: host: 'Vihaan-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Vihaan/AppData/Local/Temp/anonymous7820392430389922124webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:8904)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Vihaan/AppData/Local/Temp/anonymous7820392430389922124webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:396)
Kindly help , because I see the the xpath from firepath is exactly the same as the xpath fetched in the for loop.
Thanks
rajee
try using
driver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText()
instead of
cols.get(colnum).findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText()
It should work.
But I dont understand why you used the above line. The following code achieves the same thing.
WebDriver driver = null;
#BeforeTest
public void browserCheck() {
driver = new FirefoxDriver();
driver.get("http://demo.borland.com/gmopost/");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void TS1_TC2() {
driver.findElement(By.name("bSubmit")).click();
WebElement table = driver
.findElement(By
.xpath("//form[#name='Items']/table/tbody/tr[2]/td/div/center/table"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (int rownum = 2; rownum < rows.size(); rownum++) {
List<WebElement> cols = rows.get(rownum).findElements(
By.tagName("td"));
for (int colnum = 1; colnum < cols.size(); colnum++) {
System.out.println(cols.get(colnum).getText());
}
System.out.println("\n");
}
}
Let me know if this helps you.

Categories

Resources