如何传递存储在变量中的xpath?

时间:2012-06-27 07:11:19

标签: perl selenium selenium-rc xpath

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]每次迭代都会{1}}。

以下代码段执行替换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的方式?我该怎么办?

1 个答案:

答案 0 :(得分:2)

比较XPath字符串with mine in the previous answer的定义。您在不属于的商业广告前插入了反斜杠。