phphtmlseleniumxpathselenium-webdriver

How do I select this non-visible link lacking id with Selenium WebDriver?


I'm trying to select this link, 'User Requested'. It appears when the mouse hovers over the menu.

Imgur

It has this HTML

<ul class="k-widget k-reset k-header k-menu k-menu-horizontal" id="menu" data-role="menu" tabindex="0" role="menubar" aria-activedescendant="menu_mn_active">
...
<li class="k-item js-first-level k-state-default k-state-border-down" role="menuitem" style="z-index: 100;">
  <span class="k-link js-first-level k-state-active k-state-border-down" target="">Reports/Analytics<span class="k-icon k-i-arrow-s"></span></span>
  <div class="k-animation-container" style="width: 152px; height: 145px; margin-left: -2px; padding-left: 2px; padding-right: 2px; padding-bottom: 4px; overflow: visible; display: block; position: absolute; z-index: 10002; top: 27px; left: -2.703125px;">
    <ul class="k-group k-menu-group k-popup k-reset k-state-border-up" role="menu" data-role="popup"
    style="max-height: 578px; overflow: auto; display: block; font-size: 12px; font-family: &#39;Open Sans&#39;, sans-serif; font-stretch: normal; font-style: normal; font-weight: normal; line-height: normal; position: absolute; -webkit-transform: translateY(0px);">
      <li class="k-item js-second-level k-state-default k-first" role="menuitem">
        <a class="k-link js-second-level" href="..." target="">Reports / Analytics</a>
      </li>
      <li class="k-item js-second-level k-state-default" role="menuitem">
        <a class="k-link js-second-level" href="..." target="">User Requested</a>
      </li>

The XPath

//*[@id="menu"]/li[10]/div/ul/li[2]/a

The code

  echo "Switching to mainFrame\n";
  $driver->switchTo()->frame("mainFrame");
  echo "Finding link Reports/Analytics\n";
  //$input = $driver->findElement(WebDriverBy::partialLinkText('Reports/Analytics')); #didn't work
  $input = $driver->findElement(WebDriverBy::xpath('//*[@id="menu"]/li[10]/span'));
  $input->click();
  echo "Finding link User Requested\n";
  // $input = $driver->findElement(WebDriverBy::partialLinkText('User Requested')); # didn't work
  $input = $driver->findElement(WebDriverBy::xpath('//*[@id="menu"]/li[10]/div/ul/li[2]/a'));
  $input->click();

The error

Switching to mainFrame
Finding link Reports/Analytics
Finding link User Requested
PHP Fatal error:  Uncaught exception 'NoSuchElementException' with message 'no such element

It seems no matter what I try, it won't work. There is no id for the link.


I managed to get a link clicked, but it selects the wrong link! I'm using Chrome to right click on the elements to get the CSS Path and XPath. (Some JavaScript sets an id menu_mn_active when a menu is selected.) The element shows the text is correct, but it is going to the wrong page.

  echo "Finding link User Requested\n";
  // $input = $driver->findElement(WebDriverBy::partialLinkText('User Requested'));
  // $input = $driver->findElement(WebDriverBy::cssSelector('#menu_mn_active > div > ul > li:nth-child(2) > a'));
  // $input = $driver->findElement(WebDriverBy::xpath('//*[@id="menu"]/li[10]/div/ul/li[2]/a'));  
  $input = $driver->findElement(WebDriverBy::xpath('//*[@id="menu_mn_active"]/div/ul/li[2]/a'));
  echo("Link text: ".$input->getText()."\n");  
  $input->click();
  echo "Finding link Export Employees To Bamboo\n";

Output

Finding link User Requested
Link text: User Requested
Finding link Export Employees To Bamboo
PHP Fatal error:  Uncaught exception 'NoSuchElementException' 

I also frequently get this error when trying to click the item in the slide down menu:

PHP Fatal error:  Uncaught exception 'ElementNotVisibleException' with message 'element not visible

Solution

  • It was because the size of the browser window that the Chrome driver pops up. The window is an odd shape that is narrower than normal and the element I was trying to click was ever so slightly out of the view port by a few pixels, even though it was clearly visible to me. When I manually adjusted the size of the window everything worked perfectly! I added this line near the beginning of the session and it works:

      $driver->manage()->window()->maximize();
    

    Also, it won't work if my mouse is hovering over the window! And I have to add a sleep() to wait for the menu to slide out.