Oto niektóre alternatywne metody również dla Selenium RC. Nie są to czyste rozwiązania Selenium, pozwalają na interakcję ze strukturami danych języka programowania i Selenium.
Możesz również uzyskać źródło strony HTML, a następnie wyrażenie regularne źródło, aby zwrócić zestaw dopasowanych linków. Użyj grupowania regex, aby odseparować adresy URL, tekst/identyfikator łącza itp., A następnie możesz przekazać je z powrotem do selenu, do którego można kliknąć lub nawigować.
Inną metodą jest pobranie źródła strony HTML lub innerHTML (za pomocą lokalizatorów DOM) elementu nadrzędnego/root, a następnie przekonwertowanie HTML na XML jako obiekt DOM w swoim języku programowania. Następnie możesz przemieścić DOM z żądaną ścieżką XPath (z wyrażeniem regularnym lub nie) i uzyskać zestaw węzłów tylko z interesującymi cię odsyłaczami. Po przeanalizowaniu tekstu/identyfikatora łącza lub adresu URL możesz wrócić do selenu, aby go kliknąć lub przejść do niego.
Na żądanie poniżej zamieszczam przykłady. Jest to język mieszany, ponieważ post nie wydawał się tak czy inaczej językiem. Używam tylko tego, co mam dostępne do zhakowania razem dla przykładów. Nie są w pełni przetestowane ani przetestowane, ale pracowałem z fragmentami kodu wcześniej w innych projektach, więc są to dowody na to, że przykłady kodu ilustrują sposób wdrożenia wspomnianych rozwiązań.
//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
*/
Co nie działa, a w jaki sposób nie działa? Czy możesz opublikować kod HTML (lub jego fragment), z którym pasujesz? –
@Paul, dany przykład i metoda get_attribute() zwraca tylko jeden element, a nie listę. Plakat pyta, jaki jest ekwiwalent zwrotu listy atrybutów, na przykład. – David