在Python中使用Selenium单击隐藏链接 [英] Click Hidden Link Using Selenium in Python

查看:145
本文介绍了在Python中使用Selenium单击隐藏链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中使用Selenium尝试单击鼠标悬停在该下拉菜单上时出现的下拉菜单中的链接.这是下拉菜单信息:

I am using Selenium in Python to try to click a link within a drop down menu that appears when the mouse hovers over that drop down menu. Here is the drop down menu info:

<div id="DownloadMenu">
    <ul id="DownloadMenu_control" class="topmenu" widgetid="DownloadMenu_control">
    <li class="horizontal-list-item-2 leftFloatedItem" id="DownloadMenu_control@BatchPrintSaveMenu_1" widgetid="DownloadMenu_control@BatchPrintSaveMenu_1" style="">
    <a class="horizontal-list-item-2 leftFloatedItem" href="javascript:void(0)">Download</a>
    <ul id="common_navigation_MenuControl_6" class="submenu" style="display: none; left: 0px; visibility: hidden;" widgetid="common_navigation_MenuControl_6">
    <li class="subitems" id="DownloadMenu_control@DownloadSelectedDocumentsMenuItem" widgetid="DownloadMenu_control@DownloadSelectedDocumentsMenuItem">
    <a href="javascript:com.reuters.rts.event(event, 'openMultiDocInfoViewerBatchViewEikon', 'itemsCollectionId', 'AllDocumentListView_items', 'researchTableManagerId', 'AllDocumentListView', 'summary', escape(encodeURIComponent(dijit.byId('advancedSearchManager').getFilterSubmittedSummary())), 'fileSizeLimitForMerging', '25','forBatchPrint','true', 'action','Save');">Documents</a>
    </li>
    </ul>
    </li>
    </ul>
</div>

我试图单击"a href"标签中的文档"链接.当我将鼠标悬停在菜单上时,代码更改如下:

I am trying to click on the "Documents" link in the "a href" tag. When I hover over the menu, the code changes as follows:

<div id="DownloadMenu">
    <ul id="DownloadMenu_control" class="topmenu" widgetid="DownloadMenu_control">
    <li class="topitemOn" id="DownloadMenu_control@BatchPrintSaveMenu_1" widgetid="DownloadMenu_control@BatchPrintSaveMenu_1" style="">
    <a class="horizontal-list-item-2 leftFloatedItem" href="javascript:void(0)">Download</a>
    <ul id="common_navigation_MenuControl_6" class="submenu" style="display: none; left: 0px;" widgetid="common_navigation_MenuControl_6">
    <li class="subitems" id="DownloadMenu_control@DownloadSelectedDocumentsMenuItem" widgetid="DownloadMenu_control@DownloadSelectedDocumentsMenuItem">
    <a href="javascript:com.reuters.rts.event(event, 'openMultiDocInfoViewerBatchViewEikon', 'itemsCollectionId', 'AllDocumentListView_items', 'researchTableManagerId', 'AllDocumentListView', 'summary', escape(encodeURIComponent(dijit.byId('advancedSearchManager').getFilterSubmittedSummary())), 'fileSizeLimitForMerging', '25','forBatchPrint','true', 'action','Save');">Documents</a>
    </li>
    </ul>
    </li>
    </ul>
</div>

您可以在第二个ul标签中看到可见性:隐藏的部分".

You can see that the visibility:hidden part goes away in the second ul tag.

我使用以下代码在页面上找到了元素:

I have found the elements on the page using the following code:

download_menu = driver.find_element_by_id("DownloadMenu")
download_button = download_menu.find_elements_by_tag_name('a')[1]

如果我尝试使用以下方法单击下载按钮:

If I try to click the download button using:

download_button.click()

我收到一个错误消息,指出该元素不可见:

I get an error that the element is not visible:

ElementNotVisibleException: Message: element not visible

所以我尝试了

ActionChains(driver).move_to_element(download_menu).move_to_element(download_button).click(download_button).perform()

当我这样做时,什么也没发生.因此,我尝试执行脚本以使菜单可见,然后单击,但该菜单也不起作用:

When I do this, nothing happens. So, I tried executing the script to make the menu visible and then clicking and that didn't work either:

visibility_tag = download_menu.find_elements_by_tag_name('ul')[1]
driver.execute_script("arguments[0].style.display = 'block'; arguments[0].style.left = '0px'; arguments[0].style.visibility = ''", visibility_tag)

当我运行execute_script函数时,菜单在我的浏览器中变得可见.仍然无法单击它.我在这里疯了.有任何想法吗?我正在Python中使用Selenium的Chrome扩展程序.

When I run the execute_script function, the menu DOES become visible on my browser. It just still can't be clicked. I'm going crazy here. Any ideas? I'm using the Chrome extension of Selenium in Python.

推荐答案

您应该尝试使用

You should try using ExplicitWaits to wait until Documents link visible and enable to click as below :-

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

wait = WebDriverWait(driver, 10)

download_menu = driver.find_element_by_id("DownloadMenu")

action = ActionChains(driver)
#hover on download_menu first
action.move_to_element(download_menu).perform()

#now find Documents link and click
documents = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Documents")))
documents.click()

这篇关于在Python中使用Selenium单击隐藏链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