Selenium Python How to get web content from href without click on it

I have logged in the web by using Selenium with Python. Now I want to find all the href links and download/save the webpage of the links without using .click(). The href is something like

<a href="project.cfm?CFID=162805&amp;CFTOKEN=98566707&amp;fromlist=1&amp;project_refid=10700290" target="_parent">sth (2) - 2 xox | oo - xx dd </a>

link example

2 answers

  • answered 2017-10-11 09:59 Shoaib Akhtar

    First get list of links as shown below

    el=driver.find_elements_by_xpath("//a")
    

    Then iterate through list and get url content

    list_of_links = driver.find_elements_by_xpath("//a")
    cur_win = driver.current_window_handle # get current/main window
    
    for link in list_of_links:
        link.click()
        driver.switch_to_window([win for win in driver.window_handles if win !=cur_win][0]) # switch to new window
        link_url = driver.current_url
        print(link_url)
        driver.close() # close new window
        driver.switch_to_window(cur_win) # switch back to main window
    

  • answered 2017-10-11 09:59 Aarya Hareendranath

    First you can get the links using below

    listOFLinks =driver.find_elements_by_xpath("//a")
    

    then iterate through the list

    for link in listOFLinks
    url = link.getAttribute("href");
    

    You can print the url variable or save it into an array as per your requirement