我如何 select 这个不可见的 link 缺少 Selenium WebDriver 的 id?

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

我正在尝试 select 这个 link、'User Requested'。当鼠标悬停在菜单上时它会出现。

有这个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>

XPath

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

密码

  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();

错误

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

看来无论我怎么尝试,都不行。 link没有id


我设法让 a link 点击了,但是 select 错了 link!我使用 Chrome 右键单击​​元素以获取 CSS 路径和 XPath。 (有些 JavaScript 在 selected 菜单时设置了一个 id menu_mn_active。)该元素显示文本是正确的,但它转到了错误的页面。

  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";

输出

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

我在尝试单击下拉菜单中的项目时也经常遇到此错误:

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

我会将 xpath 简化为以下内容。正如其他人所说,使用 chrome 为您生成 xpath 确实不是一个好主意 - 或者如果您确实对它应用了一些想法。根据我的经验,chrome 通常不会在所有情况下生成正确或最有效的 xpath。

//a[contains(text(),'User Requested')]

OMFG 是因为浏览器window的大小,才弹出Chrome驱动。 window 是一个奇怪的形状,比正常情况下窄,我试图点击的元素稍微超出视口几个像素,尽管我可以清楚地看到它。当我手动调整 window 的大小时,一切正常!我在会话开始附近添加了这一行并且有效:

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

如果我的鼠标悬停在 window 上,它也不会工作!而我还要加一个sleep()等待菜单滑出。好脆!