# make sure you have done pip install selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
# create the driver by passing in the path of the chromedriver
driver = webdriver.Chrome('/Users/mckayjohns/Downloads/chromedriver')
/var/folders/l3/wlgnl3zd5hl46kfjq1yh3k9r0000gn/T/ipykernel_62143/3319012562.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome('/Users/mckayjohns/Downloads/chromedriver')
# 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)
/var/folders/l3/wlgnl3zd5hl46kfjq1yh3k9r0000gn/T/ipykernel_62143/37542349.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome('/Users/mckayjohns/Downloads/chromedriver', options=options)
# Go to a webpage
driver.get('https://www.serebii.net/pokemon/gen1pokemon.shtml')
# 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"]')
# Now lets get all of the individual table rows
pokemon = page_source.select('table[class="dextable"] tbody tr')
# lets look at the first pokemon since the first two items are actually the table headers
pokemon[2]
<tr> <td align="center" class="fooinfo"> #001 </td> <td align="center" class="fooinfo"><table class="pkmn"><tbody><tr><td><a href="/pokemon/bulbasaur"><img border="0" loading="lazy" src="/swordshield/pokemon/small/001.png" style="height:120px"/></a></td></tr></tbody></table> </td> <td align="center" class="fooinfo"> <a href="/pokemon/bulbasaur">Bulbasaur</a> </td> <td align="center" class="fooinfo"> <a href="/pokemon/type/grass"><img border="0" src="/pokedex-bw/type/grass.gif"/></a> <a href="/pokemon/type/poison"><img border="0" src="/pokedex-bw/type/poison.gif"/></a> </td> <td align="center" class="fooinfo"> <a href="/abilitydex/overgrow.shtml">Overgrow</a> <br/><a href="/abilitydex/chlorophyll.shtml">Chlorophyll</a></td> <td align="center" class="fooinfo">45</td> <td align="center" class="fooinfo">49</td> <td align="center" class="fooinfo">49</td> <td align="center" class="fooinfo">65</td> <td align="center" class="fooinfo">65</td> <td align="center" class="fooinfo">45</td> </tr>
# let's learn now how to start interacting with the page
driver.get('https://www.random.org/integers/')
# 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"]')
# get rid of current text
num_numbers.clear()
# insert new text
num_numbers.send_keys('5')
# let's click on the button
submit_button = driver.find_element(By.CSS_SELECTOR, 'input[value="Get Numbers"]')
submit_button.click()
driver.close()