Cannot access href from a element - ruby

I cannot select an href with Mechanize. I have done so successfully in the past with the following methods, but for this website it does not seem to work. Does anyone have any ideas?
Here is a snippet of the output of just the selector:
<div class="component-user-name component-user-name-15">
<a class="name-page-link" data-reco-action="view"
href="/profile/name">Name</a>
These are my attempts:
agent.page.search(".selector a").map {|link| link["href"]}
agent.page.search(".selector a["href"]")
agent.page.search(".selector a")[0]["href"]
agent.page.search(".selector a").attribute["href"]

Try this (html is different, you cannot do copy&paste from different website ;) ):
agent.page.search('a.name-page-link').attribute('href')

If it really looks like that, then:
agent.page.at("a.name-page-link")["href"]

Related

How to define a link in part of an i18n Thymeleaf / Spring message?

Using Spring / Thymeleaf i18n, I'd like to create a HTML paragraph message like "Click here", in which there is a link only for the word "here". What is the best way to do this?
The way I tried doesn't look nice and also results in a like break:
In messages.properties file:
error.generic.click=Click
error.generic.here=here
And in the HTML file:
<p th:text="#{error.generic.click}"></p><p><a th:text="#{error.generic.here}" th:href="#{/contact}"></a></p>
Answer
Your way seems okay to me. If you just want to fix the newline issue go ahead with the following one:
<p>
<span th:text="#{error.generic.click}"></span>
<a th:text="#{error.generic.here}" th:href="#{/contact}"></a>
</p>
The span will make "Click" stay on the same line as "here". However i'd just go for a link that say "Click here" instead of just "here".
For example in german you could say "Hier klicken". "Hier" would mean "here" and "klicken" would mean "click". The Problem is that the meaning for the words changed but the position didn't. You would end up with a link saying "klicken" instead of "Hier".
Not recommented
There is another approach, but it has some drawbacks. You could use:
<p th:utext="#{error.generic}"></p>
with the following messages.properties:
error.generic=Click here
The drawback on this one is that you can't use th:href anymore. I would not recomment this way. However this can be helpfull when using no th:* and just plain html tags. So i wanted to mention it.

click on a image link with Capybara

I am trying to click on a image link with a Capybara / Rspec test. I am having very little success at the moment.
I am trying to select the link with href "/post/3", (knowing that they are other links before). I have tried many combinations of xpath without success. The only combination working was
page.first(:xpath, //a).click
however when I have changed the file and added more links above my Capybara test is broken.
<div class='row'>
<img id="imagen3" src="/system/posts/images/000/000/003/original/frankie-mannings-102nd-birthday-5160522641047552-hp.gif?1464448829" alt="Frankie mannings 102nd birthday 5160522641047552 hp" />
<p>caption</p>
</div>
How can I select that link, and click it?
ok I got it:
find(:xpath, "//a[contains(#href,'posts/3}')]").click
//a[contains(#href, 'posts/3’)]

How to check if url/links exist in page in Capybara?

I've been searching for so long already but haven't found any solution.
I would like to check if a particular URL exists in the page. Rspec Capybara
for example: I'd like to check if the url http://project/guides/basics/ is in the page.
capybara has has_link function but only accepts an id or text as a parameter so for this example,
<a href='http://project/guides/basics/'>
<div class='image-button-container'>
<img src='/images/basic_img.png'/>
</div>
</a>
how should I do the expect() in Rspec using Capybara? Thanks
The following two should work:
expect(page).to have_link('', href: 'http://project/guides/basics/')
expect(page).to have_selector("a[href='http://project/guides/basics/']")
I was really hoping there is a better looking way, but unfortunately, I can't find one.
Capybara API provides method like below -
page.should have_link("Foo")
page.should have_link("Foo", :href=>"googl.com")
page.should have_no_link("Foo", :href=>"google.com")
For your specific sample you can find all elements and then check if a is there with specific div and image. like below -
page.all("a[href='http://project/guides/basics/'] div img")[0]['src'].should be('/images/basic_img.png')

Link to anchor tag on a different page not working in Firefox

I have a link at the bottom of a page, and its purpose is to link to an anchor on a different page. It doesn't seem to work in Firefox (at least, not in v8). Instead, on load it goes right to the very bottom of the page.
Any help is appreciated!
Source: http://msi.emsix.com/news/1900/1/Six-in-Ten-Employers-Hope-Health-Care-Reform-will-be-Repealed.aspx (the "Susan McIntyre" link at the bottom)
Anchor markup:
<h3 id="mcintyre" name="mcintyre">Susan McIntyre</h3>
Try adding the following code:
<a name="mcintyre"></a>
<h3 id="mcintyre">Susan McIntyre</h3>
This works correctly if I disable javascript.
It looks like what happens is that the scroll is done before you use script to collapse away a bunch of the content. So the final scroll position ends up wrong.
If you're collapsing the content away async, that could do it...
If you are unable to create <a name='%hashName%'></a> anchor, consider this:
$(document).ready(function(){
$(window.location.hash).append('<a name="' + window.location.hash.replace('#','')+ '"></a>');
window.location.href=window.location.href;
});
I know it looks weird, but works fine.
Since today I haven't heard about div anchor tags. Are you sure it is proper to expect such kind of behaviour from browsers?

Groovy htmlunit getByXPath

I'm currently using HtmlUnit to attempt to grab an href out of a page and am having some trouble.
The XPath is:
/html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a
On the webpage it looks like:
<a class="t" title="This Brush" href=http://domain.com/this/that">Brush Set</a>
In my code I am doing:
hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[#class='t']")
However, this is returning everything in there instead of just the url that I want.
Can someone explain what I must add to get the href? (also it doesn't end with .html)
You are selecting the a. You want to select the a/#href.
hrefs = page.getByXPath("//html/body/div[2]/div/div/table/tbody/tr/td[2]/div/div[5]/div/div[2]/span/a[#class='t']/#href")

Resources