使用Splinter从动态菜单中进行选择

时间:2016-04-22 06:03:19

标签: python splinter

我正在尝试运行拆分脚本,以便在this网页中找到菜单选项。

enter image description here

菜单的HTML如下所示:

<table border="0" cellpadding="0" cellspacing="0" width="100%" class="logo">
  <tbody><tr>
    <td align="left" style="background : url('gfx/tab_fill.png'); background-repeat : repeat-x;">
      ...
      <a href="variants">
          <img src="gfx/tab_variants_F.png" alt="View variants" id="tab_variants" width="58" height="25" align="left" class="">
      </a>
      ...
    </td>
  </tr>
</tbody></table>

<ul id="menu_tab_variants" class="jeegoocontext" style="display: none; left: 180px; top: 90px;">
  <li class="icon"><a href="/variants" class="">
      <span class="icon" style="background-image: url(gfx/menu_magnifying_glass.png);"></span>View all genomic variants</a></li>
  <li class="icon"><a href="/variants/in_gene" class="">
     <span class="icon" style="background-image: url(gfx/menu_magnifying_glass.png);"></span>View all variants affecting transcripts</a></li>
  <li class="icon"><a href="/submit" class="">
      <span class="icon" style="background-image: url(gfx/plus.png);"></span>Create a new data submission</a></li>
  ... 
</ul>

脚本应悬停在菜单标题上,显示可见内容,然后将鼠标悬停在其上并显示哪些元素可见。但似乎没有任何东西可见。

from splinter import Browser

browser = Browser('firefox', wait_time=10)    
browser.visit("http://databases.lovd.nl/shared/variants")

tab_variants = browser.find_link_by_href("variants")
print tab_variants

tab_variants.first.mouse_over()
print browser.find_link_by_partial_text(
    "View all genomic variants").first.visible
print browser.find_link_by_partial_text(
    "View all variants affecting transcripts").first.visible

tab_variants.mouse_out()
print browser.find_link_by_partial_text(
    "View all genomic variants").first.visible
print browser.find_link_by_partial_text(
    "View all variants affecting transcripts").first.visible

我得到了这个输出:

[<splinter.driver.webdriver.WebDriverElement object at 0x18c29d0>]
False
False
False
False

我是否按照splinter中的方式使用了mouse_over方法,或者splinter无法处理此特定菜单。

1 个答案:

答案 0 :(得分:1)

在您检查.visible属性时,菜单尚未显示。但是,您可以通过将鼠标移动到子菜单来等待它:

from splinter import Browser

browser = Browser('firefox', wait_time=10)
browser.visit("http://databases.lovd.nl/shared/variants")

# move over the menu "Variant"
browser.find_by_id('tab_variants').mouse_over()
# move over the sub menu
browser.find_by_id('menu_tab_variants').mouse_over()

# print the menu items visibility
print browser.find_link_by_text("View all genomic variants").visible
print browser.find_link_by_text("View all variants affecting transcripts").visible

# move the mouse out of the sub menu
browser.find_by_id('menu_tab_variants').mouse_out()

# print the menu items visibility
print browser.find_link_by_text("View all genomic variants").visible
print browser.find_link_by_text("View all variants affecting transcripts").visible