• Open Google
  • Perform Search for Term
  • Open top 5 links in users browser

In [2]:
!pip install selenium


Collecting selenium
  Downloading selenium-3.3.1-py2.py3-none-any.whl (930kB)
Installing collected packages: selenium
Successfully installed selenium-3.3.1

In [7]:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

# Ask the user for a search term
search_term = input("What would you like to search for?: ")

# Open Firefox
# For this to work you need to download the geckodriver
# https://github.com/mozilla/geckodriver/releases
# This binary need to be available on the system PATH, ex: python directory
browser = webdriver.Firefox()

# Open google.com
browser.get('https://google.com')
search_elem = browser.find_element_by_name("q")
search_elem.send_keys(search_term)
search_elem.submit()

# Wait till javascript finishes
time.sleep(2)

# use beautifulsoup to find top 5 links
soup = BeautifulSoup(browser.page_source, "lxml")
links = soup.select("div.g div.rc > h3.r > a")[0:5]
if len(links) > 0:
    for l in links:
        browser2 = webdriver.Firefox()
        browser2.get(l.get("href"))
else:
    print("No results found")


What would you like to search for?: mike
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-7-f9b95fb0c998> in <module>()
     28     for l in links:
     29         browser2 = webdriver.Firefox()
---> 30         browser2.get(l.get("href"))
     31 else:
     32     print("No results found")

C:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in get(self, url)
    248         Loads a web page in the current browser session.
    249         """
--> 250         self.execute(Command.GET, {'url': url})
    251 
    252     @property

C:\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    236         response = self.command_executor.execute(driver_command, params)
    237         if response:
--> 238             self.error_handler.check_response(response)
    239             response['value'] = self._unwrap_value(
    240                 response.get('value', None))

C:\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    191         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    192             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 193         raise exception_class(message, screen, stacktrace)
    194 
    195     def _value_or_default(self, obj, key, default):

WebDriverException: Message: Failed to decode response from marionette

In [ ]: