Selenium unable to find an element

101 views
Skip to first unread message

שני ריבק

unread,
Mar 6, 2016, 9:51:16 PM3/6/16
to Selenium Users
Hello,
I am new to Selenium. I am unable to find specific elements in my code and I am getting this error:

org
.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"grade_radio05"}
Command duration or timeout: 30.94 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'Shany-PC', ip: '10.0.0.8', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_17'


This is the code I am running:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.gargoylesoftware.htmlunit.javascript.host.html.Image;

public class suff {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.suffolk.gov.uk/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSuff() throws Exception {
    driver.get(baseUrl + "");
   
    driver.findElement(By.cssSelector("#kampyleButtonContainer > div > img")).click();
    driver.findElement(By.id("grade_radio05")).click();

  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}


I would really appreciate the help in order to understand why it can't find "grade_radio05" whereas it can find "#kampyleButtonContainer > div > img".

Thanks in advance.

Pankaj Dubey

unread,
Mar 7, 2016, 1:53:28 AM3/7/16
to Selenium Users
You are getting the error on line 'driver.findElement(By.id("grade_radio05")).click();' as driver id not able to locate an element by its ID value "grade_radio05". This may be because of the ID value is dynamic in nature and keeps changing with every page load. You can try XPath selector and keep the value as "//*[starts-with(@ID,'grade_')]". You will have to figure out the partial value which constant with every page load and then apply the XPath methods accordingly. In case if you need the help on XPath or Css selector path methods you can watch this video:

Datta More

unread,
Mar 8, 2016, 6:09:46 AM3/8/16
to Selenium Users
https://www.suffolk.gov.uk/

In above page there is no object available with ID - grade_radio05
if objects are dynamic then you can follow steps mentioned by Pankaj in same thread.

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