【问题标题】:Python Selenium trying to export a filePython Selenium 尝试导出文件
【发布时间】:2016-01-05 22:46:17
【问题描述】:

我昨天偶然发现了 Selenium,并且能够成功登录网站并导航到我通常必须手动完成的页面。我试图复制 xpath,但找不到该元素。它看起来像这样:

我想导航到数据 --> CSV 格式并点击它。

我对其进行了编辑以包含检查元素的顶部。这归结为“导出为 PDF”选项,请记住我想要的是 CSV 格式,而不是 PDF。

<document>
<html lang="en" dir="ltr" style="visibility: visible;">
<head>
<body class="PortalBody HTMLBody" style="">
<div class="HeaderContainer">
<script type="text/javascript">obips.history.create();</script>
<form style="position: absolute; top: -1000px; display:none;">
<script>var dashboardCurrentStyle = "FusionFx" </script>
<div id="idEmbedFrameDiv" style="" src="saw.dll?ReloadDashboard&InFrameset=false&Page=Dashboard%20Insight&Embed=true&PortalPath=%2fshared%20-%20DW%2f_portal%%20Dashboard%20Detail&Caller=Dashboard&ViewState=01qglnht7bjvn3nf9qrofvafjm"/>
<script type="text/javascript">obips.ResourceManager.seedResource({p:"common/privileges.xml",v:{exprs:{"canAccessCatalogUI":true,"canAccessCatalogSearchUI":false,"canAccessHomePage":true,"canAccessSimpleSearchField":false,"canAccessAdvancedSearchLink":false,"canAccessOpenMenu":true,"canAccessNewMenu":true,"canAccessHelpMenu":true,"canAccessDashboardsMenu":true,"canAccessFavoritesMenu":true,"canAccessMyAccountLink":true,"canAccessCustomLinks":false,"enableClientStorage":true,"enableSessionStorage":true,"enableLocalStorage":true,"canReloadMetadata":false,"canOpenMetadataDict":false}}});saw.header.alertsNode = ""; saw.header.renderHeader(null, 'Dashboard Detail', '', '', {'Oracle': 'olh/l_en/toc.htm', 'Oracle Publisher': "saw.dll?xmlpObjectTasks" + '&Action=help', 'Marketing': 'olh/l_en/marketing/mstoc.html' });saw.getSessionInfos().portalPath='/shared/_portal/Dashboard Detail';</script>
<script type="text/javascript">obidash.pageIdentifier = 'sfcjp24m8jpf8gbu';obidash.canSaveSelectionForMe = true;</script>
<div id="idPersonalizationMenu" class="NQWMenu" sdir="right" menuinit="" href="javascript:void(null)" onmouseout="NQWMenuMouseOut(event)" onmouseover="NQWMenuMouseOver(event)">
<div id="idDashboardPrintDisplayLayoutMenu" class="NQWMenu" sdir="left" submenuinit="" href="javascript:void(null)" onmouseout="NQWMenuMouseOut(event)" onmouseover="NQWMenuMouseOver(event)">
<div id="idDashboardPrintManageLayoutMenu" class="NQWMenu" sdir="left" submenuinit="" href="javascript:void(null)" onmouseout="NQWMenuMouseOut(event)" onmouseover="NQWMenuMouseOver(event)">
<div id="idDashboardExportToExcelMenu" class="NQWMenu" sdir="left" submenuinit="" href="javascript:void(null)" onmouseout="NQWMenuMouseOut(event)" onmouseover="NQWMenuMouseOver(event)">
<div id="idSavedSelectionsMenu" class="NQWMenu" sdir="left" submenuinit="PersonalizationEditor.getSavedSelectionsMenu" href="javascript:void(null)" onmouseout="NQWMenuMouseOut(event)" onmouseover="NQWMenuMouseOver(event)">
<script src="res/b_mozilla/common/componentheader.js" type="text/javascript"/>
<span id="sawruler" style="display:none;visibility:hidden;white-space:nowrap;"/>
<div class="ComponentHeader ">
<iframe id="idEmbed" style="visibility: hidden; width: 0px; height: 0px; position: absolute; top: 0px;" src="res/empty.htm" name="idEmbed" title="idEmbed"/>
<div id="idDownloadLinksMenud:dashboard~p:sfcjp24m8jpf8gbu~r:tom49amoppk60jjc" class="NQWMenu" onmouseover="NQWMenuMouseOver(event)" style="visibility: visible; display: block; position: absolute; top: 422px; left: 820px;" binit="true" role="menu" aria-activedescendant="popupMenuItem">
<table class="menuShadowWrapper" cellspacing="0">
<tbody>
<tr>
<td class="shadowMenuCell" colspan="2" rowspan="2">
<a id="popupMenuItem" class="NQWMenuItem NQWMenuItemWIconMixin" onclick="NQWClearActiveMenu();Download('saw.dll?Go&ViewID=d%3adashboard%7ep%3asfcjp24m8jpf8gbu%7er%3atom49amoppk60jjc&Action=Download&SearchID=c7gl7pgfjfdkhekcg09p3p8guq&Style=FusionFx&PortalPath=%2fshared%20-%20DW%2f_portal%20Dashboard%20Detail&Page=Dashboard%20Insight&ViewState=5cb5o5dpdpsh5id2japa3i0rvu&ItemName=Report%20Data&path=%2fshared%20-%20DW%20Dashboard%20New%20Report%20Data&Format=pdf&Attach=1'); return false" href="javascript:void(null);" name="SectionElements" style="width: 119px;" tabindex="0" role="menuitem" aria-label="PDF">
<table class="MenuItemTable" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="MenuItemIconCell">
<td class="MenuItemTextCell">PDF</td>
</tr>
</tbody>
</table>
</a>

