kyoob
kyoob

Reputation: 519

How do I return the text of a WebElement using Perl's Selenium::Remote::Driver?

I feel like I must be missing something obvious. I know the XPath to a WebElement and I want to compare the text in that element to some string.

Say the XPath for the element is /html/body/div/a/strong and I want to compare it to $str.

So it shows up in source like ...<strong>find this string</strong>

So I say

use strict;
use warnings;
use Selenium::Remote::Driver;

# Fire up a Selenium object, get to the page, etc..

Test::More::ok($sel->find_element("/html/body/div/a") eq $str, "Text matched");

When I run this the test fails when it should pass. When I try to print the value of find_element($xpath) I get some hash reference. I've googled around some and find examples telling me to try find_element($xpath)->get_text() but get_text() isn't even a method in the original package. Is it an obsolete method that used to actually work?

Most of the examples online for this module say "It's easy!" then show me how to get_title() but not how to check the text at an XPath. I might be going crazy.

Upvotes: 1

Views: 2005

Answers (1)

rutter
rutter

Reputation: 11452

Following up on our comment thread, I'm posting this as an answer:

Selenium::Remote::WebDriver definitely has a method named find_element(), and Selenium::Remote::WebElement definitely has a method named get_text().

Something like this...

my $text = $sel->find_element(...)->get_text();

...works fine on my end, though it looks like it'll error out if the element isn't found.

Upvotes: 4

Related Questions