python-3.xselenium-webdriverfirefox-headlessduckduckgo

How to use headless driver in Python using Selenium


I have the below code

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

opts = Options()
opts.set_headless()
assert opts.headless  # Operating in headless mode
browser = Firefox(options=opts)
txt=browser.get('https://duckduckgo.com')
print(txt.text)

However, when i execute it, i get the error "AttributeError: 'NoneType' object has no attribute 'text'" I think I should get the text of the HTML.What am I doing wrong?


Solution

  • This error message...

    AttributeError: 'NoneType' object has no attribute 'text'
    

    ...implies that the you have tried to extract the text attribute from a NoneType object.


    get(url)

    get(url) method loads a web page in the current browser session and doesn't returns anything.

    Though you have tried to assign the variable text with the return value of get() as in:

    txt=browser.get('https://duckduckgo.com')
    

    txt still remains 'NoneType'


    Solution

    A bit more information about your usecase would have helped us to construct an answer in a better way. However if your are trying to retrieve the HTML you can use the page_source property as follows:

    Here you can find a detailed discussion on What is a None value?