与对象内部的HTML交互

时间:2018-07-25 15:46:58

标签: javascript node.js selenium-webdriver

我正在使用axios从getnada.com消息api返回此对象。下面的对象是axios返回的内容。在该对象内部是一些html代码(尽我最大的努力清理了html)。无论如何,是否可以使用id =“ ActivateButton”在URL上启动某种点击事件?我正在将node.js与selenium-webdriver一起使用。

{ uid: 'vxqFqoQGw60LYgFc3kwtbYwVA8U8f2',
  ib: 'tsljt8kq@getnada.com',
  f: 'noreply_healthid@optum.com',
  s: 'Confirm your Optum ID email address',
  d: false,
  at: [],
  r: 1532531106,
  html: '<!DOCTYPE HTML>\n<html lang="en">\n
                <head>\n    
                    <meta charset="utf-8"/>\n    
                    <title>\n        Your Optum ID account information\n    </title>\n    
                    <style type="text/css">\n
                        .textRegular\n
                        {\n            
                            color:#000;\n            
                            font-family:"Arial Regular","Arial";\n            
                            font-size:13px;\n        
                        }\n\n        
                            .textRegularBold\n        
                        {\n            
                            font-family:"Arial Regular","Arial";\n            
                            font-size:13px;\n            
                            font-weight:bold;\n        
                        }\n\n        
                            .textTitle\n        
                        {\n
                            color:#C36121;\n            
                            font-family:"Arial Bold","Arial";\n            
                            font-size:16px;\n            
                            font-weight:bold;\n        
                        }\n\n        
                            .tableCellBorder\n        
                        {\n            
                            border: 1px solid #ccc;\n            
                            padding: 15px;\n            
                            margin: 0px;\n        
                        }\n\n        
                            .tableWhole\n        
                        {\n            
                            border: 0px;\n
                            padding: 0px;\n            
                            margin: 0px;\n            
                            border-collapse:collapse;\n        
                        }\n\n    
                    </style>\n
                </head>\n
                <body>\n
                    <div class="content" role="main">\n\n\n\n\n\n\n\n\n
                        <table style="border: 0px; padding: 0px; margin: 0px; border-collapse:collapse; font-family: \'Arial Regular\', \'Arial\'; font-size: 13px;" role="presentation">\n    
                            <tr>\n        
                                <td style="border: 1px solid #ccc; padding: 15px; margin: 0px;">\n        
                                    <img  alt="MAHealthConnector" src="https://stg2-healthid.optum.com/tb/services/rest/rp/rpapplogo?rpid=MHC28623">\n        
                                    <h1 style="font-size: 16px; color: #924719; font-weight: bold; font-family: \'Arial Bold\' , \'Arial\';margin-top: 0px !important; margin-bottom: 7px !important;" id="emailHeading"><br/>
                                        Just one step left to do and your Optum ID will be ready to use. Click the button:<br/>
                                    </h1>\n\n\n\n\n\n\n\t\n\t
                                    <table cellspacing="0" cellpadding="0">\n\t\t<tr>\n\t\t\t<td align="center" style="color: #000; font-family: \'Arial Regular\', \'Arial\'; font-size: 13px;;  display: block; background-color:#f3f3f3;">\n\t\t\t<a style="display: inline-block; font-size: 17px; background-color:#f3f3f3; width: 200px; height: 28px; padding: 26px; text-decoration: none; color: #333333; border: 1px solid #333333; text-align: center;" href="https://stg2-healthid.optum.com/tb/link/verify-account?verificationDetails=5OFHzqRihZbR0c5g4aZW9s1HBwjg9eJ%2Fdm%2BVF%2FdeuZzV1Eofn8UJZFY37qCXmN6RRB3rHEwCk1iFwXcyVpm5VFbOAYyIV7c1nSYZ3nhTBS6cji3DyItOHmS3OjQHSzJjJibmormBRYYDues%2BI6l08fFBu42eYeoAyj16y1rb3984nAqSPPP6Yko5MBipXNI8bUR3VeObg57TdzNSDfqIGNq%2FxQk6h0UwP4QQVodihvVg3ew8f4j44IAFaipXXONoYxqLZuWSbe3d39WFKe6pOg2R9XBzXeSKk0TjtbxPOjI%3D&relyingAppId=MHC28623" id="ActivateButton">Activate my Optum ID\n</a>\n\t\t\t
                                </td>\n\t\t
                            </tr>\n\t
                        </table>\n\n\t
                        <p style="color: #000; font-family: \'Arial Regular\', \'Arial\'; font-size: 13px;margin-top: 0px !important; margin-bottom: 7px !important;; margin-top: 20px !important;" id="emailRegistrationPrimaryCode">\n\t
                            If you prefer, copy this 10-digit code 2771832358 and paste it into the boxfor the activation code on the Activate Your Optum ID page.\n\t
                        </p>\n\t
                        <p style="color: #000; font-family: \'Arial Regular\', \'Arial\'; font-size: 13px;margin-bottom: 0;display: inline;" id="emailRegistrationPrimaryNotRequestedMsg">\n\t
                            If you did not request an activation link or code, or if you have questions about setting up an Optum ID, contact us at 1-855-819-5909 or <a href="mailto:optumsupport@optum.com">optumsupport@optum.com</a>.\n\t
                        </p>\n\n\n
                    </div>\n
                    <div role="contentinfo">\n
                        <p style="margin: 26px 0 0;" id="emailThank">
                            Thank you,\n
                        </p>\n
                        <p style="margin:0;" id="companyName">
                            Optum ID
                        </p>\n
                    </div>\n
                </body>\n
            </html>\n',
  text: '',
  in: true,
  ru: true,
  fe: 'noreply_healthid@optum.com',

2 个答案:

答案 0 :(得分:1)

您可以使用cheerio操作DOM树。

const cheerio = require('cheerio');

const json = { html: '...' };
const $ = cheerio.load(json.html);
const script = `
    <script>
        function onActivateClick(event){
            // YOU CODE HERE
        }
    </script>
`;
$('body').append(script);
$('#ActivateButton').attr('onclick', 'onActivateClick();');

const newHTML = $.html();

答案 1 :(得分:0)

感谢您建议加油!我能够抓取DOM并解析元数据以获得所需的链接。然后,我使用执行脚本在新选项卡中打开链接。

const json = { html: '...' };
const $ = cheerio.load(json.html);
var parsedResults = [];

$('a#ActivateButton').each(function(i, element){
    var url = $(this).attr('href');
    parsedResults.push(url);
});

browser.executeScript('window.open("' + parsedResults[0] + '");');