Cucumber Capybara - Find an element based on attribute's content - cucumber

My requirement is to check if some particular content (i.e. Id=4540484 in the below example) exists in the onclick attribute on an anchor tag on a page. Can someone help me out please?
<a title="Acrobat PDF File" href="#" onclick="openPop('/example/secure/Screen.aspx?Id=4540484&certdocId=2513235)"><img src="New.gif" align="middle" border="0"></a>

If you want to find an element based on the onclick attribute, you will need to use a css (or xpath) selector.
In your case, you want the css-selector:
a[onclick*="Id=4540484"]
This says to find a link element where the onlclick attribute contains the text "Id=4540484".
You can then use the has_css? method to check if that link is on the page:
#Using an ID that exists will return true
page.has_css?('a[onclick*="Id=4540484"]')
#=> true
#Using an ID that does not exist will return false
page.has_css?('a[onclick*="Id=9999999"]')
#=> false

Related

How to click on Web check box using Excel VBA?

How do I check the table checkbox?
I tried clicking.
ie.Document.getElementsByClassName("x-grid3-hd-checker").Checked = True
<div class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on" unselectable="on" style="">
<a class="x-grid3-hd-btn" href="#"></a>
<div class="x-grid3-hd-checker"> </div>
<img class="x-grid3-sort-icon" src="/javascript/extjs/resources/images/default/s.gif">
</div>
I can't see a checkbox in the HTML code. But you use getElementsByClassName() in a wrong way for your case. getElementsByClassName() generates a node collection. If you need a specific node, you must get it by it's index in the node collection. First element has index 0.
Please note that the div tag with the CSS class class="x-grid3-hd-inner x-grid3-hd-checker x-grid3-hd-checker-on " is also included in the Node Collection, because a part of the class identifier is identical to "x-grid3-hd-checker ". [Edit: I'm not realy sure if the part must maybe stand at the begin of the identifier]
If you want to check this:
<div class="x-grid3-hd-checker"> </div>
Your code needs the second index of the node collection:
ie.Document.getElementsByClassName("x-grid3-hd-checker")(1).Checked = True
But if there are more tags with the class name "x-grid3-hd-checker" the above line don't work. I can't say anymore until you don't post more HTML and VBA code. The best would be a link to the site.

how to select class name from button of span using css selector in below code as follows and have received an error

how to select class name from button of span using css selector in below code as follows and have received an error.
<span class="input-group-btn-vertical">
<button class="btn btn-white bootstrap-touchspin-up " type="button">+</button>
<button class="btn btn-white bootstrap-touchspin-down " type="button">-</button>
</span>
this.demoTestBailrafting = function (browser) {
browser
.url('https://www.phptravels.net/')
.waitForElementVisible('body', 500)
.assert.title('PHPTRAVELS | Travel Technology Partner')
/*trying to select first element in button tag of span class*/
.click('span[class=input-group-btn-vertical]>button[1]')
.pause(500)
.end()
};
ERROR: Unable to locate element:
"span[class=input-group-btn-vertical]>button[1]"
using: css selector
Assuming you want to get the class attribute from both of the buttons below the span -- not sure what language you are using here. But you should be able to use the getAttribute function on WebElement object.
List<IWebElement> buttons = driver.findElements(By.XPath("//span[#class='input-group-btn-vertical']/button"));
foreach(IWebElement button in buttons)
{
// this is the class name
string className = button.getAttribute("class");
}
Your click method
.click('span[class=input-group-btn-vertical]>button[1]')
is apparently expecting a CSS selector based on the error message
ERROR: Unable to locate element: "span[class=input-group-btn-vertical]>button[1]" using: css selector
^^^^^^^^^^^^^^^^^^^
The index [1] that you have in your CSS selector isn't valid. It's also not needed here since you are looking for the first element and Selenium will always act on the first element unless you otherwise specify. Also, to make your CSS selector more CSS-like (and more flexible), you should change it to
span.input-group-btn-vertical > button
^ this dot indicates a class name follows
^ I removed the [1] since it's not needed
See if that works now.

How can i click the third href link?

<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[#id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag.
The code keeps clicking on the second tag.
Try
driver.find_element_by_xpath("//ul[#id='pairSublinksLevel2']/li[3]/a").click()
EDIT:
Thanks #DebanjanB for suggestion:
When you get the element with xpath //ul[#id='pairSublinksLevel2'] and search for a tag in its child elements, then it will return the first match(In your case, it could be inside second li tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
As per the HTML you have shared you can use either of the following solutions:
Using link_text:
driver.find_element_by_link_text("last data").click()
Using partial_link_text:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath:
driver.find_element_by_xpath("//ul[#class='arial_12 newBigTabs' and #id='pairSublinksLevel2']//a[contains(#href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver

how to click on the first result of a search on metacritc with selenium

how do I click on the first search result on metacritc's search bar?
this is what I have so far:
self.search_element = self.driver.find_element_by_name("search_term")
self.search_element.clear()
self.search_element.send_keys(self.game_line_edit.text())
self.link_to_click = self.driver.find_element_by_name("search_results_item")
self.link_to_click.click()
# self.game.setText(self.driver.find_element("search_results_item"))
self.game_line_edit.setText("")
self.driver.close()
but I'm getting this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"search_results_item"}
I realize selenium can't find the link but I am not sure what element it is
this is the HTML I'm trying to click on:
<a class="search_results_item" href="https://www.metacritic.com/game/pc/into-the-breach">
<span class="metascore_w score_outstanding">90</span>
<span class="title" data-mctitle="Into the Breach"><b>Into the</b> Breach</span>
<span class="type secondary">PC Game</span>
<span class="separ secondary">,</span>
<span class="date secondary">2018</span>
</a>
can someone help?
Thanks!
You are searching by name when you are referring to a class. Instead use a CSS selector, e.g.
.find_element_by_css_selector(".search_results_item")
.search_results_item indicates a class with the name 'search_results_item'.
If that doesn't work, you probably need a wait. See this answer for more info.
If you're fine with using xPath, you can select by index.
(//a[contains(#class,'search_results_item')])[1]
#JeffC is also correct as well. You should not select by name because this element has no name. At the very least, select by class or tag.

jquery / cheerio: how to select multiple elements?

I need to parse some markup similar to this one, from an html page:
<a href="#">
<i class="icon-location"></i>London
</a>
I need to get London.
I did try something like (using cheerio):
$('a', 'i[class="icon-location"]').text();
or
$('a > i[class="icon-location"]').text();
without success...
I'd like to avoid methods like next(), since the expression should be passed to a method which just extracts the text from the selector.
What expression should I use (if it's feasible) ?
There's a solution, which is pretty unusual, but it works :
$("#foo")
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text();
Demo : https://jsfiddle.net/2r19xvep/
Or, you could surround your value by a new tag so you just select it:
<i class="icon-location"></i><span class="whatever">London</span>
Then
$('.whatever').text();
$('a').text();
will get text as 'London'.
$("a .icon-location").map(function(){
return $(this).text()
}).get();

Resources