click on a image link with Capybara - xpath

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’)]

Related

Cannot access href from a element

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"]

Selecting with Xpath in Scrapy

I'm using Scapy to scrape some data from a site and I need help using Xpath to select "data" from the following.
<span class="result_item"><span class="text3"><span class="header_text3">**data**</span><br />
**data**<br />
**data**</span> <span class="phone_button_out"><span class="phone_button" style="margin-top: 0"
onclick="pageTracker._trackEvent('USDSearch','Call Now!F');phone_win.open('name','**data**',27101650,0)">
Call Now!<br />
</span></span>
What statements can I use to select the necessary data? I hope this isn't a stupid question. If it is, please point me in the right direction.
There are multiple data elements to get in the posted html. Assuming that <span class="result_item"> is parent of the items, you can try the following:
To get header:
//span[#class='result_item']/span[#class='header_text3']/text()
To get anchor link data:
//span[#class='result_item']/a/text()
Also, to help with xpaths, install Firebug Addon in Firefox, then FirePath addon on Firebug. Pointing to elements will give you autogenerated xpaths (good for beginners. sometime needs xpath tuning)

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')

A way around Element cannot be scrolled into view - Watir-webdriver with Ruby

So, we have the following code in our page:
<div class="toggle-wrapper">
<input id="HasRegistration_true" class="registration_required toggle" type="radio" value="True" name="HasRegistration" data-val-required="The HasRegistration field is required." data-val="true">
<label for="HasRegistration_true" class="">On</label>
<input id="HasRegistration_false" class="registration_required toggle" type="radio" value="False" name="HasRegistration" checked="checked">
<label class="checked" for="HasRegistration_false">Off</label>
</div>
These are 2 radio buttons. 'On' and 'Off'. 'Off' is the default value.
Using Watir-webdriver and Ruby, we want to select the 'On' radio button. We do so like this:
browser.radio(:id => "HasRegistration_true").set
But in doing so, we get the following error:
`WebElement.clickElement': Element cannot be scrolled into view:[object HTMLInputElement] (Selenium::WebDriver::Error::MoveTargetOutOfBoundsError)
We know Selenium 2 scrolls the page to the element, so trying to scroll down is useless.
We are always using the latest releases of watir-webdriver and ruby.
We can't change the HTML of the page since we're QA engineers.
Here are two solutions that have worked for me:
There is a Ruby gem called watir-scroll that can work.
Then
require 'watir-scroll'
browser.scroll.to browser.radio(:id, "HasRegistration_true")
If you don't want to add a gem, my co-worker suggested something that somewhat surprisingly (to me) had the same effect as the above code:
browser.radio(:id, "HasRegistration_true").focus
browser.radio(:id, "HasRegistration_true").set
In my code, ".focus" scrolled straight to the element that was previously not visible. It may work for you as well.
First of all try locating the element using XPATH:
browser.element(:xpath, "//input[#id='HasRegistration_true']").click
or
alternatively if it is a hidden element you are trying to locate then you are better off using CSS. Download firebug add-on for firefox and copy the CSS path of your element.
It should be something like:
browser.element(:css => "the CSS path you have copied from Firebug").click
One of the 2 should do it for you!!
Best of luck!
You could manipulate the html on the fly by executing some javascript to make the radio element settable. To execute javascript on a page, do something like:
#browser.execute_script("your javascript here")
I used something like the following javascript to strip the class out of a label tag which moved it out of the way of the input tag I was attempting to act on for a Chrome specific problem I had.
execute_script("$(\"label.classname\").removeClass(\"classname inline\")")
If the element is contained within a form and a div (Wrap) class I found that I had to do the following to click the 'No' radio button on the "https://quote.comparethemarket.com/Motor/Motor/AboutYourVehicle.aspx?" page:
div_list = #browser.form(:action => "AboutYourVehicle.aspx?ton_t=CTMMO&prdcls=PC&rqstyp=newmotorquote&AFFCLIE=CM01").div(:class => "inputWrap").divs(:class => "custom-radio")
And then:
div_list[1].click
Hope this solves your issue too :-)
I'm not using watir but I have the same error as you "...could not be scrolled into view ...". I tried to use watir just to solve it and didn't work for me. Then, I use an ActionBuilder (move_to with click) and the error disappeared.
My line to avoid the error is:
#driver.action.move_to(*webelement*).click.perform
I hope it will be useful for you

how do I assert that a hyperlink points to a given URL?

I am using Selenium IDE and Selenium RC to check links on a website, but I am having trouble finding a way to assert that a link points to a specified url and has the specified text.
Constraints:
locating the element by dom or xpath is not feasible for it will make the test brittle
there are multiple link elements with identical link text
The HTML:
link text
link text
The test I'd like to do (rspec):
page.is_element_present?("Href=path_y, Link=link text").should be_true
#locator parameters are in order of preference
Any ideas?
i guess get_attribute should help. you should get href from link, it is attribute.
You could use isElementPresent as follows:
assertTrue(selenium.isElementPresent("//a[text()='Example Link' and #href='http://www.example.com/']");
Given the following HTML:
<html>
<body>
<a id="myLink" href="http://www.example.com">Example Link</a>
</body>
</html>
You could use the following Selenium commands (using Java/TestNG, and Selenium 1.x)
assertEquals(selenium.getText("id=myLink#href"), "Example Link");
assertEquals(selenium.getAttribute("id=myLink#href"), "http://www.example.com/");
Using Selenium 2.x the example would be:
WebElement myLink = driver.findElement(By.id("myLink"));
assertEquals(myLink.getText(), "Example Link");
assertEquals(myLink.getAttribute("href"), "http://www.example.com/");

Resources