Ecco alcuni metodi alternativi anche per Selenium RC. Queste non sono soluzioni al selenio, ma consentono l'interazione con le strutture dati del linguaggio di programmazione e il selenio.
È anche possibile ottenere l'origine della pagina HTML, quindi regolare l'origine per restituire una serie di collegamenti. Utilizza il raggruppamento delle espressioni regolari per separare gli URL, collegare il testo/ID, ecc. E puoi quindi trasferirli nuovamente al selenio per fare clic su o navigare.
Un altro metodo è ottenere l'origine della pagina HTML o innerHTML (tramite i locatori DOM) di un elemento padre/root, quindi convertire l'HTML in XML come oggetto DOM nel linguaggio di programmazione. È quindi possibile attraversare il DOM con l'XPath desiderato (con espressione regolare o meno) e ottenere un nodeset solo dei collegamenti di interesse. Dalla loro analisi esce il testo del link/ID o URL e puoi tornare al selenio per fare clic su o navigare.
Su richiesta, sto fornendo esempi di seguito. Sono lingue miste, dal momento che il post non sembra essere comunque specifico per la lingua. Sto solo usando quello che avevo a disposizione per hackerare insieme per degli esempi. Non sono stati completamente testati o testati, ma in passato ho lavorato con bit del codice in altri progetti, quindi questi sono esempi di codice concettuali su come implementare le soluzioni che ho appena menzionato.
//Example of element attribute processing by page source and regex (in PHP)
$pgSrc = $sel->getPageSource();
//simple hyperlink extraction via regex below, replace with better regex pattern as desired
preg_match_all("/<a.+href=\"(.+)\"/",$pgSrc,$matches,PREG_PATTERN_ORDER);
//$matches is a 2D array, $matches[0] is array of whole string matched, $matches[1] is array of what's in parenthesis
//you either get an array of all matched link URL values in parenthesis capture group or an empty array
$links = count($matches) >= 2 ? $matches[1] : array();
//now do as you wish, iterating over all link URLs
//NOTE: these are URLs only, not actual hyperlink elements
//Example of XML DOM parsing with Selenium RC (in Java)
String locator = "id=someElement";
String htmlSrcSubset = sel.getEval("this.browserbot.findElement(\""+locator+"\").innerHTML");
//using JSoup XML parser library for Java, see jsoup.org
Document doc = Jsoup.parse(htmlSrcSubset);
/* once you have this document object, can then manipulate & traverse
it as an XML/HTML node tree. I'm not going to go into details on this
as you'd need to know XML DOM traversal and XPath (not just for finding locators).
But this tutorial URL will give you some ideas:
http://jsoup.org/cookbook/extracting-data/dom-navigation
the example there seems to indicate first getting the element/node defined
by content tag within the "document" or source, then from there get all
hyperlink elements/nodes and then traverse that as a list/array, doing
whatever you want with an object oriented approach for each element in
the array. Each element is an XML node with properties. If you study it,
you'd find this approach gives you the power/access that WebDriver/Selenium 2
now gives you with WebElements but the example here is what you can do in
Selenium RC to get similar WebElement kind of capability
*/
Quindi cosa non funziona e in che modo non funziona? Puoi pubblicare l'HTML (o un suo frammento) a cui stai confrontando? –
@Paul, l'esempio fornito e il metodo get_attribute() restituisce solo un singolo elemento, non un elenco. Poster sta chiedendo qual è l'equivalente per la restituzione di un elenco di attributi, ad esempio. – David