在OSX和Firefox上使用perl 5.22和Selenium :: Remote :: WebElement :: VERSION ='0.2701'。第一次嘗試使用Selenium(也是XPath!)。爲什麼find_child_elements忽略Perl Selenium中的第一個參數?
我的問題出現了,當我試圖讓一個給定的「<選擇>」的孩子,然後枚舉所有其「<選項」> S作爲這樣的:
use strict;
use warnings;
use Selenium::Remote::Driver;
use Selenium::Remote::WebElement;
use URI::file;
my $extraCaps = {"browser" => "Firefox", "browser_version" => "42.0","os" => "OSX","os_version" => "7","resolution" => "1024x768"};
my $driver = new Selenium::Remote::Driver('remote_server_addr' => 'localhost', 'port' => '4444','extra_capabilities' => $extraCaps);
$driver->get(URI::file->new_abs('./test.html')->as_string);
sleep(2);
# get <select> named 'SEL2':
my $sel2 = $driver->find_element('//select[@name="SEL2"]');
# this works, but it's not what i want: my $sel2_options = $driver->find_child_elements($sel2, '//select[@name="SEL2"]//option');
# get all the options for **that specific** <select>
my $sel2_options = $driver->find_child_elements($sel2, '//option');
foreach my $ap (@$sel2_options){
print "found option ".$ap->get_text()."\n";
}
sleep(1);
$driver->quit();
輸入HTML文件(test.html中)是:
<!DOCTYPE html>
<head></head>
<body>
<select name="SEL1" id="ID1">
<option value="SEL1-option11">SEL1-option1</option>
<option value="SEL1-option12">SEL1-option2</option>
<option value="SEL1-option13">SEL1-option3</option>
</select>
<select name="SEL2" id="ID2">
<option value="SEL2-option1">SEL2-option1</option>
<option value="SEL2-option2">SEL2-option2</option>
<option value="SEL2-option3">SEL2-option2</option>
</select>
</body></html>
和輸出我得到的是:
found option SEL1-option1
found option SEL1-option2
found option SEL1-option3
found option SEL2-option1
found option SEL2-option2
found option SEL2-option2
不應該我只獲得 SEL2列出的選項?
我做錯了什麼?在我看來,對於這種情況,find_child_elements忽略了它的第一個參數 - 據我瞭解 - WebElement將指定的選擇器(//選項)應用於它。
在另一方面,這正常工作,但它在某種程度上違背了目的:
my $sel2_options = $driver->find_child_elements($sel2, '//select[@name="SEL2"]//option');
千恩萬謝, bliako
嘗試'我的$ sel2_options = $ sel2-> find_elements( '//選項');' –
不,在我係統中,sel2(Selenium :: Remote :: WebElement)中沒有方法'find_elements' – bliako
我懷疑問題是'$ sel2'(也許它是空的?)。你不需要在xpath中轉義'@',即:'my $ sel2 = $ driver-> find_element('//選擇[\ @ name =「SEL2」]');'?示例至少如此說明:http://search.cpan.org/~chowes/Selenium-Remote-Driver-0.17/lib/Selenium/Remote/Driver.pm#find_child_elements –