2015-11-02 137 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");