1

I will start by saying that I am new to Selenium and also to Java. I installed Selenium IDE, recorded some steps and modified this basic test and executed it as JUnit test within Eclipse. I was able to get something basic running against IE 11. However, I'm having a problem with Selenium clicking on an element. Another element that works, seems very similar so I don't understand what the problem is. Below is the InspectElement results from IE for the page I am working with in our app. InspectElement screenshot

The findElement for the "Add Substation" towards the bottom of my code below works fine. The findElement for the "Delete Substation" that follows doesn't work. I've tried multiple things and can't figure it out. Again, I am quite new to Java and Selenium, so it may be something obvious to many of you...just not to me.

Below is a portion of the code I am running: //#######################################################################

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


public void setUp() throws Exception {
  // Set path of IEDriverServer.exe.
  // Note : IEDriverServer.exe should be In C: drive.
  System.setProperty("webdriver.ie.driver", "C://IEDriverServer.exe");

  // Initialize InternetExplorerDriver Instance.
  WebDriver driver = new InternetExplorerDriver();

baseUrl = "https://10.10.10.10:8443/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@Test
public void testTry1SeleniumIDEJunitExport() throws Exception {
  System.setProperty("webdriver.ie.driver", "C://IEDriverServer.exe");
  WebDriver driver = new InternetExplorerDriver();

  //baseUrl = "https://10.10.10.10:8443/";
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  

  driver.findElement(By.id("continueToSite")).click();

  driver.get(baseUrl + "/ams/login");
  driver.findElement(By.name("j_username")).clear();
  driver.findElement(By.name("j_username")).sendKeys("xxxxxxx");
  driver.findElement(By.name("j_password")).clear();
  driver.findElement(By.name("j_password")).sendKeys("xxxxxxx");
  driver.findElement(By.id("loginSubmit")).click();
  Thread.sleep(1000);

  driver.findElement(By.linkText("Add Substation")).click();

...

Thread.sleep(5000);
driver.get(baseUrl + "/ams/substations_circuits");
driver.findElement(By.linkText("Delete Substation")).click();

//#######################################################################

Here is the error I receive:

org.openqa.selenium.NoSuchElementException: Unable to find element with link text == Delete Substation (WARNING: The server did not provide any stacktrace information)

I Googled and tried multiple solutions, but couldn't fix the problem. I did find some code to print out the table elements and the Delete is returned as part of the list, so I don't understand why I can't find the Delete and click on it. Here's the debug code I added:

//Code to loop through table and print contents (WORKING)
Thread.sleep(15000);
WebElement table_element = driver.findElement(By.id("subTable"));
List<WebElement> rows = table_element.findElements(By.xpath("id('subTable')/tbody/tr"));
java.util.Iterator<WebElement> i = rows.iterator();
while(i.hasNext()) {
    WebElement row = i.next();
    System.out.println(row.getText());
}

Here are the results of debug code being run:

Add Substation

mmm Providence RI Edit RTU Edit Hands On Disabled Settings Delete

I should also note that I recorded this in Firefox with Selenium IDE to generate most of the code.

The only thing that comes to mind is that there are only two rows in the subTable, Add Substation is the first and only element in the first row, Delete Substation is the last of many elements in the second row. I think this may be part of the problem, but I'm not sure how to get around it.

Thanks in advance for any suggestions.

3

"Delete Substation" is title, not link text.

You should add ID or name attribute to your anchor (element <a>), and locate it by that, much more reliable. If you cannot add it yourself, ask/plead/beg your developers to add it for you.

Locating elements by ID/name/CSS is much more reliable - XPath is last resort on websites of third parties where you have no leverage. You can see how many QAs are struggling with XPath, because QA got outsourced and they have no control, have to work twice as hard to get half the reliability.

If you cannot do id/name locate it by CSS class "deleteSubstation".

Also, next time try to paste source text and not the image.

Another advice: Avoid implicit waits, they WILL bite you. Google "selenium implicit wait" to learn why.

  • Thank you for the quick answer. When you say I should add ID or name attribute, I'm QA, so that would really be up to our application developer I would think, correct? If I am understanding you correctly. After looking at your comment, I noticed I left off a part of the code to show Delete Substation has link text, I just didn't expand the title line. When I did expand, I noticed the link text is just "Delete", not "Delete Substation", so my code was wrong. I fixed it and it worked. Thank you. I should talk to the developers about being consistent with link naming I guess. – Marc Aug 23 '16 at 20:08
  • Yup, avoid XPath. I usually do not waste time answering if the question is related to XPath - 99% it is the wrong approach. – Peter M. Aug 23 '16 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.