Created
December 6, 2023 14:44
-
-
Save dieissonmartins/3606c0d3ff685d8e1e6ff0b900d21449 to your computer and use it in GitHub Desktop.
php-webdriver
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
Base: https://dados.gov.br/dados/conjuntos-dados/cadastro-nacional-da-pessoa-juridica---cnpj | |
Lib: https://github.com/php-webdriver/php-webdriver | |
Instalação: | |
mkdir selenium-php-example | |
cd selenium-php-example | |
composer require php-webdriver/webdriver | |
Configuração: | |
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Facebook\WebDriver\Remote\RemoteWebDriver; | |
use Facebook\WebDriver\WebDriverBy; | |
$webDriver = RemoteWebDriver::create( | |
'http://localhost:4444/wd/hub', | |
\Facebook\WebDriver\Remote\DesiredCapabilities::chrome() | |
); | |
// Navegar até o Google | |
$webDriver->get('https://www.google.com'); | |
// Encontrar o campo de pesquisa e digitar algo | |
$searchBox = $webDriver->findElement(WebDriverBy::name('q')); | |
$searchBox->sendKeys('Selenium WebDriver PHP'); | |
// Submeter o formulário | |
$searchBox->submit(); | |
// Aguardar alguns segundos e fechar o navegador | |
sleep(5); | |
$webDriver->quit(); | |
Baixar arquivo: | |
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Facebook\WebDriver\Remote\RemoteWebDriver; | |
use Facebook\WebDriver\Remote\DesiredCapabilities; | |
use Facebook\WebDriver\WebDriverBy; | |
use Facebook\WebDriver\WebDriverExpectedCondition; | |
use Facebook\WebDriver\WebDriverWait; | |
use Facebook\WebDriver\WebDriverActions; | |
// Configurações do WebDriver | |
$host = 'http://localhost:4444/wd/hub'; | |
$capabilities = DesiredCapabilities::chrome(); | |
$driver = RemoteWebDriver::create($host, $capabilities); | |
try { | |
// Navegar até a página que contém o elemento para download | |
$driver->get('URL_DA_SUA_PAGINA'); | |
// Localizar o elemento que você deseja clicar | |
$element = $driver->findElement(WebDriverBy::id('ID_DO_SEU_ELEMENTO')); | |
// Clicar no elemento usando JavaScript Executor | |
$driver->executeScript('arguments[0].click();', [$element]); | |
// Esperar até que o download seja concluído | |
$wait = new WebDriverWait($driver, 10); | |
$wait->until(WebDriverExpectedCondition::titleContains('Download Concluído')); | |
// Usar Selenium Actions para simular a combinação de teclas Ctrl + J (ou Command + J no Mac) para abrir a página de downloads | |
$actions = new WebDriverActions($driver); | |
$actions->keyDown(null, 'Control')->sendKeys('j')->keyUp(null, 'Control')->perform(); | |
// Esperar até que o arquivo seja baixado (ajuste conforme necessário) | |
sleep(5); | |
// Mover o arquivo baixado para a pasta desejada | |
$downloadFolderPath = '/caminho/para/sua/pasta/de/download'; | |
$downloadedFiles = glob('/caminho/para/sua/pasta/de/download/*'); // Obter todos os arquivos na pasta de download | |
foreach ($downloadedFiles as $file) { | |
$newLocation = $downloadFolderPath . '/' . basename($file); | |
rename($file, $newLocation); | |
echo "Arquivo movido para: " . $newLocation . "\n"; | |
} | |
} finally { | |
// Fechar o navegador | |
$driver->quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment