0
import unittest
from selenium import webdriver
import HtmlTestRunner

class Link_Front_Page(unittest.TestCase):

   def setUp(self):
    self.driver = webdriver.Chrome(executable_path="F:\\automation\\chromedriver.exe")

  def test_front_page(self):
    driver = self.driver
    driver.maximize_window()
    driver.get("https://google.com")
    list_links = driver.find_elements_by_tag_name('a')
    for i in list_links:
        print(i.get_attribute('href'))

  def tearDown(self):
    self.driver.quit()


 if __name__ == "__main__":
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='F:\\automation\\reports'))

test_front_page (main.Link_Front_Page) ... OK (37.377611)s

Ran 1 test in 0:00:37 OK

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • If you are _not able to get all the links_, what are you getting _reduced number of links_ or some _error_? What is your exact usecase? – undetected Selenium Oct 22 '18 at 09:26
  • Hi sourav, can you add some extra info as suggested here? https://stackoverflow.com/help/how-to-ask your question right now can't be edited without basically write the question for you! in this way you will improve your chances to get an answer :) – pep 1 Oct 22 '18 at 09:27
  • i am getting only this message test_front_page (__main__.Link_Front_Page) ... OK (37.377611)s – sourav sharan Oct 22 '18 at 09:27
  • I have edited noe you can check – sourav sharan Oct 22 '18 at 09:32
  • @souravsharan , Why are you using UnitTest framework for *getting links*? There are no assertions in your test – Andersson Oct 22 '18 at 09:42
  • @Andersson i am new in it i thought we have to write test cases like that only can you send me the poper code – sourav sharan Oct 22 '18 at 09:51

1 Answers1

1

There is no need to use UnitTest, pyTest or other test frameworks to just automate some web routine with Selenium.

You can use below code to print out link references:

driver = webdriver.Chrome(executable_path="F:\\automation\\chromedriver.exe")
driver.maximize_window()
driver.get("https://google.com")
list_links = driver.find_elements_by_tag_name('a')
for i in list_links:
    print(i.get_attribute('href'))

Note that if you want to get Google search results you should enter string into search input field and submit your query, e.g.:

search = driver.find_element_by_name('q')
search.send_keys('Qwerty')
search.submit()
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • and how could i export all the url into excel sheet or html file – sourav sharan Oct 22 '18 at 10:19
  • You can use code [to write values into Excel file](https://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet) instead of `print(i.get_attribute('href'))` – Andersson Oct 22 '18 at 10:23