How I Copy Text from Chrome Webpage using Javascript/applescript and XPath

webscrape
xpath
applescript
javascript

#1

I just purchased KM, and I encountered this problem on how to copy text from webpage using only the javascript and XPath.

I want to put the javascript or applescript in this macro.

Browser use: Chrome but if you have better ways in using Safari please share.

This is the website:
http://whois.melbourneit.com.au/index.php3

This is the OuterHTML that I want to copy the text.

<td width="35%" class="">Gumtree AU Pty Limited</td>

This is the Xpath:

/html/body/table[2]/tbody/tr/td[2]/table[2]/tbody/tr/td[2]/div[1]/center[2]/table/tbody/tr[22]/td[2]

The system clipboard should result like this "Gumtree AU Pty Limited" without ""

This is the full outerHTML from that webpage table.

<table width="61%" cellspacing="0" border="0">
    <tbody><tr><td colspan="2" class="listbody" align="CENTER">
            WhoIs Result For gumtree.com.au @ whois.auda.ltd</td></tr>
    <tr valign="BASELINE">
     <td width="29%"><b>Domain Name</b></td>
     <td width="35%">GUMTREE.COM.AU</td>
    </tr><tr valign="BASELINE">
     <td colspan="2" class="txtsmall"><hr size="1"></td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registry Domain ID</b></td>
     <td width="35%">D407400000000227694-AU</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registrar WHOIS Server</b></td>
     <td width="35%">whois.auda.org.au</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Last Modified</b></td>
     <td width="35%">2019-01-04T21:58:12Z</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registrar Name</b></td>
     <td width="35%">MarkMonitor Corporate Services Inc</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Status</b></td>
     <td width="35%">clientDeleteProhibited https://afilias.com.au/get-au/whois-status-codes#clientDeleteProhibited</td>
    </tr><tr valign="BASELINE">
     <td width="29%"></td>
     <td width="35%">clientUpdateProhibited https://afilias.com.au/get-au/whois-status-codes#clientUpdateProhibited</td>
    </tr><tr valign="BASELINE">
     <td width="29%"></td>
     <td width="35%">serverRenewProhibited https://afilias.com.au/get-au/whois-status-codes#serverRenewProhibited</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registrant Contact ID</b></td>
     <td width="35%">mmr-176010</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registrant Contact Name</b></td>
     <td width="35%">Domain Administrator</td>
    </tr><tr valign="BASELINE">
     <td colspan="2" class="txtsmall"><hr size="1"></td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Tech Contact ID</b></td>
     <td width="35%">mmr-172996</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Tech Contact Name</b></td>
     <td width="35%">Domain Administrator</td>
    </tr><tr valign="BASELINE">
     <td colspan="2" class="txtsmall"><hr size="1"></td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Name Server</b></td>
     <td width="35%">NS4.P13.DYNECT.NET</td>
    </tr><tr valign="BASELINE">
     <td width="29%"></td>
     <td width="35%">NS1.P13.DYNECT.NET</td>
    </tr><tr valign="BASELINE">
     <td width="29%" class=""></td>
     <td width="35%" class="">NS3.P13.DYNECT.NET</td>
    </tr><tr valign="BASELINE">
     <td width="29%" class=""></td>
     <td width="35%">NS2.P13.DYNECT.NET</td>
    </tr><tr valign="BASELINE">
     <td width="29%" class=""><b>DNSSEC</b></td>
     <td width="35%" class="">unsigned</td>
    </tr><tr valign="BASELINE">
     <td width="29%" class=""><b>Registrant</b></td>
     <td width="35%" class="">Gumtree AU Pty Limited</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b>Registrant ID</b></td>
     <td width="35%" class="">ABN 33616996840</td>
    </tr><tr valign="BASELINE">
     <td width="29%" class=""><b>Eligibility Type</b></td>
     <td width="35%" class="">Registered Business</td>
    </tr><tr valign="BASELINE">
     <td width="29%"><b> Last update of WHOIS database</b></td>
     <td width="35%" class="">2019-01-31T13:37:24Z </td>
    </tr></tbody></table>

Just let me know if you have questions. I appreciate all your help guys. Thanks in advance.


#2

Given the above HTML from your web page, this Execute a JavaScript in Front Browser action should do the trick:

Example Results

image

JavaScript in Front Browser Action

image

JavaScript

var getXPath = '//b[text()="Registrant"]';
var oBold = document.evaluate(getXPath, document, null, XPathResult.ANY_TYPE, null).iterateNext();
var oTD = oBold.parentNode;
var registrantName = oTD.nextElementSibling.innerText;
registrantName;

Questions?

Please let us know if this works for you, or you have more questions.


#3

JMichaelTX Thank you very much sir, it works perfectly.

Is it possible sir, you can make macro to input only the XPath of the text that I want to extract?
Like this function in the image.


#4

Yes. See:

MACRO: Get Text of Element Using XPath Contains Text [Example]


#5

Wow, this is very powerful tool for me sir. Thank you very much. I hope you can help more people like me who has little knowledge in scripting. I will try to use this in different websites.