如何通过Selenium和VBA根据html单击带有文本作为SRP Banner的元素?

时间:2018-08-23 04:44:33

标签: vba selenium selenium-webdriver xpath webdriver

我正在尝试使用网页中的硒vba单击href,但出现以下错误消息。

下面是我尝试过的代码。

Sub test()
    Dim Driver As New Selenium.FirefoxDriver
    Driver.Get "http://sums.99acres.com/sums/?profilename=sums"
    Driver.FindElementByName("userlogin") _
    .SendKeys("xyz") _
    .Submit
    Driver.FindElementByName("password") _
    .SendKeys("12345") _
    .Submit
    Driver.Get "http://sums.99acres.com/sums/getmyproducts.php?trans_id=1819-T0639786"
    Driver.Wait 5000
    Driver.FindElementByXPath("//a[contains(@class,'navlink')][contains(text(),'SRP Banner')]").Click
End Sub

我也尝试过

Driver.FindElementByLinkText("SRP Banner").Click

以下是我收到的错误消息

enter image description here

下面是我要单击的屏幕截图

enter image description here

下面是html代码。

<frame src="http://sums.99acres.com/sums/products_offered.php?cid=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998792&amp;trans_id=1819-T0639786" name="left" frameborder="1">
<html><head><script type="text/javascript" src="https://bam.nr-data.net/1/1621b6db8b?a=103016861&amp;v=1071.385e752&amp;to=NV1TZkBVXBdVWk1RVgwXZEBbG0ERWUoWSEsNXERRRkdtC1JfXEpcBhZBWkI%3D&amp;rst=847&amp;ref=http://sums.99acres.com/sums/products_offered.php&amp;ap=736&amp;be=817&amp;fe=840&amp;dc=839&amp;perf=%7B%22timing%22:%7B%22of%22:1534998816389,%22n%22:0,%22f%22:0,%22dn%22:0,%22dne%22:0,%22c%22:0,%22ce%22:0,%22rq%22:13,%22rp%22:798,%22rpe%22:801,%22dl%22:803,%22di%22:838,%22ds%22:838,%22de%22:839,%22dc%22:839,%22l%22:839,%22le%22:840%7D,%22navigation%22:%7B%7D%7D&amp;at=GRpQEAhPTxk%3D&amp;jsonp=NREUM.setToken"></script><script src="https://js-agent.newrelic.com/nr-1071.min.js"></script><script type="text/javascript">window.NREUM||(NREUM={}),__nr_require=function(e,t,n){function r(n){if(!t[n]){var o=t[n]={exports:{}};e[n][0].call(o.exports,function(t){var o=e[n][1][t];return r(o||t)},o,o.exports)}return t[n].exports}if("function"==typeof __nr_require)return __nr_require;for(var o=0;o<n.length;o++)r(n[o]);return r}({1:[function(e,t,n){function r(){}function o(e,t,n){return function(){return i(e,[f.now()].concat(u(arguments)),t?null:this,n),t?void 0:this}}var i=e("handle"),a=e(2),u=e(3),c=e("ee").get("tracer"),f=e("loader"),s=NREUM;"undefined"==typeof window.newrelic&&(newrelic=s);var p=["setPageViewName","setCustomAttribute","setErrorHandler","finished","addToTrace","inlineHit","addRelease"],d="api-",l=d+"ixn-";a(p,function(e,t){s[t]=o(d+t,!0,"api")}),s.addPageAction=o(d+"addPageAction",!0),s.setCurrentRouteName=o(d+"routeName",!0),t.exports=newrelic,s.interaction=function(){return(new r).get()};var m=r.prototype={createTracer:function(e,t){var n={},r=this,o="function"==typeof t;return i(l+"tracer",[f.now(),e,n],r),function(){if(c.emit((o?"":"no-")+"fn-start",[f.now(),r,o],n),o)try{return t.apply(this,arguments)}catch(e){throw c.emit("fn-err",[arguments,this,e],n),e}finally{c.emit("fn-end",[f.now()],n)}}}};a("setName,setAttribute,save,ignore,onEnd,getContext,end,get".split(","),function(e,t){m[t]=o(l+t)}),newrelic.noticeError=function(e){"string"==typeof e&&(e=new Error(e)),i("err",[e,f.now()])}},{}],2:[function(e,t,n){function r(e,t){var n=[],r="",i=0;for(r in e)o.call(e,r)&&(n[i]=t(r,e[r]),i+=1);return n}var o=Object.prototype.hasOwnProperty;t.exports=r},{}],3:[function(e,t,n){function r(e,t,n){t||(t=0),"undefined"==typeof n&&(n=e?e.length:0);for(var r=-1,o=n-t||0,i=Array(o<0?0:o);++r<o;)i[r]=e[t+r];return i}t.exports=r},{}],4:[function(e,t,n){t.exports={exists:"undefined"!=typeof window.performance&&window.performance.timing&&"undefined"!=typeof window.performance.timing.navigationStart}},{}],ee:[function(e,t,n){function r(){}function o(e){function t(e){return e&&e instanceof r?e:e?c(e,u,i):i()}function n(n,r,o,i){if(!d.aborted||i){e&&e(n,r,o);for(var a=t(o),u=m(n),c=u.length,f=0;f<c;f++)u[f].apply(a,r);var p=s[y[n]];return p&&p.push([b,n,r,a]),a}}function l(e,t){v[e]=m(e).concat(t)}function m(e){return v[e]||[]}function w(e){return p[e]=p[e]||o(n)}function g(e,t){f(e,function(e,n){t=t||"feature",y[n]=t,t in s||(s[t]=[])})}var v={},y={},b={on:l,emit:n,get:w,listeners:m,context:t,buffer:g,abort:a,aborted:!1};return b}function i(){return new r}function a(){(s.api||s.feature)&&(d.aborted=!0,s=d.backlog={})}var u="nr@context",c=e("gos"),f=e(2),s={},p={},d=t.exports=o();d.backlog=s},{}],gos:[function(e,t,n){function r(e,t,n){if(o.call(e,t))return e[t];var r=n();if(Object.defineProperty&&Object.keys)try{return Object.defineProperty(e,t,{value:r,writable:!0,enumerable:!1}),r}catch(i){}return e[t]=r,r}var o=Object.prototype.hasOwnProperty;t.exports=r},{}],handle:[function(e,t,n){function r(e,t,n,r){o.buffer([e],r),o.emit(e,t,n)}var o=e("ee").get("handle");t.exports=r,r.ee=o},{}],id:[function(e,t,n){function r(e){var t=typeof e;return!e||"object"!==t&&"function"!==t?-1:e===window?0:a(e,i,function(){return o++})}var o=1,i="nr@id",a=e("gos");t.exports=r},{}],loader:[function(e,t,n){function r(){if(!x++){var e=h.info=NREUM.info,t=d.getElementsByTagName("script")[0];if(setTimeout(s.abort,3e4),!(e&&e.licenseKey&&e.applicationID&&t))return s.abort();f(y,function(t,n){e[t]||(e[t]=n)}),c("mark",["onload",a()+h.offset],null,"api");var n=d.createElement("script");n.src="https://"+e.agent,t.parentNode.insertBefore(n,t)}}function o(){"complete"===d.readyState&&i()}function i(){c("mark",["domContent",a()+h.offset],null,"api")}function a(){return E.exists&&performance.now?Math.round(performance.now()):(u=Math.max((new Date).getTime(),u))-h.offset}var u=(new Date).getTime(),c=e("handle"),f=e(2),s=e("ee"),p=window,d=p.document,l="addEventListener",m="attachEvent",w=p.XMLHttpRequest,g=w&&w.prototype;NREUM.o={ST:setTimeout,SI:p.setImmediate,CT:clearTimeout,XHR:w,REQ:p.Request,EV:p.Event,PR:p.Promise,MO:p.MutationObserver};var v=""+location,y={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",agent:"js-agent.newrelic.com/nr-1071.min.js"},b=w&&g&&g[l]&&!/CriOS/.test(navigator.userAgent),h=t.exports={offset:u,now:a,origin:v,features:{},xhrWrappable:b};e(1),d[l]?(d[l]("DOMContentLoaded",i,!1),p[l]("load",r,!1)):(d[m]("onreadystatechange",o),p[m]("onload",r)),c("mark",["firstbyte",u],null,"api");var x=0,E=e(4)},{}]},{},["loader"]);</script>
<title>SUMS : MAINPAGE</title>

<style>
a:link, a:visited, a:active{color:#0000ff;}
</style>
</head>
<body style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">

<br><br><br>
<table width="80%" align="center" style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">
<tbody><tr><td>
<table width="100%" aligh="center"><tbody><tr align="center">
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;"><a href="../sums/mainpage.php">SUMS</a></td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;"><a href="../crm/mainpage.php?checksum=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998807" onclick="parent.location='http://sums.99acres.com/crm/mainpage.php?checksum=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998807'">CRM</a></td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;"><a href="../sums_mis/mainpage.php?name=vishal&amp;cid=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998807">Mis</a></td>
<td width="25%" style="color:#9400D3;font-size:14px;text-decoration:underline;"><a href="../sums/logout.php" onclick="parent.location='http://sums.99acres.com/sums/'"><!--a href="../sums/logout.php?name=vishal&cid=547e14073e8fca793e4629a2acca12c5Si13489029+1534998765+1534998807" -->Logout</a></td>
</tr></tbody></table><br>
</td></tr></tbody></table>
<br><br><table width="300" border="0" cellspacing="2" cellpadding="2" style="font-family:verdana, Helvetica, sans-serif;color:#000;font-size:11px;">
<tbody><tr>  <td colspan="2" style="color:#ff6600;font-weight:bold;font-size:18px;">PRODUCTS OFFERED</td></tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="19">1.</td>
<td width="374"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Tahoma','sans-serif'"><b><a href="#"></a><a target="right" href="http://sums.99acres.com/sums/zedo_srp_products.php?module=SUMS&amp;trans_id=1819-T0639786&amp;productType=SRPB&amp;prod=SRPB" class="navlink">SRP Banner </a></b> <span style="COLOR: gray">&nbsp;(Pending:&nbsp;1&nbsp;of&nbsp;1)</span></span></td>

</tr>



<tr>
<td colspan="2">&nbsp;</td>
</tr>

</tbody></table>

<br><br>
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"1621b6db8b","applicationID":"103016861","transactionName":"NV1TZkBVXBdVWk1RVgwXZEBbG0ERWUoWSEsNXERRRkdtC1JfXEpcBhZBWkI=","queueTime":0,"applicationTime":736,"atts":"GRpQEAhPTxk=","errorBeacon":"bam.nr-data.net","agent":""}</script>


</body></html>

2 个答案:

答案 0 :(得分:1)

根据您共享的 HTML ,在所需元素上点击 click 首先,您需要切换到框架,然后寻找该元素您可以使用以下解决方案:

Driver.Get "http://sums.99acres.com/sums/getmyproducts.php?trans_id=1819-T0639786"
Driver.Wait 5000
Driver.SwitchToFrame.FindElementByXPath("//frame[@name='left' and contains(@src,'http://sums.99acres.com/sums/products_offered.php')]", timeout:=10000)
Driver.Wait 3000
Driver.FindElementByXPath("//a[@class='navlink' and contains(@href,'http://sums.99acres.com/sums/zedo_srp_products.php?module')][contains(.,'SRP Banner')]").Click

答案 1 :(得分:0)

此时间太长,无法清晰地显示为评论。您可以尝试将pageSource HTML插入HTMLDocument并单击吗?下面的代码行从您已经拥有的Wait行继续进行,如下所示:

Driver.Wait 5000
Dim html As New HTMLDocument, ele As Object
html.body.innerHTML = Driver.PageSource

Set ele = html.getElementsByTagName("form")(0)
ele.querySelector("a[href*='trans_id=1819']").Click

我将其分几个阶段进行了操作,因此您可以在尝试单击之前调试是否设置了ele。