2014-02-20 7 views
2

다른 링크가 포함 된 10 개의 다른 페이지가 있습니다. 모든 링크를 클릭하는 방법?웹 페이지의 모든 링크를 클릭하는 방법

조건은 다음과 같습니다 은 내가 나에게 셀레늄 webdriver 스크립트를 제안 해주십시오 계산 및 각 링크

을 클릭합니다 가 얼마나 많은 링크 모른다. 웹 페이지에

답변

5

캡처 및 이동 모든 링크

반복자와 비슷한 일을 할 수 루프 고급; 그러나 루프 내의 페이지 탐색에 대한 불일치는 배열 개념을 사용하여 해결할 수 있습니다.

private static String[] links = null; 
private static int linksCount = 0; 

driver.get("www.xyz.com"); 
List<WebElement> linksize = driver.findElements(By.tagName("a")); 
linksCount = linksize.size(); 
System.out.println("Total no of links Available: "+linksCount); 
links= new String[linksCount]; 
System.out.println("List of links Available: "); 
// print all the links from webpage 
for(int i=0;i<linksCount;i++) 
{ 
links[i] = linksize.get(i).getAttribute("href"); 
System.out.println(all_links_webpage.get(i).getAttribute("href")); 
} 
// navigate to each Link on the webpage 
for(int i=0;i<linksCount;i++) 
{ 
driver.navigate().to(links[i]); 
Thread.sleep(3000); 
} 

1 | 하나

driver.get("www.xyz.com"); 
WebElement element = driver.findElement(By.id(Value)); 
List<WebElement> elements = element.findElements(By.tagName("a")); 
int sizeOfAllLinks = elements.size(); 
System.out.println(sizeOfAllLinks); 
for(int i=0; i<sizeOfAllLinks ;i++) 
{ 
System.out.println(elements.get(i).getAttribute("href")); 
} 
for (int index=0; index<sizeOfAllLinks; index++) { 
getElementWithIndex(By.tagName("a"), index).click(); 
driver.navigate().back(); 
} 

public WebElement getElementWithIndex(By by, int index) { 
WebElement element = driver.findElement(By.id(Value)); 
List<WebElement> elements = element.findElements(By.tagName("a")); 
return elements.get(index); 
} 

2로 ID와 이동 한 | 클래스 | 특정 프레임에서 모든 링크를 캡처 | 배열에있는 모든 링크를 캡처 [대체 방법]

자바

driver.get(baseUrl + "https://www.google.co.in"); 
List<WebElement> all_links_webpage = driver.findElements(By.tagName("a")); 
System.out.println("Total no of links Available: " + all_links_webpage.size()); 
int k = all_links_webpage.size(); 
System.out.println("List of links Available: "); 
for(int i=0;i<k;i++) 
{ 
if(all_links_webpage.get(i).getAttribute("href").contains("google")) 
{ 
String link = all_links_webpage.get(i).getAttribute("href"); 
System.out.println(link); 
} 
} 

파이썬

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get("https://www.google.co.in/") 
list_links = driver.find_elements_by_tag_name('a') 

for i in list_links: 
     print i.get_attribute('href') 

driver.quit() 
0

스토어 그 다음을 클릭합니다

ArrayList<WebElement> input_type = (ArrayList<WebElement>)  
    driver.findElements(By.tagName("a")); 

for (WebElement type : input_type) 
{ 


     type.click(); 


    } 

이 모든 하나 하나를 클릭합니다 태그와 링크, 당신이 요점을 가지고 있기를 바랍니다. 즐기십시오!

0
import java.util.List; 

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.WebElement; 

import org.openqa.selenium.firefox.FirefoxDriver; 

import org.openqa.selenium.firefox.FirefoxProfile; 

import org.openqa.selenium.firefox.internal.ProfilesIni; 

public class Find_all_Links { 

private static String testUrl = "http://www.google.co.in/"; 

private static WebDriver driver = null; 

public static void main(String[] args) { 

    ProfilesIni profile = new ProfilesIni(); 

    FirefoxProfile myProfile = profile.getProfile("AutomationQA"); 

    driver = new FirefoxDriver(myProfile); 

    driver.get(testUrl); 

    List<WebElement> oLinksOnPage = driver.findElements(By.tagName("a")); 

    System.out.println(oLinksOnPage.size()); 

    for(int i=0;i<oLinksOnPage.size();i++){ 

     System.out.println(oLinksOnPage.get(i).getText()); 
    } 


} 

} 
+0

답을 포맷하십시오 –

1
public static void main(String[] args) 
    { 
     FirefoxDriver fd=new FirefoxDriver(); 
     fd.get("http:www.facebook.com"); 
     List<WebElement> links=fd.findElements(By.tagName("a")); 
     System.out.println("no of links:" +links.size()); 

     for(int i=0;i<links.size();i++) 
     { 
      if(!(links.get(i).getText().isEmpty())) 
      { 
      links.get(i).click(); 
      fd.navigate().back(); 
      links=fd.findElements(By.tagName("a")); 
      }  
     } 
    } 

이 프로그램 링크를 클릭 다시 페이지로 이동하고 다시 제 2 링크를 클릭한다.

0
package selenium.tests; 

import java.util.List; 



import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.*; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class TestAllLinks { 



public static void main(String[] args) { 
     String baseUrl = "http://www.qaautomated.com/"; 
     System.setProperty("webdriver.chrome.driver", 

     "C:\\Users\\chromedriver_win32\\chromedriver.exe"); 
     WebDriver driver=new ChromeDriver(); 
     String notWorkingUrlTitle = "Under Construction: QAAutomated"; 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

     driver.get(baseUrl); 
     List<WebElement> linkElements = driver.findElements(By.tagName("a")); 
     String[] linkTexts = new String[linkElements.size()]; 
     int i = 0; 

     //extract the link texts of each link element 
     for (WebElement elements : linkElements) { 
      linkTexts[i] = elements.getText(); 
      i++; 
     } 

     //test each link 
     for (String t : linkTexts) { 
      driver.findElement(By.linkText(t)).click(); 
      if (driver.getTitle().equals(notWorkingUrlTitle)) { 
       System.out.println("\"" + t + "\"" 
         + " is not working."); 
      } else { 
       System.out.println("\"" + t + "\"" 
         + " is working."); 
      } 
      driver.navigate().back(); 
     } 
     driver.quit(); 
    } 
} 

http://www.qaautomated.com/2016/10/selenium-test-to-check-links-in-web.html

관련 문제