Unable to find the element and search in webdriver | Selenium Forum
M
Posted on 22/01/2017
Here is the sample code i have written to search a friend in fb page from the search box entering the search keyword to search but unable to run it as it gives error.
Please help me what i need to add anything else in the code to get the correct output.

mport java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class testgmail {

public static void main(String[] args) throws InterruptedException {

FirefoxDriver driver =new FirefoxDriver();
driver.get("https://www.facebook.com");
driver.findElementsByXPath(".//*[@id='login_form']/table/tbody/tr[1]/td[1]");
driver.findElementByXPath(".//*[@id='email']").sendKeys("xxxxx");
driver.findElementByXPath(".//*[@id='pass']").sendKeys("yyyyy");
driver.findElementByXPath(".//*[@id='loginbutton']").click();
driver.findElementByXPath(".//*[@id='u_0_3']/div[1]/div[1]/div/a/span").click();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

driver.findElementByXPath("//span[@class='_gs6']").click();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.findElementByXPath("//span[@class='inputtext']").sendKeys("xyz");
//driver.findElementByXPath("//span[@class='accessible_elem']").sendKeys("xyz");

driver.findElementByXPath(".//*[@id='u_jsonp_10_1g']/span/button").click();

M
Replied on 22/01/2017

you haven't told me the error

so, i would suggest use this
[code:21y2uxqi]
driver.findElementByXPath("//span[@class='inputtext']").sendKeys("xyz"+Keys.RETURN);[/code:21y2uxqi]



comment out this last line.
[code:21y2uxqi]
driver.findElementByXPath(".//*[@id='u_jsonp_10_1g']/span/button").click();[/code:21y2uxqi]


M
Replied on 23/01/2017

[quote="qtpselenium.supp0rt@gmail.com":194suzqf]you haven't told me the error

so, i would suggest use this
[code:194suzqf]
driver.findElementByXPath("//span[@class='inputtext']").sendKeys("xyz"+Keys.RETURN);[/code:194suzqf]



comment out this last line.
[code:194suzqf]
driver.findElementByXPath(".//*[@id='u_jsonp_10_1g']/span/button").click();[/code:194suzqf][/quote:194suzqf]


M
Replied on 23/01/2017

This is the error message i am getting after i have written the code as mentioned.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[@class='inputtext']"}
Command duration or timeout: 20.09 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: 'AJIT', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 57a63585-5d6d-4009-9db3-2a8abaac880d
*** Element info: {Using=xpath, value=//span[@class='inputtext']}
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.findElementByXPath(RemoteWebDriver.java:500)
at testgmail.main(testgmail.java:21)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[@class='inputtext']"}
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: 'AJIT', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Ajit/AppData/Local/Temp/anonymous4901989828757727490webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Ajit/AppData/Local/Temp/anonymous4901989828757727490webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:625)


M
Replied on 25/01/2017

this xpath is wrong

[quote:22r64rit]//span[@class='inputtext'][/quote:22r64rit]

try to find another one.