首页 > 解决方案 > 访问网页中的主要 HTML 文档以使用 Selenium 自动化 Chrome

问题描述

我会通过单击我公司某个网页中的某个按钮和链接序列来自动下载 CSV。

但是,通过查看页面源代码,该网页没有显式的 HTML 元素;它显示如下:

<HTML ><head><title></title>
<script language="javascript" src="23048/scripts/swecommon.js?_scb="></script> 
<script language="javascript" src="23048/scripts/swecmn_hi.js?_scb="></script> 
</head><body leftmargin="8" topmargin="2" marginwidth="20" marginheight="2" class="ScreenBack"><script>
if(typeof(window._swescript)!='undefined'&&window._swescript!=null) window._swescript.InitFrame(this);</script>
<h1><span class="AppletTitle">Session Warning Message</span></h1><h4><span class="error">You have initiated a Siebel session while another Siebel session is currently active. Please choose the option that applies to you.</span></h4><ul><li><span class="error">You already have a Siebel session with unsaved data running in another browser window. To save data from a second session go to the already open session and either save and exit or continue to use that data.</span></li><li><span class="error">You do not have unsaved data in your other session and wish to close it and launch a new session.&nbsp;</span><span class="error">Click&nbsp;</span><a href='JavaScript:void(0)' onclick='SWEClearHistoryGotoURL ("start.swe?SWENeedContext=false&SWECmd=Logoff&SWEC=14&SWEBID=-1&SRN=&SWETS="); return false;'  target="_top" id='s_swepi_2'>here</a></li><li><span class="error">You have closed your previous Siebel browser instance using the Close instead of the Logoff feature and wish to open a new Siebel session.&nbsp;</span><span class="error">Click&nbsp;</span><a href='JavaScript:void(0)' onclick='SWEClearHistoryGotoURL ("start.swe?SWENeedContext=false&SWECmd=Logoff&SWEC=14&SWEBID=-1&SRN=&SWETS="); return false;'  target="_top" id='s_swepi_2'>here</a><script>

trackChange("");
SWEIsHighInteract = true; Top().SWEIsHighInteract = true;
if (typeof(window._samePage) != 'undefined' && window._samePage!="") window._samePage = "";
if (top.opener == null || typeof top.opener.swetop == 'unknown' || typeof top.opener.swetop == 'undefined') top.SiebelApp.S_App.SWECount = 14;
else top.opener.top.SiebelApp.S_App.SWECount = 14;
g_bInitialized = true;
</script>
</li></ul>

但是,当我尝试检查链接或按钮时,它会显示一个 HTML 文档,我认为我可以通过 Selenium 方法对其进行解析。

VBA 中的代码不适用于这个奇怪的网页,它适用于像 Facebook 这样的简单网页:

Sub TestSelenium()
    Dim ChDriver As New webdriver
    ChDriver.Start "Chrome"
    ChDriver.get "https://chromedriver.storage.googleapis.com/index.html?path=83.0.4103.39/"
    Application.Wait Now + TimeValue("00:00:05")
    Stop
    ChDriver.FindElementByXPath("/html/body/table/tbody/tr[6]/td[2]/a").Click
    Stop
End Sub

提前谢谢。

标签: excelvbaseleniumselenium-webdriver

解决方案


推荐阅读