Created
October 26, 2021 16:05
-
-
Save luizomf/1163cce263b589d6cdb47d63f74e5145 to your computer and use it in GitHub Desktop.
Código selenium para curso de Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from selenium import webdriver | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
from time import sleep | |
# Caminho para a raiz do projeto | |
ROOT_FOLDER = Path(__file__).parent.parent.parent | |
# Caminho para a pasta onde o chromedriver está | |
CHROME_DRIVER_PATH = ROOT_FOLDER / 'bin' / 'chromedriver' | |
def make_chrome_browser(*options: str) -> webdriver.Chrome: | |
chrome_options = webdriver.ChromeOptions() | |
# chrome_options.add_argument('--headless') | |
if options is not None: | |
for option in options: | |
chrome_options.add_argument(option) | |
chrome_service = Service( | |
executable_path=CHROME_DRIVER_PATH, | |
) | |
browser = webdriver.Chrome( | |
service=chrome_service, | |
options=chrome_options | |
) | |
return browser | |
if __name__ == '__main__': | |
# Example | |
options = ('--disable-gpu', '--no-sandbox',) | |
browser = make_chrome_browser(*options) | |
# Como antes | |
browser.get('https://www.google.com') | |
input_element = browser.find_element(By.NAME, 'q') | |
input_element.send_keys('Python') | |
sleep(3) | |
input_element.send_keys(Keys.ENTER) | |
sleep(3) | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment