Perl 从下拉菜单中查找元素
这是关于Perl的CPAN中的WWW::Selenium模块的。我在下面的HTML中查找Perl 从下拉菜单中查找元素,perl,selenium,Perl,Selenium,这是关于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
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>
上面的代码可以工作,但在实际测试中,我需要根据textBigBroLot1446130409
而不是value=497
查找元素
<option value="497">
BigBroLot1446130409
- 0g
(100 credits to list)
</option>
任何帮助都将不胜感激。如前所述,我想从下拉菜单中根据变量中的“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_定位器可以是:
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\u select\u options
函数:
$locator = q{//select[@id="lot_id" and @name="lot_id"]};
my @array = $sel->get_select_options($locator);
对于GRIN,我想验证@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
函数中所述,我可以使用索引查找元素,因此在列表::MoreUtils
模块的帮助下,我将其用作$sel->select
函数的选项定位器。例如,假设$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");
@ikegami我不会使用正则表达式,我已经改写了问题,谢谢。
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");