I am trying to access sequential elements and don't know how to advance the the script down the page. So, here is a snippet of the code.
<div class="card highlighted">
<div class="card-fix">
<a class="card-remove icon" title="Don't show member again" onclick="$jq( '#search-results' ).msg( 'remove', this )" data-userid="93872246">
X
</a>
<dl></dl>
<div class="card-actions">
<a class="quickview" title="Quick view" onclick="$jq('#dialog-profile').msg('show', '/profileinfo/SearchProfi…lTseEdmEQ==&lid=1&searchType=S&pageNumber=1'); return false;" href="#"></a>
<a class="button button-primary" title="Favorite her!" href="/matchbook/addEntry.aspx?uid=Mu/BwYPn0nxhWlTseEdmEQ==&pn=1&rn=4&tp=S&handle=Annie5170&lid=1065"></a>
</div>
</div>
<div class="card-fix">
<a class="card-remove icon" title="Don't show member again" onclick="$jq( '#search-results' ).msg( 'remove', this )" data-userid="108272583"></a>
<dl></dl>
<div class="card-actions">
<a class="quickview" title="Quick view" onclick="$jq('#dialog-profile').msg('show', '/profileinfo/SearchProfi…DA3R3daaQ==&lid=1&searchType=S&pageNumber=1'); return false;" href="#"></a>
<a class="button button-primary" title="Favorite her!" href="/matchbook/addEntry.aspx?uid=y98x/Mj+hc+61DA3R3daaQ==&pn=1&rn=4&tp=S&handle=dancer4498&lid=1065"></a>
</div>
</div>
In other words I need to continue looping through these quickviews and then running a small script and doing it all again. Then go to the next page and do it again...etc. However, if I use this now in a loop it will only pick up the first quickview 5 times.
# 5.times do |n|
# browser.link(:title => 'Quick view').click
# sleep (2)
# browser.link(:class => "icon dialog-abandon").click
# # sleep (2)
# end
So my question is how can I have it advance through all of the quick views which there are 18 per page.
It would also be great to be able to save the
data-user-id="93872246"
and the portion after the handle in the href, ie.
handle=Annie5710
in a file in a hash as well.
Thanks and I appreciate all of the help.
You have to do this:
//get total title in a page
totalTitleSize=browser.link(:title => 'Quick view').size
and looping it:
totalTitleSize.times do |n|
browser.link(:title => 'Quick view', :index=> n).click
sleep (2)
//test it or use index in it If you need
browser.link(:class => "icon dialog-abandon").click
sleep (2)
end
this should be added to the code.
browser.link(:title => 'Quick view', :index=> n).click
now everything works fine.
As the other answers show, the problem is that the line:
browser.link(:title => 'Quick view').click
Will always click the first matching link. Using the :index allows you to override the default first match. However, I would argue the better solution is to get a collection of matching links and iterate through that. It will save you from manually handling the index.
browser.links(:title => 'Quick view').each do |link|
link.click
sleep (2)
browser.link(:class => "icon dialog-abandon").click
sleep (2)
end
Notice here that we have created a collection using links that we iterate though using each.
However, since you also want to collect other data, you might want to iterate through the parent divs:
browser.divs(:class => 'card-fix').each do |parent_div|
puts parent_div.link(:class => 'card-remove').data_userid
#=> "93872246"
href = parent_div.button(:class => 'button-primary').href
puts href[/handle=([^&]+)/, 1]
#=> "Annie5170"
parent_div.link(:class => "quickview").click
sleep (2)
browser.link(:class => "icon dialog-abandon").click
sleep (2)
end
Related
I'm making a website using ror and I have a list of fandoms, and I want to display different pictures and writing for that fandom. Right now, just for testing, I'm having it just display the name of the fandom at the top of the page, but no matter which one I click on, it only displays the name of the first fandom. Here's the parts of the files I think are relevant, but if you need to see more, I'll post it.
Section of routes.rb:
get 'fandoms' => 'fandoms#index'
get 'fandoms/new' => 'fandoms#new'
post 'fandoms' => 'fandoms#create'
get 'fandoms/:id' => 'fandoms#show', as: :fandom
resources :fandoms
The show method in fandoms_controller:
def show
#fandom = Fandom.find_by(params[:id])
end
The index.html.erb for fandoms:
<div class="main">
<div class="container">
<h2>Fandoms</h2>
<%= link_to "New Fandom (dev only)", "fandoms/new" %>
<% #fandoms.each do |fandom| %>
<div class="fandoms">
<%= link_to fandom.name, fandom_path(fandom) %>
</div>
<% end %>
</div>
And finally the show.html.erb for fandoms:
<div class="main">
<div class="container">
<h2>Fandoms</h2>
<div class="fandom">
<h1><%= #fandom.name %></h1>
</div>
</div>
</div>
I double checked my code with the Ror tutorial on codecademy, but I still couldn't find any differences for what I wanted to do.
Thanks.
Your show action has a subtle typo that's breaking your logic:
#fandom = Fandom.find_by(params[:id])
Should actually be:
#fandom = Fandom.find(params[:id])
The difference is, find_by will return the first row for which the conditions passed are met. You're not actually passing any conditions; just an ID, which in Postgres doesn't even work:
irb> Fandom.find_by(1)
PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer
LINE 1: SELECT "fandoms".* FROM "fandoms" WHERE (1) LIMIT 1
Your database is more permissive, but seems to be treating it just as a WHERE TRUE clause and thus every single row meets the condition. As such, it returns the first row every time. Using find(id) will give you the desired result.
I am unable to select a list element when I click on the link I get the list to appear which should make the style display block visible however watir cannot find the element and times out.
Here is a sample of the code just showing one list item but they are all the same. The unique aspect would be the text. "Machine Translation"
<a id="addQuality" data-toggle="tooltip" data-placement="top" title="" data-original-title="Add New Output Quality">
<ul id="addQualityList" class="dropdown-menu" style="display: none;">
<li id="3d3c4316-b8ad-45bf-b8be-03ee69452deb" class="dropdown-menu-list addQualityTabItem add-quality-dropdown" data-url="/Client/AddContractTranslationQuality?id=255b9627-5452-4b6a-af46-084af684ff27&qualityId=3d3c4316-b8ad-45bf-b8be-03ee69452deb&prefix=Contracts[43c13738-abc4-42e1-89b1-7211d581e4be]">Machine Translation</li>
#b.link(:id => "addQuality").click # This will display the list
#b.select_list(:id => "addQualityList").option(:text => "Presentation").when_present.select
and I tried...
#b.select_list(:id => "addQualityList").when_present(2).click
I was able to come up with a solution. If anyone has another that is more elegant please post. Thanks.
#b.link(:id => "addQuality").click
#b.div(:class => "col-md-12").ul.li(:text => "Publication").click
I am new to Watir and Cucumber, and I am trying to run an automation to create Live IDs. Below is the HTML for the link that I want to click, the "New" text is what it is showing on webpage. It would lead me to the form to add new contact to my Live account.
<ul class="c_cc" role="presentation" styple="overflow:visible;">
<li class="c_sm c_mcp" id = "new">
<a id href="#" class="c_nobdr t_prs">
<span class="is_c" dir="ltr" style="padding-right: 5px;">
<img class="is_img" src="https://p.pfx.ms/is/invis.gif" onload="this.onload=null;$Do.when('$IS.Init',0,this);" style="width:26px;height:26px;background-position:-1px -1px;background-image:url('https://p.pfx.ms/h/command4.png');" alt="New contact" title />
</span>
"New"
</a>
<span class="c_ms"></span>
</li>
</ul>
the watir code I wrote to click the "New" is below:
#browser.div(:id, "c_header").div(:id, "c_cb0").ul(:class, "c_cc").span(:text, "is_c").when_present.click
I get this error:
Watir::Wait::TimeoutError: timed out after 30 seconds, waiting for {:id=>"is_c", :tag_name=>"span"} to become present
Then I tried below code:
#browser.div(:id, "c_header").div(:id, "c_cb0").ul(:class, "c_cc").span(:text, "New").when_present.click
but this code does not really clicking on the "New" link, so the next form won't show up, and the rest of the code cannot run. Does anyone know any solution to this problem?
I found out a new window popped up, so it could not find the element in the old window. Thanks guys for helping.
In the first watir code snippet, a :text locator is being used for the .span method instead of a :class locator. For example:
browser.ul(:class, "c_cc").span(:text, "is_c").exists? #=> false
browser.ul(:class, "c_cc").span(:class, "is_c").exists? #=> true
In the second watir code snippet, a :text locator with a value of "New" is being used for the .span method. In this case, a .link method should be used. Additionally, the string includes double-quotes, so the double-quotes must be escaped if enclosed in another set of double-quotes (or enclose in single-quotes). For example:
browser.ul(:class, "c_cc").span(:text, "New").exists? #=> false
browser.ul(:class, "c_cc").span(:text, "\"New\"").exists? #=> false
browser.ul(:class, "c_cc").link(:text, "\"New\"").exists? #=> true
browser.ul(:class, "c_cc").link(:text, '"New"').exists? #=> true
So, one of the following examples should work:
browser.link(:text, "\"New\"").when_present.click
browser.link(:text, '"New"').when_present.click
browser.link(:class, "c_nobdr t_prs").when_present.click
Wow. This is old and I have encountered the same behavior. First, this is in a Cucumber step definition. The following works perfectly in open Ruby code. It simply refuses to function as a step.
It finds the link in the table and clicks it. Click doesn't function.
link = browser.table(class: 'alert-table').tbody.rows[1].cells.last.link(text: 'View')
# <Watir::Anchor: located: false; {:class=>"alert-table", :tag_name=>"table"} --> {:tag_name=>"tbody"} --> {:index=>1} --> {:index=>-1} --> {:text=>"View", :tag_name=>"a"}>
link.click
Ruby 2.4
watir (6.16.5)
regexp_parser (~> 1.2)
selenium-webdriver (~> 3.6)
I'm trying to have CakePHP output a link that looks like this:
<a href="/foo/bar" class="some other classes">
<span class="icon new"></span>FooBar</a>
So I use the following code in my view
<?php
echo $this->Html->link(
$this->Html->tag('span', null, array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
?>
However CakePHP outputs the following:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</span></a>
Which breaks my design. How can I get CakePHP to append "FooBar" after the <span> tags?
EDIT: Its also worth mentioning that I know a <span> tags shouldn't be within an anchor tag usually, but in the case its a must.
You need to use an empty string in stead of null as the text for the span, then your code will work as expected.
Looking at the source code of the HtmlHelper, null is treated as a 'special' value, causing only the opening tag of the span to be created. You can see this in this line:
https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper/HtmlHelper.php#L906
Change your code to this and it should work;
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
Additional explanation of the closing </span>
A bit of explanation, for those who wonder:
The closing </span> in your example is actually not present in the output generated by CakePHP, but automatically 'added' by your browser. If you view the source of the HTML in your browser, you'll see that this is what is actually in your HTML:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</a>
As you can see, no closing 'span'
Because the <span> is not closed, the browser will try to correct this error and automatically assumes that you 'forgot' to close it. Therefor it will add a closing </span> before the next tag it finds (in this case the closing </a>).
The 'inspector' in your browser will always show the HTML that the browser uses to render the output. This includes automatic corrections made by the browser and dynamically generated elements (e.g. Elements added via JavaScript).
To check the output of your PHP scripts, always view the source, not the inspector
In this situation, I've avoided the CakePHP helpers entirely because the markup becomes really messy and cannot take advantage of autocomplete or validation within your IDE.
I usually do something like this:
<span class="icon-new"></span>Foobar
This looks a little overkill for me. Just do this:
echo $this->Html->link(
"<span class="icon new"></span> FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
I've been using CakePHP for 4 years and I don't see the benefit of using tag in this instance.
Would you be able to use regular PHP in this case?
I am thinking you could do it like this:
<?PHP
echo('<span class="' . 'icon new' . '"></span>' . 'FooBar' . '')
?>
In coding layout is the link styled like
<span>Previous</span>
<a href="#" class="class1 class2 class3"><span>Next</span>
I tried to search and I found some possible modifications of a shape of these two links. It's:
<div class="pag">
<%= will_paginate #data, :page_links => false, :next_label => '' %>
</div>
<div class="pag">
<%= will_paginate #data, :page_links => false, :previous_label => '' %>
</div>
My question: How can I update the second example to the shape of the first example? My problem is those two span elements...
Thanks
It's can be very easy - just add html markup for your paginate components to your config/locales/en.yml file
for example:
en:
hello: "Hello world"
will_paginate:
previous_label: <span class="glyph glyph-prev"></span>
next_label: <span class="glyph glyph-next"></span>
It's all!
http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3 You can have your own formatter so you can customize anything you want.
Wiki
en:
will_paginate:
previous_label: "← Previous"
next_label: "Next →"
page_gap: "…"
https://github.com/mislav/will_paginate/wiki/I18n#translating-will_paginate-output