3

enter image description here I have problems with finding this element: I tried these finders:

IWebElement profilUploadPhoto = driverGC.FindElement(By.XPath("//button[contains(.,'submit')]"));

IWebElement profilUploadPhoto = driverGC.FindElement(By.XPath("//button[contains(.,'btn btn-success')]"));

IWebElement profilUploadPhoto = driverGC.FindElement(By.ClassName("btn btn-success"));

IWebElement profilUploadPhoto = driverGC.FindElement(By.ClassName("glyphicon glyphicon-upload"));

I am usually finding elements by CSS selector or ID, but in this case, I can not do this.

3

can you please try the options below:

By Xpath:

button[@type='submit']
button[@class='btn btn-success']
button[@type='submit'][@class='btn btn-success']

By css selector:

button[type='submit']
button[class='btn btn-success']
button[type='submit'][class='btn btn-success']

Any one expression above should locate this element, please tell me if it still does not work.

  • If there are three or more elements with the same xpath (same class and type), how would you get the second one? I tried button[@type='submit'][@class='btn btn-success'][2] but that did not work. – Malcolm Salvador Jul 17 '18 at 0:30
  • the second element should use this index [1], not [2] @Malky.Kid – Yu Zhang Jul 17 '18 at 0:36
  • I have tried.. but encounter the error ERRORNoSuchElementError : Unable to locate the element.. but I can see four button elements that have the same type and class – Malcolm Salvador Jul 17 '18 at 1:07
  • @Malky.Kid, is your element in an iFrame? – Yu Zhang Jul 17 '18 at 1:10
  • possibly, when I right click and "view pagesource" I cannot see the elements I am looking for. Specifically, on inspect element I have these button[@type='button'][@class='btn btn-link2'][2]. Supposedly there are 3 elements that have these characteristics – Malcolm Salvador Jul 17 '18 at 1:12
2

If the class name is unique in the page then you can use the class name.

Keep in mind that the class name might not select the selector you need if there are multiple elements with the same class.

As an alternative check if you have other unique attributes for which you have a method or use a css/Xpath selector.

If you don't have any other attributes i suggest trying css selector and if you have no luck then Xpath.

  • 2
    Could you please edit your answer to include some examples drawn from the information the OP added after you posted? That would make your answer much better. – Kate Paulk Jul 26 '16 at 11:26
1

My approach would be something along the lines of:

WebElement yourElement
List<WebElement> Elements = driver.findElements(By.tagName("span"));
for(int Counter = 0; Counter < Elements.size(); Counter++){
     if(Elements.get(Counter).getAttribute("class").contains("glyphicon") &&
        Elements.get(Counter).getAttribute("aria-hidden").equals("true")){
          yourElement = Elements(Counter);
          break;
     }
}

It allows you to make sure that you've got the exact Element you need by adding statements to the boolean equation within the if argument. It might be overkill, but at the very least it's a safe approach.

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.