Selenium - Selecionando elementos com By, expected_conditions e WebDriverWait

This commit is contained in:
Luiz Otávio
2023-02-07 09:08:33 -03:00
parent 61fc09d56c
commit 136274d6b6

View File

@@ -1,12 +1,18 @@
# type: ignore
# Selenium - Automatizando tarefas no navegador # Selenium - Automatizando tarefas no navegador
from pathlib import Path from pathlib import Path
from time import sleep from time import sleep
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# Chrome Options # Chrome Options
# https://peter.sh/experiments/chromium-command-line-switches/ # https://peter.sh/experiments/chromium-command-line-switches/
# Doc Selenium
# https://selenium-python.readthedocs.io/locating-elements.html
# Caminho para a raiz do projeto # Caminho para a raiz do projeto
@@ -21,7 +27,7 @@ def make_chrome_browser(*options: str) -> webdriver.Chrome:
# chrome_options.add_argument('--headless') # chrome_options.add_argument('--headless')
if options is not None: if options is not None:
for option in options: for option in options:
chrome_options.add_argument(option) # type: ignore chrome_options.add_argument(option)
chrome_service = Service( chrome_service = Service(
executable_path=str(CHROME_DRIVER_PATH), executable_path=str(CHROME_DRIVER_PATH),
@@ -36,6 +42,8 @@ def make_chrome_browser(*options: str) -> webdriver.Chrome:
if __name__ == '__main__': if __name__ == '__main__':
TIME_TO_WAIT = 10
# Example # Example
# options = '--headless', '--disable-gpu', # options = '--headless', '--disable-gpu',
options = () options = ()
@@ -44,5 +52,13 @@ if __name__ == '__main__':
# Como antes # Como antes
browser.get('https://www.google.com') browser.get('https://www.google.com')
# Espere para encontrar o input
search_input = WebDriverWait(browser, TIME_TO_WAIT).until(
EC.presence_of_element_located(
(By.NAME, 'q')
)
)
search_input.send_keys('Hello World!')
# Dorme por 10 segundos # Dorme por 10 segundos
sleep(10) sleep(TIME_TO_WAIT)