unable to locate an element using xpath

253 views
Skip to first unread message

poornima...@gmail.com

unread,
Mar 1, 2017, 3:55:43 PM3/1/17
to Selenium Users
Hello all,

I am a beginner in selenium.Trying to run the below code in selenium 3.0.1 and firefox 51.0.1(64 bit)

I Am not able to locate the "My Account" link and also receiving a 

// TODO Auto-generated method stub
//Objects and variable declaration
//if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code
System.setProperty("webdriver.gecko.driver", "C:/Users/syama/Desktop/Selenium/geckodriver-v0.13.0-win64/geckodriver.exe");

//Now you can Initialize marionette driver to launch firefox
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
//@SuppressWarnings("deprecation")
WebDriver driver = new MarionetteDriver(capabilities); 

//WebDriver driver = new FirefoxDriver();
String appUrl = "http://phptravels.com/demo/";
//Launch the firefox browser
driver.get(appUrl);
//maximize the browser
driver.manage().window().maximize();
//functionality 
driver.findElement(By.xpath("//*[@class='btn btn-primary btn-lg btn-block']//*[text()='http://www.phptravels.net']")).click();
System.out.println("Clicked on the homepage-front end button in the home page");
//wait
driver.manage().timeouts();
//select value from drop down
    Select dropList = new Select(driver.findElement(By.xpath("//a[contains(.,'My Account')]']")));
dropList.selectByVisibleText("  Sign Up");

Console:

1488399652115 geckodriver INFO Listening on 127.0.0.1:27695
Mar 01, 2017 3:20:52 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1488399652449 mozprofile::profile INFO Using profile path C:\Users\syama\AppData\Local\Temp\rust_mozprofile.pJeIYHUa7B3i
1488399652480 geckodriver::marionette INFO Starting browser C:\Program Files\Mozilla Firefox\firefox.exe
1488399652542 geckodriver::marionette INFO Connecting to Marionette on localhost:61873
1488399655546 Marionette INFO Listening on port 61873
Mar 01, 2017 3:21:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Clicked on the homepage-front end button in the home page
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'DESKTOP-A7VC6HV', ip: '192.168.1.90', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: driver.version: RemoteWebDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, appBuildId=20170125094131, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=728, browserVersion=51.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}]
Session ID: a40cb531-8cd4-4fa2-adee-03a550df6130
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:622)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at Signup.main(Signup.java:44)
Caused by: java.lang.IllegalArgumentException: expected one element but was: <org.openqa.selenium.remote.ErrorCodes$KnownError@c4437c4, org.openqa.selenium.remote.ErrorCodes$KnownError@6193b845>
at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:322)
at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:284)
at org.openqa.selenium.remote.ErrorCodes.toStatus(ErrorCodes.java:138)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:92)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
... 5 more
Please help me to resolve this error and run my script successfully

Jegadeesh mani

unread,
Mar 1, 2017, 4:54:49 PM3/1/17
to seleniu...@googlegroups.com
Select dropList = new Select(driver.findElement(By.xpath("//a[contains(locatorsid,'My Account')]']")));

Replace this line of code Instead of old one.

Because you're getting unhanded execution.

Note: try to entire locator name I didn't mention above line of code.

Please let me know so you have any concern on this.


Thanks
Jegadeesh

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/916acd96-3cce-4a09-904e-ea03b3c38cfe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xiang Dong

unread,
Mar 1, 2017, 7:40:06 PM3/1/17
to seleniu...@googlegroups.com

after you click the first link, a new window will be opened, to look at element on the new opened window, you need to switch your webdriver to it firstly, then, you can use By by = By.linkText("My Account") to locate the "My Account" element


Note: "My Account" is not a <select> element, you can't treat it as <select> box, the way is click this element, then, sub-menu will be displayed and then locate "Sign Up" and click it.


--david


From: seleniu...@googlegroups.com <seleniu...@googlegroups.com> on behalf of Jegadeesh mani <jegade...@gmail.com>
Sent: Thursday, March 2, 2017 5:54 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] unable to locate an element using xpath
 
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CADrrHisHx0uKdz6nDQfOpTzvJFFMiwDwX1px73OQ8EEHdeG2ng%40mail.gmail.com.

pelto...@yahoo.com

unread,
Mar 1, 2017, 8:20:30 PM3/1/17
to seleniu...@googlegroups.com

--------------------------------------------
On Thu, 3/2/17, Xiang Dong <chiefw...@hotmail.com> wrote:

Subject: Re: [selenium-users] unable to locate an element using xpath
To: "seleniu...@googlegroups.com" <seleniu...@googlegroups.com>
Date: Thursday, March 2, 2017, 2:39 AM



after you click the first link, a new window will be
opened, to look at element on the new opened window, you
need to switch your webdriver to it firstly, then, you can
use By by = By.linkText("My Account") to
locate the "My Account" element



Note: "My Account" is not a
<select> element, you can't treat it as
<select> box, the way is click this element, then,
sub-menu will be displayed and then locate "Sign
Up" and click it.



--david


raisesAccessibilityExceptions= false, marionette=true,
appBuildId=20170125094131, version=, platform=XP, proxy={},
command_id=1, specificationLevel=0, acceptSslCerts=false,
processId=728, browserVersion=51.0.1, platformVersion=10.0,
XULappId={ec8030f7-c20a-464f- 9b0e-13a3a9e97384},
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/SG2PR0302MB254272BB4720BA401D399F9190280%40SG2PR0302MB2542.apcprd03.prod.outlook.com.

For more options, visit https://groups.google.com/d/optout.
1proprietate funciara nu s-a specializat in exportul de cereale. Cele 12 zile de claca statuate inca din vremea lui Constantin Mavrocordat au fost mai mereu inlocuite cu un volum de munca reciproc convenabil prin buna invoiala de multe ori convertit in bani in Moldova de la 1 leu la 2 lei in tara Romaneasca cresterea a urcat pana la 1 leu spre 1818 .

eddieB

unread,
Mar 1, 2017, 11:28:38 PM3/1/17
to Selenium Users
Select dropList = new Select(driver.findElement(By.xpath("//a[contains(.,'My Account')]']")));

you have an extra single quote marked in red, remove that and try 

anemuday

unread,
Mar 2, 2017, 1:19:35 AM3/2/17
to Selenium Users
Poornima,

Below is the working code:

 //Objects and variable declaration
 
//if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code

 
System.setProperty("webdriver.gecko.driver", "D:/Cigniti/Softwares/geckodriver.exe");
 
/* DesiredCapabilities capabilities = DesiredCapabilities.firefox();
 capabilities.setCapability("marionette", true);
 WebDriver driver = new MarionetteDriver(capabilities);*/

 
WebDriver driver = new FirefoxDriver();
 
String appUrl = "http://phptravels.net/";

 
 
//Launch the firefox browser
 
 driver
.get(appUrl);
 
 
//maximize the browser
 driver
.manage().window().maximize();

 driver
.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
//functionality
 
 driver
.findElement(By.xpath("//a[contains(.,'My Account')]")).click();
 
System.out.println("Clicked on the My Account in the home page");
 
 driver
.findElement(By.xpath("//a[contains(.,'Sign Up')]")).click();

One point to mention here is, you cannot use 'Select' for all element. Here 'My Account' is <ul> and items are <li>. You should not use Select in this case.

There is a separate html tag called ,<select> and its list items are <option>. Then only you should use Select in your code.

I hope the above helps.

Thanks,
Uday
Reply all
Reply to author
Forward
0 new messages