0

I have a web page which contains 2 links with the same classnames but with different div classes. The first one is invisible (it's in a dropdown menu) and the other one that I want is visible. So, I'm trying to locate the visible element.

His HTML :

 <div class="mainActionPanel">
   <a css="create"></a>
 </div>

The link has a dynamic ID. When I do a XPath search with the ID, I find rightly the element but it's deprecated because the button hasn't the same ID on each page.

I tried to locate the element with Selenium IDE, the following locator worked : css=div.mainActionPanel > a.create

The problem is with the locator that I showed above. When I try to find the element, I always have this exception :

NoSuchElementException : Element By.cssSelector: css=div.mainActionPanel > a.create (first) (LazyElement) is not present.

He doesn't find it. I tried several syntax like an example in the FluentLenium documentation ($("form > input[data-custom=selenium]") but it doesn't worked.

Moreover, the el(".create").click() is throwing an ElementNotVisibleException because he selects the dropdown link.

How can I find the right element ?

Drisnor
  • 13
  • 4

3 Answers3

0

May be this should help you..

 // this will provide you list of web elements based on class name
 List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));

Then you have to find out the required element by iterating through the list, like below.

  WebElement tempElement = null;
  for (WebElement element : webElements) {

            if (element.getAttribute("css").equals("create")) {
                tempElement = element;
            }
        }

Full code as follow,

  List<WebElement> webElements = driver.findElements(By.className("mainActionPanel"));

  WebElement tempElement = null;
  for (WebElement element : webElements) {

            if (element.getAttribute("css").equals("create")) {
                tempElement = element;
            }
  }


  //then you can perform that you want
  tempElement.click();
Tushar Deshpande
  • 448
  • 2
  • 9
  • 28
  • Doesn't works, the web elements list is null => NullPointerException – Drisnor May 29 '17 at 12:37
  • Have you tried this way, and before finding element give timer for atleast 4-5sec WebElement webElement = driver.findElement(By.cssName("create")); here don't give class name as ".create" – Tushar Deshpande May 29 '17 at 12:44
  • Seems that the if condition is never true so tempElement stays null and it throws the exception. – Drisnor May 29 '17 at 12:47
  • If condition will only be executed if and only if element is in the list, otherwise it will not executed. – Tushar Deshpande May 29 '17 at 12:48
  • There are 3 elements in the list, I ran it in debug mode and there's a problem with the if condition cuz it throws the NullPointer. – Drisnor May 29 '17 at 12:50
  • Try this instead of taking list, WebElement webElement = driver.findElement(By.cssName("create"); and dont give "create" as ".create" – Tushar Deshpande May 29 '17 at 12:54
  • ElementNotVisibleException, he tries to take the first one but it's invisible... like I said for el(".create"), moreover el("create") isn't working either :/ – Drisnor May 29 '17 at 13:00
0

Changed from css search to xpath using :

xpath = (//a[contains(@href, '#')])[5]

Because it's the 4th "a" element with an href attribute which contains the text "#". No more trouble.

Look at this : http://www.guru99.com/locators-in-selenium-ide.html

Drisnor
  • 13
  • 4
0

Try using below:

 WebElement elem = driver.findElement(By.cssSelector("div.mainActionPanel > a"));
 WebDriverWait wait= new WebDriverWait(driver, 20);

 wait.until(ExpectedConditions.visibilityOf(elem));

 elem.click();
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46