I'm stuck in a very basic code

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

I'm stuck in a very basic code

Karthikeyan
Hi Tarun,

First of all, I really appreciate your thoughtfulness in giving back to the community and I'm sure its benefiting a lot of people like me.

Coming to the question: I'm very new to Selenium and doing the very first program in Selenium. I basically copied the program from your 2nd Selenium video and while trying to run, getting "Unable to locate element: {"method":"css selector","selector":".mv-search-results li:nth-of-type(1) a"}" error.

All the steps are exactly same to what you have done - so, really puzzled with the error. Kindly help me in clearing this error - sorry for asking a very basic question.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest {
        public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.wikipedia.org");
        driver.findElement(By.id("searchInput")).sendKeys("Selenium Software");
        driver.findElement(By.name("go")).click();
        driver.findElement(By.cssSelector(".mv-search-results li:nth-of-type(1) a")).click();
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

Karthikeyan
I get this error in IE as well as Firefox browsers. Havent tried Chrome yet.
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

softwaretestingforum
Administrator
Looks like html of wiki page is updated and correct element locator is -

".mw-search-results li:nth-of-type(1) a"


I suggest you to watch first selenium training video in which I explained on writing element locators. As you would have to modify element locator when there is a change in application HTML.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

bhavi
Hi Tarun,

I tried the same code with the correct element locator ".mw-search-results li:nth-of-type(1) a" but still gives me the same ST

My code: driver.findElement(By.cssSelector(".mw-search-results li:nth-of-type(1) a")).click();

Do you think the statement above should "wait" for results to load? Just guessing.

Error: Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".mw-search-results li:nth-of-type(1) a"}
Command duration or timeout: 43.18 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
System info: os.name: 'Windows Vista', os.arch: 'x86', os.version: '6.0', java.version: '1.7.0_21'
Session ID: 5ca2b98b-0a79-42db-9bd7-6a4a3a7398e7
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=20.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:191)
        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.findElementByCssSelector(RemoteWebDriver.java:396)
        at org.openqa.selenium.By$ByCssSelector.findElement(By.java:407)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
        at com.selenium.SeleniumWebDriverTest.main(SeleniumWebDriverTest.java:17)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"css selector","selector":".mw-search-results li:nth-of-type(1) a"}
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:32:38'
System info: os.name: 'Windows Vista', os.arch: 'x86', os.version: '6.0', java.version: '1.7.0_21'
Driver info: driver.version: unknown
        at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Dell/AppData/Local/Temp/anonymous7206569895609628411webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8444)
        at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/Dell/AppData/Local/Temp/anonymous7206569895609628411webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8453)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Dell/AppData/Local/Temp/anonymous7206569895609628411webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10456)
        at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Dell/AppData/Local/Temp/anonymous7206569895609628411webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10461)
        at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Dell/AppData/Local/Temp/anonymous7206569895609628411webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10401)
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

bhavi
Update: Interestingly, at first I was getting the above error but when I used the driver.close() and driver.quit(), it worked.
I commented the close and quit commands and it still worked. cleared the cache, restarted the browser, it still works. Still wondering what happened earlier.
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

softwaretestingforum
Administrator
I am just glad to know it worked :)
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: I'm stuck in a very basic code

bhavi
Update: I faced the same error again after a few repeated runs of the same code. Today I cleared the cache and form of the browser and it works just fine.Phew!