Selenium: how to find div with specific content? - html

I need to find a <div> with certain content and click it from Selenium, as so:
<tr>
<td>clickAndWait</td>
<td>//div[#class='gwt-Label' ***WITH CONTENT='Logout'***]</td>
<td>5000</td>
</tr>
Is there some way to do this? I don't want to use an absolute xpath.

You could also use CSS locators:
<div class="gwt-Label">This FindMe DIV</div>
Could be located using:
css=.gwt-Label:contains('FindMe')

try this:
//div[#class='gwt-Label' and contains(., 'Logout')]

Perhaps your XPath just isn't quite doing what you think. You may need to use the string() function to concatenate all the text in a block.
For example, in TestPlan (using Selenium as backend) you would do something like this:
Click //div[#class='gwt-Label'][contains(string(),'Logout')]
Note the use of string()

Related

How to select <div class="ok">.....<a href="soft://an.id/">...</div> nodes?

A document has several <div class="ok"> tags. I am able to select all of them with
"//*[#class="ok"]" (i don't have to specify div, because only div tags have this class). I get a list of 6 nodes matching this.
Now, i need
either to test each node in order to see if it includes the tag <a href="soft://an.id/">. This inclusion is not direct. I mean, the <div> includes a <table> with many <tr> and <td> and <span>, and the <a..> (only one, or none) somewhere before </div>.
or to directly select only (div) nodes of class="ok" that include this <a> tag.
I have tried many things, that all fail. Including protecting the "/" in the href detection (is it required?).
I am quite familiar with regular expressions, but i must confess that i find XPath syntax even harder to understand.. And the W3C reference documents are so hard, without examples..
Any hints are welcome.
In order to select only <div class="ok"> element containing <a href="soft://an.id/"> child element you can use the following XPath locator:
"//div[#class='ok' and .//a[#href='soft://an.id/']]"
If I understand you correctly, you have a nested somewhere under the div with class "ok", right?
So in xpath, the a / is meant for a direct locator under/above the current tag. If you are looking for the somewhere under the found div, you need to use:
//div[#class="ok"]//a[#href="soft://an.id/"]
Then you need to check if it exists or not by using some kind of an assertion.

Selecting an element with a class that contains a certain string

Say I have this HTML:
<body>
<div>
</div class="Something-generated-is7293n">Hello</div>
</div>
</body>
How would I go about finding this considering the following constrains:
This is for testing purposes so I don't want to assume a certain structure. This way if a <div> suddenly is added to the HTML, my xPath won't break my test.
I cannot even try to guess what the class will be. I can only know it will contain, say, Something and generated.
I tried //div[contains(#class, 'Something') and contains(#class, 'generated')] with no success at all, which makes me think I'm missing something to have xPath evaluate only part of a class.
Of note, my tests use ChimpJS with uses WebdriverIO.
This xpath-fiddle might do the trick for you: http://xpathfiddle.net/UN71EW

Protractor: Finding Element by Div Text

Hey I have this code in one of my div elements:
<div class="col-sm-8">Account Information: </div>
Can someone tell me how I would go about finding this element in my protractor code? Is it possible to do something like this:
expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);
I have multiple elements with the class "col-sm-8" so I am not able to find the element by class. I was just wondering if there is any way to possibly find the element using the text in the div element? Thanks for the help!
I would recommend you to use by.cssContainingText
element(by.cssContainingText('.col-sm-8', 'Account Information'))
There is no webdriver method which would allow locating an element by its text. You could try using xpath in the following way (not tested):
element(by.xpath('//div[contains(text(), "Account Information: ")]')
keep in mind by.cssContainingText matches the element by PARTIAL text
so element(by.cssContainingText('div', 'male')) will actually match both male and female text
To solve this, use xpath with exact text match
element(by.xpath('//div[text()="male"]'))

How to convert a string into a HTML element in Mithril?

Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?
Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.
Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:
m("htmlattribute.classeCss" , "value");
So in your case:
m("span.msg" , "Text goes here");
Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)
You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.
Docs here.

passing angular.js variables to html?

I have an angular app in which there's a table that includes something of the general form:
<tr ng-repeat='d in data'>
<td>{{d.foo}}</td>
</tr>
I'd like to use the value of d.foo (which, for example, could be Bicycle to turn the cell into a link to a website like http://en.wikipedia.org/wiki/Bicycle. I've tried to find an answer to this on SO already but have had no luck; my apologie if I just didn't see it.
Is it possible to do the described task? If so, any pointers or suggestions?
Use:
<td><a ng-href="http://en.wikipedia.org/wiki/{{d.foo}}">{{d.foo}}</a></td>
You can read more here: http://docs.angularjs.org/api/ng.directive:ngHref
;)
Use it in a <a> tag.
For example:
{{d.foo}}