0
import requests
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
links = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", links)
driver = webdriver.Chrome(chrome_options=chrome_options, 
executable_path="F:\\automation\\chromedriver.exe")
driver.maximize_window()
driver.get('https://google.com/')
links = driver.find_elements_by_tag_name("a")
for link in links:
    r = requests.head(link.get_attribute('href'))
    print(link.get_attribute('href'), r.status_code, 'front_page')
driver.quit()

I am getting this error:

Traceback (most recent call last):
File "F:/automation/frontpages.py", line 15, in <module>
r = requests.head(link.get_attribute('href'))
File "F:\automation\venv\lib\site-packages\requests\api.py", line 101, in head return request('head', url, **kwargs)
File "F:\automation\venv\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs)
File "F:\automation\venv\lib\site-packages\requests\sessions.py", line 524, in request resp = self.send(prep, **send_kwargs)
File "F:\automation\venv\lib\site-packages\requests\sessions.py", line 631, in send adapter = self.get_adapter(url=request.url)
File "F:\automation\venv\lib\site-packages\requests\sessions.py", line 722, in get_adapter raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for 'mailto:care@pushengage.com'

and I want to export all the links into the HTML sheet when the test case passed

Why I am getting this error?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
sourav
  • 13
  • 6
  • @MosheSlavin When you add `blockquotes` to the error sack trace, the error logs gets _word wrapped_ and debugging the error logs becomes difficult. Can you please revert back the `blockquotes`? – undetected Selenium Oct 22 '18 at 13:37
  • This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – undetected Selenium Oct 22 '18 at 13:38
  • 1
    @DebanjanB I have made an edit... sorry – Moshe Slavin Oct 22 '18 at 15:46

1 Answers1

0

Some link has mailto link which is not http url .You are trying to use an mailto link(mailto:some@somepage.com) into r = requests.head. Which is not an http request, so your getting this error.

Filter the those invalid links in findelements itself.

links = driver.find_elements_by_xpath("//a[not(contains(@href,'mailto'))][contains(@href,'pushengage.com')]")
for link in links:
    r = requests.head(link.get_attribute('href'))
    print(link.get_attribute('href'), r.status_code, 'front_page')
driver.quit()
Navarasu
  • 8,209
  • 2
  • 21
  • 32
  • Traceback (most recent call last): File "F:/automation/frontpages.py", line 16, in r = requests.head(link.get_attribute('href')) File "F:\automation\venv\lib\site-packages\requests\api.py", line 101, in head return request('head', url, **kwargs) getting this error – sourav Oct 22 '18 at 13:25
  • Thanks it is working fine I have one more question how can I export all the passed links into excel sheet with status code – sourav Oct 22 '18 at 14:17
  • done I have one more question how can I export all the links into excel sheet or HTML page with status code and links – sourav Oct 22 '18 at 14:25
  • You can use this code https://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet – Navarasu Oct 22 '18 at 14:33