1

I'm fairly new to using Selenium Remote Driver and Perl. What I'd like to do is to have Selenium find all elements on a page using a partial match of text. Then store the full text of those elements into an array.

I've tried using:
@elements = $driver->find_elements("//tbody/tr[td[2]/div/span[2][contains(text(),'matching text')]]")->get_text; However, this doesn't seem to work.

I've also tried: @elements = $driver->find_elements("//tbody/tr[td[2]/div/span[2][contains(text(),'matching text')]]"); This does populate the array with webelements.

my @elements;
my @elementtext;
my $elementtext;
@elements = $driver->find_elements("//tbody/tr[td[2]/div/span[2][contains(text(),'matching text')]]");
foreach my $currentelement (@elements) {
  $elementtext = $driver->find_element($currentelement)->get_text();
  push @elementtext, $elementtext;
}

This causes perl to generate an error because webdriver can't find the element. Any ideas on what I'm doing wrong and how to fix it? I suspect that the problem is with the contents of the @elements array not actually being xpath elements.

Here is an example of the html:

<td>
<div class='cellContent'><a href="/link/1">Atlanta</a></div>
</td>
<td>
<div class='cellContent'>City</div>
</td>
<td>
<div class='cellContent'>Georgia</div>
</td>
<td class='sort_column'>
<div class='cellContent'>USA</div>
</td>
</tr>
<tr>
<td>
<div class='cellContent'><a href="/travelers/1">Joe Passenger</a></div>
</td>
<td>
<div class='cellContent'>        <span>NFL</span>
        <span>matching text: Atlanta.Falcons.team</span>
</div>
</td>

I want to get 'matching text: Atlanta.Falcons.team' stored into the array.

Thornsong
  • 11
  • 2

1 Answers1

0

you can directly point to span tag using this xpath.

//tbody/tr/td/div/span[contains(text(),'matching text')]
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
  • @elements = $driver->find_elements("//tbody/tr/td/div/span[contains(text(),'matching text')]"); This command will store all of the matching elements into the array as Selenium Remote Webelement hashes. How do I get the matching text from them? – Thornsong Jul 19 '19 at 18:48