小编典典

在Python中使用Selenium单击隐藏链接

selenium

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

<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”标签中的“文档”链接。当我将鼠标悬停在菜单上时,代码更改如下:

<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标签中看到“ visible :: hidden”部分消失了。

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

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

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

download_button.click()

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

ElementNotVisibleException: Message: element not visible

所以我尝试了

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

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

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扩展。


阅读 350

收藏
2020-06-26

共1个答案

小编典典

您应该尝试使用ExplicitWaits,直到Documents链接可见并启用单击,如下所示:-

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()
2020-06-26