셀레늄을 사용하여 링크의 전체 소스를 얻는 방법
-
07-07-2019 - |
문제
셀레늄 RC를 사용하고 있으며 모든 속성과 모든 속성을 얻고 싶습니다. 같은 것 :
link = sel.get_full_link('//a[@id="specific-link"]')
그리고 결과는 다음과 같습니다.
print link
다음과 같습니다.
<a id="specific-link" name="links-name" href="url"> text </a>
이게 가능해?
감사해요
해결책
더 멋진 솔루션은 다음과 같습니다.
sel.get_eval("window.document.getElementByID('ID').innerHTML")
(JavaScript에서 나에게 까다 롭지 마세요 ..)
다른 팁
이를 수행하는 가장 좋은 방법은 GethtmlSource 명령을 사용하여 전체 HTML 소스를 얻은 다음 정규식 또는 HTML 파서를 사용하여 관심 요소를 추출하는 것입니다.
다음 Java 예제는 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());
}
String href = selenium.getAttribute ( "xpath = // [@id ="species-link "]/@href")
나는 이것을하려고 노력했고 다음을 생각해 냈습니다.-
var selenium = 셀레늄;
문자열 linktext = selenium.getText ( "// [@href = '/admin/design-management']);
Assert.areequal ( "디자인 관리", LinkText);
아래 코드를 사용하여 페이지에서 모든 링크를 가져옵니다.
$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++;
}
}
링크가 역동적이지 않으면 다소 치즈 맛이 좋고 해킹 된 솔루션을 시도하십시오 (이것은 파이썬에 있습니다).
selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()
치즈하지만 작동합니다.
참고 : 링크가 리디렉션되는 경우 작동하지 않습니다.
제휴하지 않습니다 StackOverflow