How to click on All links in web page in Selenium WebDriver

后端 未结 8 2045
余生分开走
余生分开走 2020-12-29 00:04

I have 10 different pages contain different links. How to click on all the links?

Conditions are : i) I don\'t know how many links are there ii) I want to count and

8条回答
  •  北海茫月
    2020-12-29 00:44

    Capture and Navigate all the Links on Webpage

    Iterator and advanced for loop can do similar job; However, the inconsistency on page navigation within a loop can be solved using array concept.

    private static String[] links = null;
    private static int linksCount = 0;
    
    driver.get("www.xyz.com");
    List 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

    1| Capture all links under specific frame|class|id and Navigate one by one

    driver.get("www.xyz.com");  
    WebElement element = driver.findElement(By.id(Value));
    List elements = element.findElements(By.tagName("a"));
    int sizeOfAllLinks = elements.size();
    System.out.println(sizeOfAllLinks);
    for(int i=0; i elements = element.findElements(By.tagName("a")); 
    return elements.get(index);
    }
    

    2| Capture all links [Alternate method]

    Java

    driver.get(baseUrl + "https://www.google.co.in");
    List 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

    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()
    

提交回复
热议问题