2014-02-20 48 views
2

我有10個不同的頁面包含不同的鏈接。如何點擊所有鏈接?如何點擊網頁中的所有鏈接

條件是: 我不知道有多少鏈接有 我想算,然後點擊每個鏈接

。請給我建議硒的webdriver腳本。

回答

5

捕捉和瀏覽所有的網頁鏈接

迭代器和先進的for循環可以做類似的工作;但是,循環內的頁面導航不一致可以使用數組概念來解決。

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和一個導航|捕捉在特定幀中的所有鏈接|類|在一個陣列捕獲的所有鏈接[替代方法]

的Java

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); 
} 
} 

的Python

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")); 
      }  
     } 
    } 

這個程序點擊一個鏈接,導航回頁面,再次點擊第二個鏈接。

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