EXCEL VBA/ SELEIUM "Click on a Button" <> 找不到元素错误
EXCEL VBA/ SELEIUM "Click on a Button" <> Element not Found error
尝试单击“下载按钮”时,我总是遇到错误,但我可以浏览该页面。但是当需要单击 .xlsx 下载按钮 (button-1127) 时,它找不到该元素。有没有更好的办法 ??请原谅我粗糙的 VBA 编码。我唯一能想到的是它需要在可点击之前悬停。在 Chrome inspect 中,我确实看到 class 按钮按下和悬停的变化。我试过 xpath 和相同的结果。
元素在 iframe 中
<iframe id="uxtabiframe-1061-iframeEl">
Xpath:
//*[@id="button-1127"]
元素:
<a class="x-btn x-unselectable x-box-item x-toolbar-item uft-id-gridexportexcel x-btn-gridtoolbar-toolbar-small" hidefocus="on" unselectable="on" role="button" aria-hidden="false" aria-disabled="false" id="button-1127" tabindex="10910" data-componentid="button-1127" style="left: 2794px; margin: 0px; top: 3px;"><span id="button-1127-btnWrap" data-ref="btnWrap" role="presentation" unselectable="on" style="" class="x-btn-wrap x-btn-wrap-gridtoolbar-toolbar-small "><span id="button-1127-btnEl" data-ref="btnEl" role="presentation" unselectable="on" style="" class="x-btn-button x-btn-button-gridtoolbar-toolbar-small x-btn-no-text x-btn-icon x-btn-icon-left x-btn-button-center "><span id="button-1127-btnIconEl" data-ref="btnIconEl" role="presentation" unselectable="on" class="x-btn-icon-el x-btn-icon-el-gridtoolbar-toolbar-small exportExcel " style=""></span><span id="button-1127-btnInnerEl" data-ref="btnInnerEl" unselectable="on" class="x-btn-inner x-btn-inner-gridtoolbar-toolbar-small"> </span></span></span></a>
代码:
'
Set d = New ChromeDriver
With d
.SetPreference "download.default_directory", DOWNLOAD_DIRECTORY
.SetPreference "download.directory_upgrade", True 'safeguard
.SetPreference "download.prompt_for_download", False 'safeguard
.Get URL
.FindElementById("textfield-1034-inputEl").Click
.SendKeys ("*******")
Application.Wait (Now + TimeValue("00:00:1"))
.FindElementById("textfield-1035-labelEl").Click
.SendKeys ("******")
Application.Wait (Now + TimeValue("00:00:1"))
.FindElementById("button-1036").Click
Application.Wait (Now + TimeValue("00:00:5"))
.FindElementById("tab-1064-btnEl").Click
Application.Wait (Now + TimeValue("00:00:10"))
.FindElementById("button-1127-btnEl").Click
Application.Wait (Now + TimeValue("00:00:10"))
d.Quit
End With
`
我需要切换 iframe 才能看到元素。
示例:
.SwitchToFrame ("uxtabiframe-1061-iframeEl")
Application.Wait (Now + TimeValue("00:00:2"))
.FindElementById("button-1127").Click
Application.Wait (Now + TimeValue("00:00:5"))
尝试单击“下载按钮”时,我总是遇到错误,但我可以浏览该页面。但是当需要单击 .xlsx 下载按钮 (button-1127) 时,它找不到该元素。有没有更好的办法 ??请原谅我粗糙的 VBA 编码。我唯一能想到的是它需要在可点击之前悬停。在 Chrome inspect 中,我确实看到 class 按钮按下和悬停的变化。我试过 xpath 和相同的结果。
元素在 iframe 中
<iframe id="uxtabiframe-1061-iframeEl">
Xpath:
//*[@id="button-1127"]
元素:
<a class="x-btn x-unselectable x-box-item x-toolbar-item uft-id-gridexportexcel x-btn-gridtoolbar-toolbar-small" hidefocus="on" unselectable="on" role="button" aria-hidden="false" aria-disabled="false" id="button-1127" tabindex="10910" data-componentid="button-1127" style="left: 2794px; margin: 0px; top: 3px;"><span id="button-1127-btnWrap" data-ref="btnWrap" role="presentation" unselectable="on" style="" class="x-btn-wrap x-btn-wrap-gridtoolbar-toolbar-small "><span id="button-1127-btnEl" data-ref="btnEl" role="presentation" unselectable="on" style="" class="x-btn-button x-btn-button-gridtoolbar-toolbar-small x-btn-no-text x-btn-icon x-btn-icon-left x-btn-button-center "><span id="button-1127-btnIconEl" data-ref="btnIconEl" role="presentation" unselectable="on" class="x-btn-icon-el x-btn-icon-el-gridtoolbar-toolbar-small exportExcel " style=""></span><span id="button-1127-btnInnerEl" data-ref="btnInnerEl" unselectable="on" class="x-btn-inner x-btn-inner-gridtoolbar-toolbar-small"> </span></span></span></a>
代码: '
Set d = New ChromeDriver
With d
.SetPreference "download.default_directory", DOWNLOAD_DIRECTORY
.SetPreference "download.directory_upgrade", True 'safeguard
.SetPreference "download.prompt_for_download", False 'safeguard
.Get URL
.FindElementById("textfield-1034-inputEl").Click
.SendKeys ("*******")
Application.Wait (Now + TimeValue("00:00:1"))
.FindElementById("textfield-1035-labelEl").Click
.SendKeys ("******")
Application.Wait (Now + TimeValue("00:00:1"))
.FindElementById("button-1036").Click
Application.Wait (Now + TimeValue("00:00:5"))
.FindElementById("tab-1064-btnEl").Click
Application.Wait (Now + TimeValue("00:00:10"))
.FindElementById("button-1127-btnEl").Click
Application.Wait (Now + TimeValue("00:00:10"))
d.Quit
End With
`
我需要切换 iframe 才能看到元素。
示例:
.SwitchToFrame ("uxtabiframe-1061-iframeEl")
Application.Wait (Now + TimeValue("00:00:2"))
.FindElementById("button-1127").Click
Application.Wait (Now + TimeValue("00:00:5"))