2017-08-17 25 views
1

私は選択された運送会社(TNT)にオンライン注文を与えるタスクを自動化するPHP facebook/webdriver 1.4.1とセレニウム3.5のスクリプトをまとめようとしています。は、この機能を実現するための休憩APIを提供していないため、実際には手間をかけずに済ませることができます。セレブのドロップダウンから選択してくださいfacebookから

<ul><li>の束で生成されたjscriptであるSelectboxを除いて、スクリプトが正常に動作し、目的の値を選択できません。

これは<li> IDが生成され、自動、およびページ内の他のselectboxesに複製され、私はできない$driver->findElement(WebDriverBy::id('6'))->click();選択

<td> 
 
\t <select id="latestCollectionTime" name="latestCollectionTime" style="display: none;"> 
 
\t \t <option value="" selected="selected"> 
 
\t \t \t selecteer... 
 
\t \t </option> 
 
\t \t <option value="2230"> 
 
\t \t \t 22:30 
 
\t \t </option> 
 
\t \t <option value="2245"> 
 
\t \t \t 22:45 
 
\t \t </option> 
 
\t \t <option value="2300"> 
 
\t \t \t 23:00 
 
\t \t </option> 
 
\t \t <option value="2315"> 
 
\t \t \t 23:15 
 
\t \t </option> 
 
\t \t <option value="2330"> 
 
\t \t \t 23:30 
 
\t \t </option> 
 
\t \t <option value="2345"> 
 
\t \t \t 23:45 
 
\t \t </option> 
 
\t </select> 
 
\t <span id="latestCollectionTime-dropdown" class="selectboxit-container"> 
 
\t \t <span id="latestCollectionTimeSelectBoxIt" class="selectboxit dropdown-menu" style="" name="latestCollectionTime" tabindex="0" unselectable="on" role="combobox" aria-autocomplete="list" aria-expanded="false" 
 
\t \t aria-owns="latestCollectionTimeSelectBoxItOptions" aria-activedescendant="0" aria-label="" aria-live="assertive"> 
 
\t \t \t <i id="latestCollectionTimeSelectBoxItDefaultIcon" class="selectboxit-default-icon selectboxit-option-icon" unselectable="on" style="margin-top: 5.5px;"> 
 
\t \t \t </i> 
 
\t \t \t <span id="latestCollectionTimeSelectBoxItText" class="selectboxit-text" unselectable="on" data-val="" style="line-height: 22px; max-width: 88px;"> 
 
\t \t \t \t selecteer... 
 
\t \t \t </span> 
 
\t \t \t <span id="latestCollectionTimeSelectBoxItArrowContainer" class="selectboxit-arrow-container" unselectable="on" style="height: 22px;"> 
 
\t \t \t \t <i id="latestCollectionTimeSelectBoxItArrow" class="selectboxit-arrow caret" unselectable="on" style="margin-top: 7px;"> 
 
\t \t \t \t </i> 
 
\t \t \t </span> 
 
\t \t </span> 
 
\t \t <ul id="latestCollectionTimeSelectBoxItOptions" class="selectboxit-options" tabindex="-1" role="listbox" aria-hidden="true" style="max-height: 198px; top: auto; display: none;"> 
 
\t \t \t <li id="0" data-val="" data-disabled="false" class="selectboxit-option selectboxit-option-first" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>selecteer...</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="1" data-val="2230" data-disabled="false" class="selectboxit-option" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>22:30</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="2" data-val="2245" data-disabled="false" class="selectboxit-option" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>22:45</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="3" data-val="2300" data-disabled="false" class="selectboxit-option" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:00</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="4" data-val="2315" data-disabled="false" class="selectboxit-option" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:15</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="5" data-val="2330" data-disabled="false" class="selectboxit-option" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:30</a> 
 
\t \t \t </li> 
 
\t \t \t <li id="6" data-val="2345" data-disabled="false" class="selectboxit-option selectboxit-option-last" style="" role="option"> 
 
\t \t \t \t <a class="selectboxit-option-anchor"><i class="selectboxit-option-icon " style="margin-top: 3.5px;"></i>23:45</a> 
 
\t \t \t </li> 
 
\t \t </ul> 
 
\t </span> 
 
</td>
の一例です。

通常の方法では選択できません。

$driver->findElement(WebDriverBy::id('latestCollectionTime')) 
       ->findElement(WebDriverBy::cssSelector("option[value='11']")) 
       ->click(); 

選択が隠されているのはstyle="display: none;で、xpathではないためです。

I直接data-val="2330"ような値を選択できるようにしたいが、同じ<li> IDとデータ-VAL値を有するが、diferent <span><ul> IDを持つ他の3 selectboxesであることを考慮して取ることになります。

誰もがこれを手伝ってくれますか? ありがとうございます。

EDIT:たxPath sintaxと正しい方向に私を指しているため、以下の答えで@DANに再び

//make the dropdow list visible. 

$driver->findElement(WebDriverBy::id('latestCollectionTimeSelectBoxIt'))->click(); 

// to click in the desired option. 

$driver->findElement(WebDriverBy::xPath('//*[@id = "latestCollectionTimeSelectBoxItOptions"]/li[@data-val = "'.$desired_value.'"]'))->click(); 

ありがとう:

私はこのようにそれを使用して終了します。

答えて

0

最初に、重複するIDを持つ要素は有効なHTMLではなく、違法なページ要素で何を行うかを決めるのは各ブラウザの責任です。私が知る限り、ほとんどのブラウザでは重複したIDを持つ複数のDOM要素が許可されますが、これは将来変更される可能性があります。

第2に、興味のある要素をXPath経由で選択できます。たとえば、XPath (//*[@id = 'someID'])[1]は、IDがsomeIDの最初の要素を選択します。

ただし、<ul>要素のIDが異なると述べたので、//ul[@id = 'latestCollectionTimeSelectBoxItOptions']/li[@data-val = '2230']などのXPathを使用して要素を選択できます。

最後に、リストアイテムは、ドロップダウンメニューの一部であるため、非表示になっているためクリックできないと言います。

この場合、まずドロップダウンをクリックして開き、適切なリスト項目を見つけてクリックする必要があります。

これが役に立ちます。

+0

ありがとうございます! 私はこれを次のように使います: '$ driver-> findElement(WebDriverBy :: id( 'latestCollectionTimeSelectBoxIt')) - > click();'をクリックするとドロップドウンが表示され、 '$ driver-> findElement目的のオプションをクリックするには、WebDriverBy :: xPath( '// * [@ id = "latestCollectionTimeSelectBoxItOptions"]/li [@ data-val = "2230"]')) - >クリック(); もう一度、xPath sintaxを使用して、正しい方向にポイントしてくれてありがとう。 – Kuasarx

0

あなたが選択したいオプションをクリックするだけです。

私は与えられた選択ボックスからオプションにアクセスしたいので、xpathを使用しています。

$optionFromSelectBox = $driver->findElement(
    WebDriverBy::xpath(
     '//select[@id=\'latestCollectionTimeSe‌​lectBoxIt\']//option[2]' 
    ) 
); 

$optionFromSelectBox->click(); 
関連する問題