my $V1 = $sel->get_text("//body[\@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr/td/div/div");
print($V1 . "\n");
当我执行上述命令时,我可以毫无错误地打印 v1。
我的目标是打印这些值(比如 20 左右)。但是每个变量的 xpath 仅因div
元素而异(即,在每次迭代中,div
元素前面table
应该被替换div[2],
并且div[2]
应该被替换。div[3]
以下代码段执行替换 div[] 元素。但是说是无效的xpath。
my $xpath = q(//body[\@id='ext-gen3']/div[14]/div[2]/div/div/div/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr/td/div/div);
for my $i (0,2)
{
my @nodes = split qr'/', $xpath;
$nodes[16] .= "[$i]" unless 0 == $i;
#say join '/', @nodes; }
my @somenames = join('/',@nodes);
#print "\n";
#print @somenames ;
foreach my $val (@somenames) {
chomp($val);
#my @new_arr = $sel->get_text($val);
print "\n";
print $val; ***this prints the xpath value which i needed***
my $V1 = $sel->get_text("$val"); ***#i'm passing the same xpath here within double quotes but says as invalid xpath***
print($V1 . "\n");
my @new_arr = $sel->get_text("$val");
}
}
我是否按照错误的方式传递 xpath ?我该怎么做 ?