【发布时间】:2021-04-01 18:02:10
【问题描述】:
所以我在单击 HTML 中的隐藏导航栏时遇到了 selenium 问题...我需要单击操作按钮,然后单击导出(Excel)。
目前,我将其设置为 aria-labeledby=share,这假设我想要单击 Export(CSV) 并执行任务(当脚本想要工作时......如果没有,那么我会收到以下错误)。当我尝试设置 find_elements_by_css_selector() 或 find_elements_by_xpath() 并将其指向 Export(Excel) 时,它不起作用。所以我目前将它设置为 aria-labeledby=Share 并且它可以正常工作,但它会随机停止工作,我会收到以下错误。在检查网页元素之前,我从未听说过嵌入 HTML 的 WAI-Aria。
action = ActionChains(driver)
aria_lablededBy = driver.find_elements_by_css_selector("[aria-labeledby=Actions]")[0]
action.move_to_element(aria_lablededBy).click(aria_lablededBy).perform()
share = driver.find_elements_by_css_selector('[aria-labeledby=Share]')[0]
action.move_to_element(share).click(share).perform()
上面的代码运行,然后随机停止,我得到这个错误信息
ERROR:
Traceback (most recent call last):
File "c:/Users/xxxxx/Desktop/Python/import/export.py", line 53, in <module>
action.move_to_element(share).click(share).perform()
File "C:\Anaconda\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform
self.w3c_actions.perform()
File "C:\Anaconda\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
(Session info: chrome=87.0.4280.88)
我要连接的 HTML 站点有 Aria-HTML 代码,我对此并不熟悉。我删除了共享变量和共享变量上的以下操作,脚本将打开操作导航...一旦启用共享变量和操作,它就会给我错误
我在这个问题上的表现超出了我的水平,所以如果社区中有人知道如何解决这个问题,我非常感兴趣。
HTML 代码 - 隐藏的操作菜单
<lp-mega-menu _ngcontent-iyn-c45="" _nghost-iyn-c31="">
<nav _ngcontent-iyn-c31="" class="dropdown" lpmegamenu="" role="menu" triggers="manual" id="KJ0X5NWLXZGZ8" aria-labeledby="Actions">
<button _ngcontent-iyn-c31="" aria-haspopup="true" tabindex="0" type="button" class="lp-themed-button lp-themed-primary-button" aria-controls="dropdown-menu-KJ0X5NWLXZGZ8" aria-expanded="false">
<!----><!----><!----><span _ngcontent-iyn-c31="">Actions</span><i _ngcontent-iyn-c31="" class="fas fa-caret-down mr-0 ml-default-half"></i>
</button>
<div _ngcontent-iyn-c31="" class="dropdown-menu dropdown-menu-right mega-menu-panel my-2" id="dropdown-menu-KJ0X5NWLXZGZ8" aria-hidden="true" style="max-height: 600px;">
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-panel-wrapper label-reset ul-reset">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-grid">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col">
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Authoring</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Authoring">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Add Record"><span _ngcontent-iyn-c31="">Add</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Filter Report"><span _ngcontent-iyn-c31="">Filters (0)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Edit Report"><span _ngcontent-iyn-c31="">Edit</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Copy Report"><span _ngcontent-iyn-c31="">Copy</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Delete Report"><span _ngcontent-iyn-c31="">Delete</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col">
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Bulk Ops</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Bulk Ops">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Edit Records"><span _ngcontent-iyn-c31="">Edit</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Move Records"><span _ngcontent-iyn-c31="">Move</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Edit/Move Records"><span _ngcontent-iyn-c31="">Edit/Move</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Delete Records"><span _ngcontent-iyn-c31="">Delete</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col-md">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Share</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Share">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Print a PDF"><span _ngcontent-iyn-c31="">Print (PDF)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Export as Excel"><span _ngcontent-iyn-c31="">Export (Excel)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Export as CSV"><span _ngcontent-iyn-c31="">Export (CSV)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Copy Report Link"><span _ngcontent-iyn-c31="">Link</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!---->
</div>
</nav>
</lp-mega-menu>
HTML 代码 - 操作菜单可见
<lp-mega-menu _ngcontent-iyn-c45="" _nghost-iyn-c31="">
<nav _ngcontent-iyn-c31="" class="dropdown open" lpmegamenu="" role="menu" triggers="manual" id="KJ0X5NWLXZGZ8" aria-labeledby="Actions">
<button _ngcontent-iyn-c31="" aria-haspopup="true" tabindex="0" type="button" class="lp-themed-button lp-themed-primary-button" aria-controls="dropdown-menu-KJ0X5NWLXZGZ8" aria-expanded="true">
<!----><!----><!----><span _ngcontent-iyn-c31="">Actions</span><i _ngcontent-iyn-c31="" class="fas fa-caret-down mr-0 ml-default-half"></i>
</button>
</nav>
</lp-mega-menu>
以下是 HTML - Action Menu Hidden 代码的一部分,但一旦菜单可见,代码就会移到此元素之外:
<div _ngcontent-iyn-c31="" class="dropdown-menu dropdown-menu-right mega-menu-panel my-2" id="dropdown-menu-KJ0X5NWLXZGZ8" aria-hidden="false" style="max-height: 600px; display: block; height: 252px; right: 8px; top: 128px;">
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-panel-wrapper label-reset ul-reset">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-grid">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col">
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Authoring</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Authoring">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Add Record"><span _ngcontent-iyn-c31="">Add</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Filter Report"><span _ngcontent-iyn-c31="">Filters (0)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Edit Report"><span _ngcontent-iyn-c31="">Edit</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Copy Report"><span _ngcontent-iyn-c31="">Copy</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Delete Report"><span _ngcontent-iyn-c31="">Delete</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col">
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Bulk Ops</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Bulk Ops">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Edit Records"><span _ngcontent-iyn-c31="">Edit</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Move Records"><span _ngcontent-iyn-c31="">Move</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Edit/Move Records"><span _ngcontent-iyn-c31="">Edit/Move</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Bulk Delete Records"><span _ngcontent-iyn-c31="">Delete</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
<!---->
<div _ngcontent-iyn-c31="" class="mega-menu-col-md">
<!----><!----><!---->
<div _ngcontent-iyn-c31="" class="mega-menu-body">
<label _ngcontent-iyn-c31="">Share</label>
<div _ngcontent-iyn-c31="" class="mega-menu-content">
<ul _ngcontent-iyn-c31="" aria-labeledby="Share">
<!---->
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Print a PDF"><span _ngcontent-iyn-c31="">Print (PDF)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Export as Excel"><span _ngcontent-iyn-c31="">Export (Excel)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Export as CSV"><span _ngcontent-iyn-c31="">Export (CSV)</span></a><!---->
</li>
<li _ngcontent-iyn-c31="">
<!----><a _ngcontent-iyn-c31="" class="lp-theme-link-focus lp-theme-link-hover lp-theme-link-active" tabindex="0" title="Copy Report Link"><span _ngcontent-iyn-c31="">Link</span></a><!---->
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!----></div
完整的 HTML - 隐藏操作时 您将看到第二个方块包含有关下拉菜单的信息,并且位于 Nav 段下方
单击操作后,DIV 容器将移出 Nav 段,现在是 Body Segment 的一部分
【问题讨论】:
-
你可以分享网站或html代码吗?
-
@NicLaforge 抱歉,我不能分享链接或代码,因为这是一个内部网站……我可以分享这两个元素的片段吗?
-
是的,我很困惑你为什么需要 ActionChains。似乎您必须单击操作菜单才能显示导航菜单。你能在菜单隐藏和显示时提供一个截图吗?
-
@NicLaforge 我正在使用 ActionChains,因为如果我这样做 Share.click() 脚本不会点击...同样至于代码片段,我已将其添加到问题中,因为 HTML 元素相当大。
-
你看过了吗:stackoverflow.com/questions/58997720/…。您提到菜单将移到元素之外。从提供的代码中,您将 ``find_elements``` 与
index=0一起使用。你有没有检查找到了多少元素。我怀疑两个,你可能点击了错误的一个?如果是这种情况,您可以改进 css_selector 以选择正确元素下的那个。此外,您确定当它失败时菜单实际上是打开的吗?
标签: python selenium selenium-webdriver webdriver webdriverwait