Finding sibling of a link while using watir-WebDriver - ruby

I'm using watir-WebDriver to automate a website test automation. I have to get to the Sibling link.
looks like this
Something > Something 2 > current page title
(link1) (link2) (text1)
I have the class id of the text1, and I have to go back to the Something 2 by clicking on the link2.
How do I get this.

I think you can get the sibling link with elements_by_xpath.
example
HTML source:
bar<p class="baz">text</p>
watir-webdriver script:
p(:class,'baz').elements_by_xpath('preceding-sibling::*')[-1]

If you can find a common parent for the two siblings, you can go to that parent, then select the element from that point using something like this:
source html:
<tr>
<th>No. of films</th>
<td>7</th>
</tr>
scrape (#b is the browser object)
#b.th(:text => "No. of films").parent.tds.first.text
#=> 7

If (text1) is a child of (link2) you might try:
(text1).parent

If I understand right you have some kind of container element like a like a div, and inside it are two links and some text. What is not clear is if the text belongs to the outer container, or is inside a container of its own. Given you say you have the 'classid' (is that the class? or the id?) of the text, I'm going to presume that it is in it's own container element.
In that case
browser.element(:class, 'value').parent.link(:index, 2).click
That's the best I can do without an actual sample of HTML, a clearer model of the DOM

As all the previous answer says, you gotta have a reference point above this hierarchy. Then browser.p(:id, 'known').parent.links[1].click would do the job. But if you really really don't, try this hopelessly simple approach:
browser.link(:text => 'Something 2').click

Related

ruby - loop capybara click all elements

May you help me in this matter, pls?
I would like to be able to delete all my files, but I would like to use a loop structure. How could I do this using the foreach in ruby, using the capybara? I've tried several posts and I did not get the expected result.
enter image description here
Delete File
Assuming there are no other buttons with the the class File__Button, you should be able to do something like:
page.all(:css, 'button.File__Button').each do |button_element|
button_element.click
end
If there is a confirmation, you will need to click that as well.

How to locate an element having href='#' attribute in anchor tag

abc
I am not able to locate above element. I tried //*[#id="contact-groups"], but with no success.
Well, XPath is not the best of the methods to find elements. But following will match the links with href = "#".
//a[#href="#"]
Not sure if your scenario is as simple as your question. I did test this with a simple html page.
The locator seems correct. You could try this too:
.//*[#id='contact-groups']
As per HTML code provided you can try below xpaths
//a[#id='contact-groups']
//a[contains(text(),'abc')]
//a[#href='#']
Thanks
I am guessing the link is in an Iframe.
Use the following code to switch to the frame and then click on the link
driver.switchTo().frame("frame-name");
driver.findElement(By.xpath("//a[#id='contact-groups']")).click();

How to use Selenium using Xpath to determine the classes of an element?

I am trying to use xpath within selenium to select a div element that is within a td.
What I am really trying to do is determine the class of the div and if it is either classed LOGO1, LOGO2, LOGO3 and so on. Originally I was going to just snag the image:url to determine with logo.jpg was used but whoever made the target website used one image for each logo type and used css to determine which portion of the image will be displayed. So Imagine 4 images on one sprite image. This is the reason why I have to determine the class of the div instead of digging through the css paths.
In selenium I am using storeElementPresent | /html/body/form/center/table/tbody/tr/td[2]/div[3]/div[2]/fieldset/table/tbody/tr[2]/td/div/table/tbody/tr[${i}]/td[8]/div//class | cardLogo .
The div has multiple classes so I am thinking that this is the issue, but any help is appreciated. Below is the target source. This is source from within the table in the tbody. Selenium has no problems identifying all the way up to td[8] but then fails to gather the div. Please help!
<td class="togglehidefields" style="width:80px;">
<div class="cardlogo LOGO1" style="background-image:url(https://www.somesite.com/merchants/images/image.jpg)"></div>
<span id="ContentPlaceHolder1_grdCCChargebackDetail_lblCardNumber_0">7777</span>
</td>
I was fiddling with selenium.getAttribute() but it kept erroring out, any ideas there?
This <div/> element has one class attribute with one value, but this one is tokenized when parsed as HTML.
As selenium only supports XPath 1.0, you will need to check for classes like this:
//div[contains(#class, "LOGO1") or contains(#class, "LOGO2")]
Extend that pattern as needed and embed it in your expression.
With XPath 2.0 and better, you could tokenize and use the = operator which works on a set-based semantics:
//div[tokenize(#class, ' ') = ("LOGO1", "LOGO2")]
Old post but I'll put the solution I used up just in case it can help anyone.
xpath=//div[contains(#class,'carouselNavNext ')]/.[contains(#class, 'disabled')]
Fire of your contains, and then follow with /. to check children AND the current element.

Deleting elements using Watir

Does anyone know how to delete an element from the source using Watir? There doesn't seem to be a method for removing elements. Perhaps I'm missing something.
If you know JavaScript, you could execute any JavaScript code on the page.
Example:
browser.execute_script("some javascript code")
I am not a JavaScript ninja, but this question could help you: JavaScript: remove element by id.
Remove elements by css:
browser.execute_script("[...document.querySelectorAll('.some.class')].map(e => {e.parentNode.removeChild(e)})")
We can remove it with javascript. Here's an example to remove a breadcrumbs div element but it's id:
browser.execute_script("bd = document.getElementById('breadcrumbs'); bd.parentNode.removeChild(bd);")
The Purpose for Watir is to do web testing, which is to say drive the browser as if a user was interacting with it. That means doing the things a user could do, clicking on stuff, filling in input fields, etc. It also means being able to verify what is there on the screen that the user can see or interact with.
Since a user cannot delete elements, there is no means by which to do that using the tool.
If the application provides a way for users to 'remove' or 'delete' something, like closing a simulated window, removing a tab etc, then you need to do that by simulating what the user would do (usually clicking on some specific element) in order for that to happen.

Capybara: Link with HTML content not found

Given a HTML structure like this
<strong>My Link</strong>
is not caught by Capybara through a cucumber step
When I follow "My Link"
using a default webstep
When /^(?:|I )follow "([^"]*)"$/ do |link|
click_link(link)
end
while this works:
When I follow "<strong>My Link</strong>"
I haven't been using Capbybara for long, but I can see what causes the problem. So, on a more general level — what's the proper way to go about this? Surely this case has to be pretty common, right?
Any ideas and general musings about Cucumber abuse very welcome!
I would move strong tag outside anchor tag or better use CSS for that.
Or you can assign id attribute to link and use it instead of content. This way is the best if your app supports multiple languages.
Otherwise you have to write some sort of xpath selector for such special case:
find(:xpath, "//a[contains(//text(), \"#{locator}\")]").click
Not tested, just an idea.

Resources