-1

i am on https://www.mister-auto.com i would like to select the first item of the menu having the attribut data-selenium='link_front_generic'

So i tried the following code:

 driver.findElement(By.xpath("(.//*[@data-selenium='link_front_generic'])[1]")).click();

However, the item is not found

i got the exception: org.openqa.selenium.ElementNotVisibleException: element not visible

dtjmsy
  • 2,664
  • 9
  • 42
  • 62
  • Take a look on that: https://stackoverflow.com/questions/1006283/xpath-select-first-element-with-a-specific-attribute – Robert Oct 03 '18 at 13:13
  • 1
    What is the actual exception you get, can you paste it in your question please? From what I can see it's an element that's in a doormat menu so isn't always visible on the page, which may be causing the issue, It depends if it's an element not visible exception or a no such element exception – Jsmith2800 Oct 03 '18 at 13:25
  • I get the exception: org.openqa.selenium.ElementNotVisibleException: element not visible, as for me it' s always visible isn' t – dtjmsy Oct 03 '18 at 13:30
  • @Robert, i think that i tried already that: (.//*[@data-selenium='link_front_generic'])[1] but no success – dtjmsy Oct 03 '18 at 13:35
  • Edit the exception message into the question. The `.//` isn't needed here. It's only used when you want to start with an existing element and search down from there. Since you are using `driver.findElement()`, it does nothing. – JeffC Oct 03 '18 at 17:56

1 Answers1

0

Element is not visible, because is inside menu wrapper which is not triggered.

You can try with the following code.

Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[text()='Pièces auto']"));
actions.moveToElement(menu);

WebElement subMenu = driver.findElement(By.xpath("//a[@title='Plaquette de frein']"));
actions.moveToElement(subMenu);
actions.click().build().perform();
Infern0
  • 2,565
  • 1
  • 8
  • 21
  • hi @Infern0, what i am trying is to use my custom attribut data-selenium, as it doesn' t relate to any DOM change in the future, the other would work, but it relates on the texts, dom etc, which required in the future more maintenance work – dtjmsy Oct 03 '18 at 13:44
  • @dtjmsy just change the xpath, if you wish to use custom attributes. I gave you an example solutions, adapt it to your needs. – Infern0 Oct 03 '18 at 13:48
  • i used the code i mentioned in my post: driver.findElement(By.xpath("(.//*[@data-selenium='link_front_generic'])[1]")).click(); it works already with other of my other custom attributs except the one that i posted – dtjmsy Oct 03 '18 at 13:50
  • @dtjmsy as you described above, the error is 'ElementNotVisibleException' this is due menu wrapper is not triggered a.k.a expanded so the element becomes visible. In this manner i gave you an example how to hover over the menu and click on the element. If you wish to add an custom locator you can change it in the example. its a bad practice to use //* it will go over the whole DOM, write more specific locator ex:(//a[@data-selenium='link_front_generic'])[1] there is plenty of tutorials how to find element by index. If there is something else as an error, please let us know. – Infern0 Oct 03 '18 at 14:08