#!/usr/bin/env python # coding: utf-8 # In[8]: # make sure you have done pip install selenium from selenium import webdriver from selenium.webdriver.common.by import By # In[28]: # create the driver by passing in the path of the chromedriver driver = webdriver.Chrome('/Users/mckayjohns/Downloads/chromedriver') # In[10]: # we can also add options # List of all options available here https://peter.sh/experiments/chromium-command-line-switches/ options = webdriver.ChromeOptions() options.add_argument("--headless") # runs the browser without a UI # create driver with the options driver = webdriver.Chrome('/Users/mckayjohns/Downloads/chromedriver', options=options) # In[29]: # Go to a webpage driver.get('https://www.serebii.net/pokemon/gen1pokemon.shtml') # In[30]: # First we'll get all of the pokemon # We will use css selectors to do this # opening up the chrome dev tools by hitting CMD + Option + i on Mac or you can right click inspect # understanding a little bit of css and html will help this process # We will get the table element from bs4 import BeautifulSoup page_source = BeautifulSoup(driver.page_source, 'html.parser') pokemon_table = page_source.select_one('table[class="dextable"]') # In[31]: # Now lets get all of the individual table rows pokemon = page_source.select('table[class="dextable"] tbody tr') # In[32]: # lets look at the first pokemon since the first two items are actually the table headers pokemon[2] # In[43]: # let's learn now how to start interacting with the page driver.get('https://www.random.org/integers/') # In[44]: # text actions # instead of using beautiful soup we will just use the selenium defaults to find the elements num_numbers = driver.find_element(By.CSS_SELECTOR, 'input[name="num"]') # In[45]: # get rid of current text num_numbers.clear() # insert new text num_numbers.send_keys('5') # In[47]: # let's click on the button submit_button = driver.find_element(By.CSS_SELECTOR, 'input[value="Get Numbers"]') submit_button.click() # In[48]: driver.close() # In[ ]: