Driving a website using VBA and Selenium, pt2

2019-07-13 12:23发布

In this question I received some much needed help in moving my VBA/Selenium project forward (and it provides some additional background for this question, if it's needed), but my extreme lack of HTML knowledge is rearing its ugly head again.

I've managed to get the log in working, I can navigate to the page where the download link is located, and I have identified where the download link is, but I have not been able to programmatically click the download link.

This is the link itself:

<a id="download_link" class="downloadIcon" href="siam/downloadresponses.asp?sid=<ABunchOfHexDigits>&amp;s=1">Download CSV Data</a> 

and this is the HTML from the page leading up to that line:

<frameset rows="43,30,*" framespacing="0" frameborder="0" border="0">
  <frame title="Top banner" src="topbannerframe.asp?sid=<ABunchOfHexDigits>&amp;bc=2&amp;im=0&amp;nm=1" id="banner" name="banner" scrolling="no" noresize="">
  <frame title="Survey title" src="titlebannerframe.asp?sid=<ABunchOfHexDigits>" id="title" name="title" scrolling="no" noresize="">
  <frameset rows="*" cols="35%,*" frameborder="1" framespacing="10" border="10" bordercolor="#53a9db">
    <frame src="oasysframesleft.asp?sid=<ABunchOfHexDigits>&amp;t=summ" scrolling="no" id="leftframe" name="leftframe">
      #document
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Untitled Document</title>
      </head>
      <frameset rows="22,*" framespacing="0" frameborder="0" border="0">
      <frame src="left_tab.asp?sid=<ABunchOfHexDigits>&amp;t=summ" scrolling="no" noresize="">
      <frame src="summary.asp?sid=<ABunchOfHexDigits>" id="contentframe" name="contentframe" scrolling="yes" noresize="">
      #document
      <html mxmlns="http://www.w3.org/19999/xhtml">
      <body class="whitebg" onload="checkview();">
      <div id="allitems">
        <div class="errortext"></div>
        <table class="bars" cellspacing="0" cellpadding="0">
          <tbody><tr class="alt">
            <th class="alt">Title:</th>
            <td class="alt">Title text</td>
          </tr>
        <tr>
          <th>Start date:</th>
          <td>Tue Jan 10 2017 10:26 EST</td>
        </tr>
        <tr class="alt">
          <th class="alt">Status:</th>
          <td class="alt">Questionnaire live</td>
        </tr>
        <tr>
          <th>Uploaded respondents:</th>
          <td>0</td>
        </tr>
        <tr class="alt">
          <th>Cases:</th>
          <td>139<a id="download_link" class="downloadIcon" href="siam/downloadresponses.asp?sid=<ABunchOfHexDigits>&amp;s=1">Download CSV Data</a></td>
        </tr>
        <tr>...</tr>
        <tr>...</tr>
      </tbody></table>
    </div>
  </body></noframes>
</frameset>
<frame src="oasysframesright.asp?sid=<ABunchOfHexDigits>&amp;t=results" scrolling="no" id="rightframe" name="rightframe">
</frameset>
<noframes><body></body></noframes>
</frameset>

It appears to me that the download link is located within several layers of frames, but not iframes as was the case on my previous question. I have attempted to use

Driver.SwitchToFrame(0)
Driver.SwitchToFrame(2)
Driver.SwitchToFrame(1)

based on this image:

enter image description here

It will do the switch to (0), but subsequent calls fail with a "no frame by that name" (paraphrased) error. I tried Driver.SwitchToFrame(5) to go directly to the contentframe and it doesn't like that one either.

I've tried

Text = "html > frameset > frameset > frame#leftframe > html > frameset > frame#contentframe > html > body.whitebg > div#allitems > table.bars > tbody > tr.alt > td > a#download_link.downloadicon"
Set El = Driver.FindElementByCss(Text)

where I got the CSS path from Chrome's dev tools here:

enter image description here

but that didn't work, either.

I can get the page loaded using the Selenium IEDriver, and once I do that, selecting View Source from the IE11 window that is opened produces this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Variables</title>
<link href="css/unified-stylesheet-min.css?x=21" rel="stylesheet" type="text/css" media="all" />
<script language="JavaScript1.2" src="draganddrop3-min.js?x=35" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
document.onmousedown=selectmouse;
function checkview(){
  var content_iframe = parent.document.getElementById('content_iframe');
  if(content_iframe)
    content_iframe.height=document.getElementById('allitems').offsetHeight+300;

}
//]]>
</script>
</head>
<body class="whitebg" onload="checkview();">
  <div id="allitems">
    <div class="errortext"></div>
    <table class="bars" cellspacing="0" cellpadding="0">
      <tr class="alt">
        <th class="alt">Title:</th>
        <td class="alt">Title text</td>
      </tr>
      <tr>
        <th>Start date:</th>
        <td>Tue Jan 10 2017 10:26 EST</td>
      </tr>
      <tr class="alt">
        <th class="alt">Status:</th>
        <td class="alt">Questionnaire live</td>
      </tr>
      <tr>
        <th>Uploaded respondents:</th>
        <td>0</td>
      </tr>
      <tr class="alt">
        <th>Cases:</th>
        <td>139<a id="download_link" class="downloadIcon" href="siam/downloadresponses.asp?sid=<ABunchOfHexDigits>&amp;s=1">Download CSV Data</a></td>
      </tr>
      <tr>
        <th class="alt">Partials:</th>
        <td class="alt">0</td>
      </tr>
      <tr class="alt"><th>URL:</th><td>https://www.snapsurveys.com/wh/s.asp?k=<numeric code></td></tr>
    </table>
  </div>
</body>
</html>

which looks quite a bit different than what either Chrome or FF are showing me.

These are the various things I've tried (once I have loaded the download link page):

'declarations made & "Driver" set earlier in the code
Dim Driver As IEDriver
Set Driver = New IEDriver
Dim El As WebElement

'click on the "Download" link
'    Set El = Driver.FindElementByName("contentframe")
'    Driver.SwitchToFrame ("contentframe")
    Driver.SwitchToFrame (0)
'    Driver.SwitchToFrame (1)
'    Driver.SwitchToFrame (1)

'    Text = "Download CSV Data"
'    Text = "//*[@id=""download_link""]"  'result of "copy XPath"
'    Set El = Driver.FindElementByLinkText(Text)
'    Set El = Driver.FindElementByXPath(Text)
'    Text = "html > frameset > frameset > frame#leftframe > html > frameset > frame#contentframe > html > body.whitebg > div#allitems > table.bars > tbody > tr.alt > td > a#download_link.downloadicon"
    Text = "#download_link"
'    <a id="download_link" class="downloadIcon" href="siam/downloadresponses.asp?sid=<ABunchOfHexDigits>&amp;s=1">Download CSV Data</a>
    Set El = Driver.FindElementByCss(Text)
'    Set El = Driver.FindElementByClass("whitebg")
    El.Click

Would someone please point me in the right direction for being able to select the "Download CSV Data" link from this page. (Note: I cannot provide a direct link to the page - it is a secured page with HIPAA protected data on it.)

1条回答
放我归山
2楼-- · 2019-07-13 12:42

I finally figured it out thanks to this comment from 3 years ago!

When counting (0-based) frames, frame sets are not included in the count, so my numbers were off on my Display.SwitchToFrames() calls.

Based on my original look at the frames in the console: enter image description here
I was using this code:

Driver.SwitchToFrame(0)
Driver.SwitchToFrame(2)
Driver.SwitchToFrame(1)

Based on the newfound knowledge to not count the frame sets, I recounted: enter image description here
and came up with this code:

Driver.SwitchToFrame (2)
Driver.SwitchToFrame (1)

From there,

Text = "download_link"
Set El = Driver.FindElementById(Text)
El.Click

works a treat!

查看更多
登录 后发表回答