stackFan
stackFan

Reputation: 1608

Select element in webpage using selenium

I need to select these fields in a webpage using Selenium in FireFox. The HTML page is like Sample HTML Page, another sample page.

I used following code :

driver.findElement(By.id("list_item-2221889")).click();

I am passing the id's of the a-tag but that is not working. Does anyone know any better way to select those a-tags ?

The exception I am getting is as below:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: #list_item\-2221889
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'somePc', ip: '10.8.0.206', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\User\AppData\Local\Temp\rust_mozprofile.dYp9vdWr9LgT, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0, platformVersion=10.0, moz:processID=14524.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: f9bf7f0c-5f8c-4dce-a41c-0cf86c8f5420
*** Element info: {Using=id, value=list_item-2221889}
    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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)

Upvotes: 0

Views: 634

Answers (3)

santhosh kumar
santhosh kumar

Reputation: 2019

One simple idea is to use xpath and locate the element.

For example if you need to click the first element in the list,

driver.findElement(By.xPath("//ul[@class='list-group']/a[1]).click();

and for second element and so on

 driver.findElement(By.xPath("//ul[@class='list-group']/a[2]).click();

Use explicit wait if the table takes time to load. Try this and let me know. Thanks.

Upvotes: 0

jova
jova

Reputation: 31

var aTags = driver.findElements(By.xPath("//a[@class='list-group-item']"));

foreach(var aTag in aTags)
{
aTag.click();
}

You can click all a-tags with this method. If you want to only first a-tag click, use;

driver.findElement(By.xPath("//a[@class='list-group-item'][1]")).click();

(language is C#, sorry)

Upvotes: 0

Andersson
Andersson

Reputation: 52685

You may wait some time until element appears in DOM and became clickable:

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#list_item-2221889"))).click();

If you get TimeOut exception, check whether target link located inside an iframe. If so, you need to switch to iframe before handling link:

driver.switchTo().frame("iframeNameOrId");
driver.findElement(By.id("list_item-2221889")).click();

Upvotes: 1

Related Questions