As noted above, this is JavaScript, not Java. We only mention it because in the future you'll get better responses (and those more quickly) by putting JavaScript in the post title rather than Java.
Anyway, I have written you an example Grease Monkey script. For your learning purposes, it can be installed here: »
www.jaaulde.com/greasemo ··· .user.jsThe source for it is below, but I'll quickly explain the base question you posed, which was how to select an input based on the text immediately following it. I grabbed all inputs in the document via getElementsByTagName and then began iterating over them. As I looped, I checked whether the input was a radio, and whether its nextSibling property had a nodeName of '#text'. This is the first half of your puzzle--nextSibling gets the next item in the DOM following the input, and text nodes have a nodeName of '#text'.
Once I determine we have a radio input followed by text, I move to the next half of the puzzle which is to determine if the text contains the desired value. I gain access to the actual text in string format by using the nodeValue property of the previously mention nextSibling, and I then used some regexes I assembled from your configured (lines 23 and 24) variables to see if the text is what we wanted.
That script is setup to run on anything under this directory on my server:
»
www.jaaulde.com/test_bed/jacour/If you look in that directory, you'll notice 3 pages. Clicking on the first, 1_startsHere.php, will run the Grease Monkey script which selects FedEx and submits to the next page, 2_movesToHere.php. This has the payment stuff on it and the Grease Monkey script will select Visa and submit to the final script, 3_endsHere.php. This script will show you the results of the form submissions (which were stored to PHP session as the chain was progressing).
The code for Grease Monkey looks like so:
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Shipping Radio Selection", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name Shipping Radio Selection
// @namespace http://jaaulde.com/greasemonkey
// @description Selects radios for shipping options and submits the forms
// @include http://www.jaaulde.com/test_bed/jacour/*
// ==/UserScript==
(function()
{
var shipper = 'FedEx';
var paymentMethod = 'Visa';
var shipperRegExp = new RegExp('(?:^|\\s+)'+shipper+'(?:\\s+|$)','i');
var paymentMethodRegExp = new RegExp('(?:^|\\s+)'+paymentMethod+'(?:\\s+|$)','i');
var allInputs = document.getElementsByTagName('input');
var targetInput = null;
for (var i = 0; i<allInputs.length; i++)
{
if (
allInputs[i].type === 'radio' &&
allInputs[i].nextSibling.nodeName === '#text' &&
(
allInputs[i].nextSibling.nodeValue.match(shipperRegExp) ||
allInputs[i].nextSibling.nodeValue.match(paymentMethodRegExp)
)
)
{
var targetInput = allInputs[i];
break;
}
}
if(targetInput !== null)
{
targetInput.checked = 'checked';
targetInput.form.submit();
}
}());