public function getElementsByLabel($labelText)
{
// what are we doing?
$log = usingLog()->startAction("get elements for label '{$labelText}'");
// our return value
$retval = [];
try {
// build up the xpath to use
$xpathList = ['descendant::*[text()]//parent::label', 'descendant::label[normalize-space(text()) = "' . $labelText . '"]'];
// search using the xpath
$tmpElements = $this->getElementsByXpath($xpathList);
// we're going to search in a case-insensitive manner
$searchText = trim(rtrim(strtolower($labelText)));
// filter out any labels that do not have the text we want
$labelElements = [];
foreach ($tmpElements as $tmpElement) {
$elementText = strtolower(trim(rtrim($tmpElement->text())));
$elementText = str_replace(["\n", "\r\n"], " ", $elementText);
if ($elementText == $searchText) {
$labelElements[] = $tmpElement;
} else {
usingLog()->writeToLog("rejecting element containing text '{$elementText}'");
}
}
// we cannot filter by visibility here - the <label> may be
// visible but the <input> may be invisible :(
} catch (Exception $e) {
$log->endAction("did not find label '{$labelText}'");
throw $e;
}
// search all of the label elements to find an associated input
// element that we can safely use
foreach ($labelElements as $labelElement) {
try {
// add each element that matches this label
$retval[] = $this->getElementAssociatedWithLabelElement($labelElement, $labelText);
} catch (Exception $e) {
// do nothing
}
}
// log the result
$log->endAction(count($retval) . " element(s) found");
// return the elements
return $retval;
}