【问题讨论】:

  • 好吧,如果你能显示完整的 HTML 代码或 URL 那就更好了。但是如果你真的做不到,你的 HTML 代码部分只有一个 a 标签,你想直接点击吗?
  • 看起来您需要使用ActionChain() 类及其相关方法。它将允许您执行您可能需要执行的鼠标悬停类型的操作。
  • 我尝试添加更多信息,希望对您有所帮助。不幸的是,我对这些东西完全陌生。

标签: python selenium xpath


【解决方案1】:

没有给出“导出”菜单标题和“数据”菜单项的 HTML。因此为他们做出假设。

menu = driver.find_element_by_xpath('//table[@id="menu"]//td[text()="Export"]')
menuItem = driver.find_element_by_xpath('//table[@id="menu"]//td[text()="Data"]')
subMenuItem = driver.find_element_by_xpath('//table[@id="MenuItemTable"]//td[text()="CSV Format"]')

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.move_to_element(menuItem)
actions.move_to_element(subMenuItem)
actions.click(subMenuItem)
actions.perform()

【讨论】:

  • 我可以试试这个。导出按钮的 xpath 有效。我可以看到它正确弹出。我只是无法从那里导航。
  • ".//*[@id='popupMenuItem']/table/tbody/tr/td[2]" 这是为所有菜单项指定的 xpath。所以我无法指定“数据”或“CSV 格式”。这是“导出”按钮的 xpath,但它确实有效:“.//*[@id='d:dashboard~p:sfcjp24m8jpf8gbu~r:tom49amoppk60jjcLinks']/tbody/tr/td[5]/a”跨度>
  • .//*[@id='popupMenuItem']/table/tbody/tr/td[text()='Data']可以试试。
【解决方案2】:
Actions a =new Actions(driver);
a.moveToElement(xpath of Export).moveToElement(xpath of Data).moveToElement(xpath of CSV Format).click().perform();

【讨论】:

  • 这是一个通过oracle运行的商业智能门户。它还需要用户名和密码等。我希望“检查元素”部分就足够了。
  • 这应该是评论而不是答案。
  • @KetanSethi:那么你应该发布一些真实答案来获得这 50 个代表。这很容易,因为我今天获得了 100 个代表。
  • @kevinguan 好的先生当然
  • 等等,我不认为这是 Python 代码。 OP 正在使用 Python 运行 selenium
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 2020-07-31
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 2011-08-10
相关资源
最近更新 更多