Rammboss Rammboss - 1 month ago 7
HTML Question

How can I find a special element on a page?

I am getting started with selenium. I opened the url and entered my username and password: Tribal Wars 2. The Problem is to reach this button to click:

<a href="#" class="btn-orange btn-border small-icon" ng-click="selectCharacter(w.charId, w.id, w.loginDisabled)" tooltip="" tooltip-if="w.loginDisabled" tooltip-content="Diese Welt wurde geschlossen." ng-class="{'btn-grey': w.loginDisabled}" <span class="icon" <span class="icon-inner character" ng-class="{ 'character': playerId === w.ownerId, 'coop': playerId !== w.ownerId, 'locked': w.loginDisabled }"</span </span <span class="wrapper align-left" <span class="text character ng-binding"Rammboss</span <span class="text world ng-binding"Leeds Castle</span </span </a


Java Code:

driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a")).click();


Error Message:




Starting ChromeDriver (v2.9.248315) on port 20436 Exception in thread
"main" org.openqa.selenium.NoSuchElementException: no such element

(Session info: chrome=53.0.2785.143) (Driver info:
chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64) (WARNING:
The server did not provide any stacktrace information) Command
duration or timeout: 39 milliseconds For documentation on this error,
please visit: seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.43.1', revision:
'5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10
09:43:55' System info: host: 'AGBioSA04', ip: '10.4.1.81', os.name:
'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version:
'1.8.0_102' Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false,
chrome={userDataDir=C:\Users\Admin\AppData\Local\Temp\scoped_dir4760_1168},
takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true,
version=53.0.2785.143, platform=XP, browserConnectionEnabled=false,
nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true,
webStorageEnabled=true, browserName=chrome, takesScreenshot=true,
javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID:
5f87f399ba0889cb549ffc090b9dffbf 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: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.findElementByXPath(RemoteWebDriver.java:449)
at org.openqa.selenium.By$ByXPath.findElement(By.java:357) at
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
at com.mysite.selenium.HomePage.fillField(HomePage.java:46) at
com.mysite.selenium.HomePage.main(HomePage.java:58) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


This Game is written in Php and Javascript. Is it possible to do this with selenium?

Answer

You probably need to wait for your element after login.

 WebDriverWait wait = new WebDriverWait(driver, 10); // Wait for 10 seconds.
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a")));
 WebElement element = driver.findElement(By.xpath("//*[@id=\"wrapper\"]/div[2]/div[3]/div/div[ 1]/div[3]/div[ 1]/ul/li/a"));
 element.click();
  1. First line creates the instance of webdriver wait and uses "10" to wait for 10 seconds.
  2. The second line is telling selenium to wait until the element specified appears. Again, it will wait for 10 seconds.
  3. The 3rd line finds the element and calls it "element".
  4. The 4th line clicks on the element.

Hope that helps.

Edit: Just incase you have trouble with finding xpaths - In chrome:

  1. Right click on the element on the webpage and click inspect
  2. The HTML for the element should be highlighted
  3. Right click on the highlighted section
  4. Hover over 'copy'
  5. Click Copy xpath
Comments