问题
I am using Rspec and Watir-webdriver and am looking to get a count of all the elements that have been clicked.
What I'm doing:
I perform a search and get back a list of results; I then run a loop to select only 5 of them. The class name for these elements goes from asset-card selectable
to now saying asset-card selectable selected
.
I try to get a proper count by doing this:
count = @browser.elements(:class, 'selected').size
But this is still giving me all results and not specifically the ones that now have selected
as part of the class name.
Any thoughts on how I can accomplish this?
This is what the html looks like:
<div class="search results">
<div class="asset-card selectable selected"></div>
<div class="asset-card selectable selected"></div>
<div class="asset-card selectable"></div>
<div class="asset-card selectable"></div>
<div class="asset-card selectable"></div>
</div>
In this html example, the first two elements have been selected
回答1:
I feel rather dumb; I found a solution that fixes the issue of why I wasn't getting a proper count returned: it's because I wasn't waiting long enough
After I placed a sleep 1
duration, it returned a count accurate to what I was clicking on. My thought on this isn't exactly technical, but it was acting as if the click action and class name change was performing actions before a proper count could be determined.
I will be looking to place a more elegant wait
solution as I'd like to avoid having to use sleep
commands if possible
来源:https://stackoverflow.com/questions/37492341/watir-webdriver-counting-elements-with-changing-class-names