美文网首页
模拟点击php webdriver

模拟点击php webdriver

作者: 空气KQ | 来源:发表于2020-01-15 00:21 被阅读0次

    谷歌浏览器驱动:
    https://sites.google.com/a/chromium.org/chromedriver/downloads

    selenium
    http://selenium-release.storage.googleapis.com/index.html

    php 包
    https://github.com/facebook/php-webdriver

    composer init
    

    java sdk 环境配置
    https://www.runoob.com/java/java-environment-setup.html

    composer require facebook/webdriver
    
    <?php
    
    namespace Facebook\WebDriver;
    use Facebook\WebDriver\Remote\DesiredCapabilities;
    use Facebook\WebDriver\Remote\RemoteWebDriver;
    require_once('vendor/autoload.php');
    // start Chrome with 5 seconds timeout
    $host = 'http://localhost:4444/wd/hub'; // this is the default
    $capabilities = DesiredCapabilities::chrome();
    $driver = RemoteWebDriver::create($host, $capabilities, 5000);
    // navigate to 'http://www.seleniumhq.org/'
    $driver->get('https://www.seleniumhq.org/');
    // adding cookie
    $driver->manage()->deleteAllCookies();
    $cookie = new Cookie('cookie_name', 'cookie_value');
    $driver->manage()->addCookie($cookie);
    $cookies = $driver->manage()->getCookies();
    print_r($cookies);
    // click the link 'About'
    $link = $driver->findElement(
        WebDriverBy::id('menu_about')
    );
    $link->click();
    // wait until the page is loaded
    $driver->wait()->until(
        WebDriverExpectedCondition::titleContains('About')
    );
    // print the title of the current page
    echo "The title is '" . $driver->getTitle() . "'\n";
    // print the URI of the current page
    echo "The current URI is '" . $driver->getCurrentURL() . "'\n";
    // write 'php' in the search box
    $driver->findElement(WebDriverBy::id('q'))
        ->sendKeys('php') // fill the search box
        ->submit(); // submit the whole form
    // wait at most 10 seconds until at least one result is shown
    $driver->wait(10)->until(
        WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
            WebDriverBy::className('gsc-result')
        )
    );
    // close the browser
    $driver->quit();
    

    cmd 监听

    java -Dwebdriver.chrome.driver="G:/chromedriver.exe" -jar selenium-server-standalone-4.0.0-alpha-1.jar
    

    相关文章

      网友评论

          本文标题:模拟点击php webdriver

          本文链接:https://www.haomeiwen.com/subject/soszactx.html