How to test onclick in this object without id - watir

I'm trying to test the on-click on this object but i don't know how to get to i'm, anyone can help me?
<a onclick="addRemoveSelectedNumber(this);">
<img src="../../static/images/sec_core/delete.png">
</a>

Two intuitive alternatives:
browser.link(:onclick=>"addRemoveSelectedNumber(this);").click
You may have to escape the slashes in this one...
browser.image(:src=>"../../static/images/sec_core/delete.png").click

Use an XPath expression to find your element.
browser.link(:xpath, "//path/to/my/element/a").click
See: http://wiki.openqa.org/display/WTR/XPath

Related

How can I get text from span(s) in dirs which are located in table?

I have just only recently started using selenium and I can't get it to work properly.
I need to get a text from (s) which are inside dives which are inside a table.
Like this:
<td class="some-class">
"""many divs above"""
<div class="props">
<span>text</span>
<span class="mr2">text</span>
</div>
"""/many divs above"""
</td>
So, I need to get 'text(s)' from spans, but also there are many td s like this one and inside divs, classes repeat.
Moreover, how can I get it as a list?
I have tried this:
el = browser.find_elements_by_css_selector('dir.props > span')
print(el)
for i in el:
dict_of_orders.append(i.text)
print(dict_of_orders)
However, it returns only empty lists.
Any advice?:)
Thanks ahead for any help!
You have a typo in your CSS selector. It should be div instead of dir:
el = browser.find_elements_by_css_selector('div.props > span')
I don't know if that will fix your problem, but you'll have to fix that.
Another thing I would try is, instead of text, use the innerText attribute, like this:
dict_of_orders.append(i.get_attribute('innerText'))
Hello for future someone like me. My problem was solved by adding the delay.
Check it, maybe a site that you're trying to parse is slow.
time.sleep(5) was the answer.
el =browser.find_elements_by_xpath("//div[#class='props']/span")

Cannot identify the object in Selenium

I cannot identify the object of the icon showed in the attached screen shot. I have shown the HTML code as well.
The ID's are getting changed dynamically.
Can anyone guide on how to identity this kind of objects in Selenium?
If the ID is always changed, I recommend using CssSelector instead.
For instance,
<div id="running_number_12345" class="icon something">...</div>
You can use locator
driver.FindElement(By.CssSelector("div[class*='icon something']"));
If your icon doesn't have any specific css pattern, I recommend adding something in class attribute. If not, you have to use complex CssSelector to find it.
Try this
driver.findElement(By.cssSelector(".icon something"));

Is it possible to add some arrows to ms2gallery and use it as carousel?

I have project on modx and there is used ms2gallery to show images. And customer want to add some arrows(prev, next) to use this ms2gallery as carousel or slider. Is it possible to build-in this functional into ms2gallery? If not could you advice something to solve this task?
You can use the pdoPage snippet for just this purpose. Here's the example form the ms2Gallery documentation:
[[!pdoPage?
&element=`ms2GalleryResources`
&parents=`0`
&tpl=`#INLINE
<p>
[[+pagetitle]]
<img src="[[+120x90]]" title="[[+120x90.name]]" />
</p>
`
&typeOfJoin=`inner`
&includeThumbs=`120x90`
&includeOriginal=`1`
]]
[[!+page.nav]]
You will need to add some extra properties to the snippet call to get more than one result. All the available options and several examples are documented on the pdoPage readme.

How to click on link element with specific text using watir?

I'm not able to click on text link 'Add' using watir:
PAGE:
<div id="divAdd" style="float: right">
<a onclick="SwitchView('2')" style="color: #1B56A7; cursor: pointer;">Add</a>
</div>
Watir CODE:
browser.link(:text =>"Add").click
EXCEPTION:
Unable to locate element, using {:tag_name=>["a"], :text=>"Add"}
Please help me how to handle this?
If the page has a lot of ajax and javascript going on, you may just have to wait a little bit for the client side code to finish rendering the page after it has been loaded from the browser.
Try this
browser.link(:text =>"Add").when_present.click
If that does not work, then make sure the item is not in a frame or something..
btw, if there is more than one link on the page with the text 'Add' then you may have to specify a container outside the link that lets you identify which link you want. eg.
browser.div(id: => "divAdd").link.when_present.click
If
This would be my way of doing it.
while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do
browser.div(:class, 'containerDIV').a(:text, 'Add').click
end

How to use Watir to find a specific link when all the links have the same display text?

The links are wrapped in a span:
<span class='editbio'>
Edit
</span>
...
<span class='addbio'>
Edit
</span>
How about something like
browser.span(:class, "editbio").link(:text, "Edit")
to get the first link
vs
browser.span(:class, "addbio").link(:text, "Edit")
for the second link?
You can get a list of elements and tags you can use at Watir: Methods supported by Element.
You can try to use multiple arguments with the Watir's link method:
http://wiki.openqa.org/display/WTR/Multiple+Attributes
For your example, the accepted answer works fine. But if both of your links were in a single span and had the same display text, like so:
<span class='edit'>
Edit
Edit
</span>
You could use something like this for the second link (zero-based index):
browser.span(:class, 'edit').link(text: 'Edit', index: 1)

Resources