-1

How to write a code that will click links on a webpage with specified conditions:

The script should look into each div.table-row and check if list element (div.domains ul li) in the div contains certain string (somedomain.com), and click the link a.delete link inside which is in div.actions inside div.table-row.

Heres the HTML structure:

<div class="table-row">
  <div class="domains">
    <ul>
    <li>somedomain.com</li
    <li>someotherdomain.com</li>
    </ul>
  </div>
  <div class="actions">
    <ul class="menu">
    <li><a class="delete">Delete</a></li>
  </div>
</div>

How to write this code in ruby mechanize?

I have no idea how to select all .table-rows and loop through them to click delete links if condition is met?

Thanks for help

all jazz
  • 2,007
  • 2
  • 21
  • 37

1 Answers1

0

You'll need to use Javascript (not RoR) to loop through the elements and check for the correct class name. There's no way to programmatically "click" on a link, but you can use Javascript to perform the action you want.

<div class="table-row">
 <div class="domains">
  <ul>
   <li class="domain1">somedomain.com</li>
   <li class="domain2">someotherdomain.com</li>
  </ul>
 </div>
<div class="actions">
 <ul class="menu">
  <li><a class="delete">Delete</a></li>
</div>

As for the Javascript, this kind of thing is easiest with a Javascript framework like JQuery, but manually it would look something like this:

<script type="text/javascript">
  function searchForDomains(domainName) {
   var divs = document.getElementsByTagName('DIV');
    for (var i = 0; i < divs.length; i++) {
     var div = divs[i];
     if (div.className == 'table-row') {
       var tableRow = div;
       var lis = tableRow.getElementsByTagName('LI');
       for (var j = 0; j < lis.length; j++) {
         var li = lis[j];
         if (li.innerHTML == domainName) {
          doDelete(tableRow);
       }
     }
    }
  }

  function doDelete(tableRow) {
    // do whatever it is that you'd like to see happen if the delete link was clicked.
  }

What this does is loops through all the DIVs on the page, and when it finds one with the right class, it loops through all of the LIs inside that DIV, and when it finds one with the domain you're looking for, it calls a function that performs some action. You'd call this function either on page load, or in response to a user action like pressing a button.

sgress454
  • 24,870
  • 4
  • 74
  • 92