Quantcast
Channel: 日本語訳タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 236

【PHP】selenium_チートシート (Facebook/webdriver)

$
0
0
Webdriver PHP API 使い方 こちらを日本語訳したものです。訳が変なところあると思いますが、温かい目で見て頂けると幸いです。 ブラウザを開く selenium-webdriverを使ってFirefoxのインスタンスをスタートする $browser_type = 'firefox' $host = 'http://localhost:4444/wd/hub' $capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => $browser_type); $driver = RemoteWebDriver::create($host, $capabilities); $browser_type # :firefox => firefox # :chrome => chrome # :ie => iexplore 指定された URL に移動 $driver->get('http://google.com'); $driver->navigate()->to('http://google.com'); '注意' -- WebDriverはページの読み込みを待機しない場合があります。明示的および暗黙的な待機を使用することをお勧めします。 要素の検索 'findElement(WebDriverBy $by)' -- 指定された引数に一致する最初の要素を検索します。 'findElements(WebDriverBy $by)' -- 指定された引数に一致するすべての要素を検索します。 'WebDriverBy' -- さまざまなメカニズムを使用して要素を選択するための静的メソッドを持つクラス。 'WebDriverBy 'class name' 'css selector' 'id' 'name' 'link text' 'partial link text' 'tag name' 'xpath' IDによる要素の検索 # example html # <input id="q">...</input> $element = $driver->findElement(WebDriverBy::id("q")); Class Nameによる要素の検索 # example html # <div class="highlight-java" style="display: none; ">...</div> $element = $driver->findElement(WebDriverBy::className('highlight-java')); Tag Nameによる要素の検索 # example html # <div class="highlight-java" style="display: none; ">...</div> $element = $driver->findElement(WebDriverBy::tagName("div")); Nameによる要素の検索 # example html # <input id="q" name='search' type='text'>…</input> $element = $driver->findElement(WebDriverBy::name('search')); Link Textによる要素の検索 # example html # <a href="http://www.google.com/search?q=cheese">cheese</a> $element = $driver->findElement(WebDriverBy::linkText("cheese")); Partial Link Textによる要素の検索 # example html # <a href="http://www.google.com/search?q=cheese">search for cheese</a> $element = $driver->findElement(WebDriverBy::partialLinkText("chee")); XPathによる要素の検索 # example html # <ul class="dropdown-menu"> # <li><a href="/login/form">Login</a></li> # <li><a href="/logout">Logout</a></li> # </ul> $element = $driver->findElement(WebDriverBy::xpath('//a[@href='/logout']')); NOTE -- When using Element#findElement with WebDriverBy::xpath, を使用する場合は、次の点に注意してください, webdriver webdriverは標準規則に従います。: 接頭辞 "//" を検索する場合、この現在のノードの子だけでなく、ドキュメント全体が検索されます。 ".//" を使用して、検索を受信要素の子に限定します。 CSS Selectorによる要素の検索 # example html # <div id="food"> # <span class="dairy">milk</span> # <span class="dairy aged">cheese</span> # </div> $element = $driver->findElement(WebDriverBy::cssSelector('#food span.dairy')); 要素の操作 ボタン・リンク・画像(Button/Link/Image) $element = $driver->findElement(WebDriverBy::id('BUTTON_ID'))->click(); テキストボックス # テキスト入力 $driver->findElement(WebDriverBy::id('BUTTON_ID'))->click(); $driver->getKeyboard()->sendKeys('InputText'); # `cmd+a` & `delete`をキーボードで入力し送信する。 $driver->getKeyboard() ->sendKeys(array(WebDriverKeys::COMMAND, // Use control on non-mac computers. 'a',)); $driver->getKeyboard() ->sendKeys(array(WebDriverKeys::BACKSPACE,)); 'メモ' -- /RemoteWebElement->clear() は、テキスト領域またはテキスト入力からテキストをクリアします。 チェックボックス・ラジオボタン(Checkbox/Radio) # チェックする場合 $driver->findElement(WebDriverBy::id('CheckBox'))->isSelected(); # 要素を選択する場合。 $driver->findElement(WebDriverBy::id('CheckBox'))->click(); # 要素を選択しない場合。 $driver->findElement(WebDriverBy::id('CheckBox'))->clears(); 選択 # get the select element $select = $driver->findElement(WebDriverBy::tagName('select')); # get all the options for this element $allOptions = $select->findElement(WebDriverBy::tagName('option')); # select the options foreach ($allOptions as $option) { echo "Value is:" . $option->getAttribute('value); $option->click(); } visibility(視認性) $driver->findElement(WebDriverBy::id('element'))->isDisplayed(); テキスト取得 $driver->findElement(WebDriverBy::id('element'))->getText(); 属性の取得 $driver->findElement(WebDriverBy::id('element'))->getAttribute('class'); Driver's operation javascriptを実行する example.php $driver->executeScript("return window.location.pathname"); 特定の要素が表示されるのを待つ # set the timeout to 20 seconds, and the time in interval to 1000 ms $driver->wait(20, 1000)->until( WebDriverExpectedCondition::titleIs('WebDriver Page') ); implicit waits An implicit wait とは、1つまたは複数の要素がすぐに利用できない場合に、それらを検索しようとするときに、一定時間DOMをポーリングするようにWebDriverに指示することです。 # set the timeout for implicit waits as 10 seconds $driver->manage()->timeouts()->implicitlyWait = 10 $driver->get("http://somedomain/url_that_delays_loading"); $element = $driver->findElement(WebDriverBy::id('some-dynamic-element')); フレームを切り替える # switch to an iframe $driver->switchTo()->frame(/WebDriverElement('the id or the name of the frame"some-frame" # name or id')); # switch back to the main document $driver->switchTo()->defaultContent(); * ウィンドウを切り替える #TODO: Add examples. * javascriptダイアログを扱う # get the alert $a = $driver->switchTo->alert(); # operation on the alert if ($a->getText == 'A value you are looking for') { a->dismiss(); } else { a->accept(); } クッキー(Cookies) クッキーの削除 # 削除には2種類の方法があります。 # By name $driver->manage()->deleteCookieNamed('CookieName'); # Or all of them $driver->manage()->deleteAllCookies(); 参考サイト

Viewing all articles
Browse latest Browse all 236

Trending Articles