Cómo obtener la fuente completa de un enlace usando selenio
-
07-07-2019 - |
Pregunta
Estoy usando selenium RC y quiero obtener todos los atributos y todo. Algo así como:
link = sel.get_full_link('//a[@id="specific-link"]')
y el resultado sería de:
print link
sería:
<a id="specific-link" name="links-name" href="url"> text </a>
¿Es esto posible?
gracias
Solución
Aquí hay una solución más elegante:
sel.get_eval("window.document.getElementByID('ID').innerHTML")
(no seas quisquilloso conmigo en javascript ..)
Otros consejos
Creo que la mejor manera de hacerlo sería usar el comando getHtmlSource para obtener toda la fuente HTML, y luego usar una expresión regular o un analizador HTML para extraer el elemento de interés.
El siguiente ejemplo de Java mostrará todos los enlaces a System.out:
selenium.open("http://www.example.com/");
String htmlSource = selenium.getHtmlSource();
Pattern linkElementPattern = Pattern.compile("<a\\b[^>]*href=\"[^>]*>(.*?)</a>");
Matcher linkElementMatcher = linkElementPattern.matcher(htmlSource);
while (linkElementMatcher.find()) {
System.out.println(linkElementMatcher.group());
}
Cadena href = selenium.getAttribute (" xpath = // a [@ id = " enlace-específico "] / @ href ")
He estado tratando de hacer esto, y se me ocurrió lo siguiente:
var selenium = selenio;
string linkText = selenium.GetText (" // a [@href = '/ admin / design-management'] ");
Assert.AreEqual (" Design Management " ;, linkText);
use el siguiente código para obtener todos los enlaces de la página:
$str3= "window.document.getElementsByTagName('a')";
$k = $this->selenium->getEval($str3);
$url = explode(",",$k);
$array_size = count($url);
$name=array();
$l=0;
for($i=0;$i<$array_size;$i++)
{
if(!strstr($url[$i], 'javascript'))
{
$name[$l]=$url[$i];
echo "\n".$name[$l];
$l++;
}
}
Si el enlace no es dinámico, intente esta solución cursi y hacky (Esto está en Python):
selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()
cursi pero funciona.
Nota: esto no funcionará si el enlace redirige.