问题描述:

<div class="wrapper">

<div id="minHeightBlock" style="min-height: 430px;">

<div class="borderbox"><div class="standaloneBox">

<div class="sysHeaderContainer clearfix"> … </div>

<div class="notesForGuests"> … </div>

<div class="filterBox clearfix"> … </div>

<div class="resListHeader"> … </div>

<div id="corporaContainer" class="fullList">

<div id="c-a06ffa6a-dc62-4640-9760-dbd661c7ffe8" class="resItem clearfix">

<div class="resTitle">

<span id="filter-empty" class="statBall statFile empty" title="Status: Empty corpus"></span>

<span class="theText">

12321 corpora

</span>

</div>

<div class="resType"> … </div>

<div class="resSize"> … </div>

<div class="resPermission private"> … </div>

<div class="resDomain"> … </div>

<div class="resDescr"> … </div>

<div class="resDetails clearfix" style="display:none;"> … </div>

</div>

<div id="c-b8c0faba-e662-4998-836f-0ee58009b7fa" class="resItem clearfix"> … </div>

<div id="c-9d02b887-4835-4606-ad4b-775b39af9f48" class="resItem clearfix"> … </div>

<div id="c-021d3ba1-db03-4c4e-81a5-294737eb5b54" class="resItem clearfix"> … </div>

This is the code of the webpage im trying to script using Watir. All i know is only the what kind of span text the element should contain. I have many of these elements and i need to colect all of the element ID values so i can use them in further actions.

I have comented the places in the above code what i know and what i need to get.

So far i have tried this code:

 @b.div(:id, "pageHeader").link(:text, "Corpora").click

sleep 5

@b.div(:id, "corporaContainer").spans(:text => /TestAuto\s.*/).each do |span|

puts span.parent.attribute_value("id")

end

But no output is done. Maybe im doing something wrong. Help me get this nut shell cracked.

网友答案:

Your attempt was close. The problem is that span.parent only goes up to the <div class="resTitle">. You need to go up one more parent:

@b.div(:id, "corporaContainer").spans(:text => /corpora/).each do |span|
  puts span.parent.parent.attribute_value("id")
end

(Note that I changed the text in the locator of the spans since TestAuto\s.* did not match the sample html.)

Alternatively, I sometimes find it better to find the divs that contain the span. This way you do not have to worry about the number of parents changing:

p @b.divs(:class => 'resItem')
  .find_all { |div| div.span(:text => /corpora/).exists? }
  .collect { |div| div.id }
#=> ["c-a06ffa6a-dc62-4640-9760-dbd661c7ffe8"]

Below is a working example. Note that there are 2 important things:

  • The list of results is loaded asynchronously. Therefore you need to wait for the list to finish loading before capturing the results. sleep(5) might work, but you are better off using an actual wait method (since it seems to take longer than 5 seconds).
  • Make sure the search text actually exists on the page. In the below example, there is no "12321 corpora" title that was mentioned in the sample html.

Example:

require 'watir-webdriver'

# Title to search for:
title_text = /UniAdm/

# Go to the Corpora page:
@b = Watir::Browser.new :ff
@b.goto "https://www.letsmt.eu/Corpora.aspx"

# Wait for the results to load:
container = @b.div(:id, "corporaContainer")
container.div(:class => 'resItem').wait_until_present

# Find the matching ids:
p container.divs(:class => 'resItem')
  .find_all { |div| div.span(:class => 'theText', :text => title_text).exists? }
  .collect { |div| div.id }
#=> ["c-87ee80a9-e529-48b2-92be-bc8d76375478", "c-f139e781-4789-41f9-82e8-914e0e3eff81", "c-e17641d2-9364-4e87-9047-ba35580dc32f"]
相关阅读:
Top