2015-11-02 135 views
0

這涉及到Perl的CPAN中的WWW :: Selenium模塊。我在下面的HTML中發現問題BigBroLot1446130409。這是一個下拉菜單。從下拉菜單中查找元素

下面是HTML

<select name="lot_id" id="lot_id" title=""> 
<option value="">Select an Available Lot</option> 


<option value="497"> 
    BigBroLot1446130409 
    - 0g 
    (100 credits to list) 
</option> 

<option value="500"> 
    BigBroLot1446133752 
    - 199g 
    (100 credits to list) 
</option> 

當我使用此代碼,它actutally工作。

$locator = q{//select[@id="lot_id" and @name="lot_id"]}; 
$ret = $sel->wait_for_element_present($locator, $timeout); 
$ret = $sel->select($locator, "value=497"); 

上述作品,但在實際測試的代碼,但我需要基於文本BigBroLot1446130409而不是value=497找到的元素。

<option value="497"> 
    BigBroLot1446130409 
    - 0g 
    (100 credits to list) 
</option> 

這是一個下拉菜單,所以我想我需要使用下面的函數從文檔:

$sel->select($select_locator, $option_locator) 

任何幫助將不勝感激。

+0

@ikegami,我已經改寫了問題, 謝謝。 – BioRod

回答

0

如前所述,我想從基於來自變量的'BigBroLotXXXXXX'名稱的下拉菜單中進行選擇。在模塊的文檔有一個叫

$sel->get_select_options($select_locator) 

Gets all option labels in the specified select drop-down. 
$select_locator is an element locator identifying a drop-down menu 
Returns an array of all option labels in the specified select drop-down. 

所以,我能夠知道,從下拉菜單中選擇我的所有選擇是在一個陣列功能。

的功能選擇從下拉菜單中的元素是:

$sel->select($select_locator, $option_locator) 

是$ option_locator可以爲這個模塊狀態的文檔:

label=labelPattern:matches options based on their labels 

label=regexp:^[Oo]ther 

value=valuePattern:matches options based on their values. 

id=id:matches options based on their ids. 

index=index:matches an option based on its index (offset from zero). 

首先,我需要定義定位器的地方我的選擇陣列是這樣我可以餵它進入get_select_options功能:

$locator = q{//select[@id="lot_id" and @name="lot_id"]}; 

my @array = $sel->get_select_options($locator); 

對於笑容,我想驗證@array也有選擇在其中:

Select an Available Lot 
BigBroLot1446130409 - 0g (100 credits to list) 
BigBroLot1446087714 - 0g (100 credits to list) 
BigBroLot1446665592 - 36g (100 credits to list) 
BigBroLot1446060974 - 0g (100 credits to list) 
BigBroLot1446668987 - 64g (100 credits to list) 

正如$sel-select功能說明,我可以找到使用索引的元素,所以我使用了我的option_locator爲$sel->select函數List::MoreUtils模塊的幫助。例如可以說$xyz = BigBroLot1446130409

use List::MoreUtils qw(first_index); 
use 5.010; 
my ($index) = grep { $array[$_] =~ /$xyz/ } (0 .. @array-1); 

這裏是我的選擇元件從下拉菜單中的代碼:我不會使用正則表達式

$locator = q{//select[@id="lot_id" and @name="lot_id"]}; 
$ret = $sel->select($locator,"index=$index");