Skip to content

Instantly share code, notes, and snippets.

@Canorus
Created October 26, 2020 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Canorus/f3ebb47e67be5de4e10932ded6c71640 to your computer and use it in GitHub Desktop.
Save Canorus/f3ebb47e67be5de4e10932ded6c71640 to your computer and use it in GitHub Desktop.
selenium chromedriver sample for beginners
# download chromedriver matching your chrome browser version from here https://chromedriver.chromium.org and place it with this Python file
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os
base = os.path.join(os.path.dirname(os.path.abspath(__file__)),'')
user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/7.0.4 Mobile/16B91 Safari/605.1.15"
# setup options
options = webdriver.ChromeOptions()
options.add_argument('--headless') # headless mode
options.add_argument('window-size=1920x1080')
options.add_argument('user-agent='+user_agent)
browser = webdriver.Chrome(executable_path=base+'chromedriver', options=options)
browser.get('https://google.com')
browser.find_element_by_id('daechoong_id').click() # caution: element vs elements
browser.find_elements_by_tag_name('daechoong_tag_name').send_keys('any strings') # refer to https://selenium-python.readthedocs.io/locating-elements.html
r = browser.page_source
# use BeautifulSoup for better parsing
from bs4 import BeautifulSoup as bs
r_bs = bs(browser.page_source, 'html.parser')
ids_ = r_bs.find_all('div', id_='daechoong_id_here')
# you can search for elements easily
# but you won't be able to interact with elements on browser
# with the search result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment