arcy
arcy

Reputation: 13133

getting text of div element with selenium

I am using Cucumber, with Ruby and Selenium. I've got the following HTML:

<html>
<body>
  <div class="breadcrumbtrail" id="breadcrumbtrail">
    home
  </div>
</body>
</html>

and the following step definition

  begin
    puts "got to bct step"
    bctValue = @driver.find_element(:class_name, "breadcrumbtrail").getText()
    puts "got bctValue value of #{bctValue}"
    assert(crumb == bctValue, "breadcrumbtrail #{bctValue}, not #{crumb}")
  end

My output shows that the step is being run; I had tried getting an attribute of "innerText" from the div at one point, that appeared to give me the name of the div.

I've tried various things after find_element -- this one tells me undefined method getText. What should I use in a ruby step definition to get the text within that div? And does anyone have a suggestion for documentation on this? The only one I found that was close went through hundreds of lines, many for each language supported, which made it difficult to find what I needed.

Upvotes: 1

Views: 6938

Answers (3)

Mrityunjay Ranjan
Mrityunjay Ranjan

Reputation: 1

you can use below lines of code that will fix your issue.

bctValue = @driver.find_element(:class_name, "breadcrumbtrail").text

Upvotes: 0

arcy
arcy

Reputation: 13133

It turns out that the correct method on Selenium::WebDriver:Element is "text()", not getText(). Now that I've changed it to "text", it works fine.

Upvotes: 1

ddavison
ddavison

Reputation: 29052

You are using ruby. Ruby doesn't use camelCasing.

Change this line:

bctValue = @driver.find_element(:class_name, "breadcrumbtrail").getText()

to

bctValue = @driver.find_element(:class_name, "breadcrumbtrail").get_text

Also, don't use :class_name if you have an id...

bctValue = @driver.find_element_by_id('breadcrumbtrail').get_text

Upvotes: 0

Related Questions