Finding text contained in an element - watir

I have a span element with a keyword in it that I'd like to find. How can I best do this with watir?
I can't figure out how to find the span with my text in it. I can find the spans, but narrowing down to the one with my keyword in it isn't working-- not sure how to stick a regex into this statement below.
Here's what I've got so far, to give an idea on what I'm after:
#browser.div(:id, 'my_div_element').span(:text, 'my keyword here').exists?
..that returns 'false' at the moment. I can find the spans but when I try narrowing down with the :text bit I run into a wall. And I know my keyword is in there.

Based on the example provided, this is the syntax for locating text (enclosed in <div> and <span> tags) using regex:
#b.div(:id, 'div_element').span(:text, /keyword/).exists?

Related

How is this even showing no such element unable to locate element.My project is to scrap datas from product table from . Help to find the correct xpat

No such element
Help me to find the correct Xpath of the website etsy product table https://www.etsy.com/in-en/c/jewelry/earrings/ear-jackets-and-climbers?ref=catnav-10855
If you are looking for the whole table, then I believe the xpath you are looking for is:
//*[#class='wt-bg-white wt-display-block wt-pb-xs-2 wt-mt-xs-0']
As far as I can tell the table is the only element with that particular class combination, so I would use that to identify it.
If you use relative xpath it might break due to unforseen chances in DOM structure or css.

Can't seem to find an XPath with a value that contains double quotes

I've read through the forums and apparently almost nobody has issues to find XPaths with values with double quotes, most posts I found talked about finding Xpaths to elements with values that had both single quotes and double quotes, so I decided to ask this question, I apologize if this is already answered elsewhere.
Anyway, the element I wanted to find goes more or less like this:
<a class="product" title="REALIZE "WHY NO.T"" width="454" height="423" alt="" id="">
</a>
I tried changing the XPath several times without success, using Selenium Webdriver
'//a[#title="REALIZE "WHY NO.T""]'
'//a[#title="REALIZE \"WHY NO.T\""]'
"//a[#title="REALIZE \"WHY NO.T\""]"
These are a few of the ones I tried, there are a few more but I didn't save all the ones I tried
I feel like it might be a matter of me missing something terribly basic, but I've been looking for the answer for hours without success
//*[#title='REALIZE "WHY NO.T"']
You have to wrap content with single quotes, the python code will be ( escaping the single quotes
driver.find_element_by_xpath('//*[#title=\'REALIZE "WHY NO.T"\']')
You say " the element I wanted to find goes more or less like this:"
"More or less"? What on earth does that mean? Is it more like this, or less like this? How can we help you if we don't know exactly what it's like?
And then you say:
<a class="product" title="REALIZE "WHY NO.T"" width="454" height="423" alt="" id="">
</a>
But that's not well-formed XML. How is the parser supposed to work out where the title attribute ends? The XML parser should throw it out at that point.
OK, you're probably using an HTML parser rather than an XML parser, and HTML parsers try to make sense of any old garbage you throw at them. But I've no idea what an HTML parser will do with this input. HTML parsers are smart, but they're not smart enough to work out which of these quotes are part of the attribute value and which of them mark its beginning and end. It's probably turned it into something quite different from what you were expecting, and that's why your XPath expression doesn't work.
I would recommend
right-clicking the intended element > click inspect element > look over element in console > copy xpath > paste it and analyze how it outputs.
From there I would then compare it to your current solution and maybe tweak a thing or two.

Excel VBA formula: extract file extension from filepath?

Given an excel column containing filepaths, what excel formula returns only the file extension?
src\main\java\com\something\proj\UI.java --> java
src\main\java\com\something\proj\Server.scala --> scala
src\main\java\com\something\proj\include.h\someinclude.hpp --> hpp
Note 1: this formula works great for filepaths with only a single period, but not for case 3:=IF(A1="","",RIGHT(A1,LEN(A1)-FIND(".",A1)))
Note 2: I understand that these filepaths are Windows-specific, I don't need a cross-platform solution.
Related: triming extension from filename in Excel and How to extract file name from path?
With data in A1, use:
=SUBSTITUTE(RIGHT(SUBSTITUTE(A1,".",REPT(".",999)),999),".","")
From:
Jim Cone's old post
This will find everything after the last .:
=MID(A1,FIND("{{{",SUBSTITUTE(A1,".","{{{",LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))+1,LEN(A1))
Here's a nice long answer. :-)
=SUBSTITUTE(A1,LEFT(A1,FIND(CHAR(1),SUBSTITUTE(A1,".",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))),"")
A neat trick I sometimes use for string parsing in general is to leverage the FilterXML() function (Excel 2013 and later). The basic strategy is to use Substitute() to format your string in a way that it is parsed across elements in an xml string, and then you can use xpath syntax to conveniently navigate your parsed elements. Using this strategy, getting an extension would look like this...
=FILTERXML("<A><p>" & SUBSTITUTE(A1,".","</p><p>.")&"</p></A>","//p[last()]")
If you're not familiar with xml, this can seem intimidating, but if you can grasp what's going on, I find it to be cleaner, more flexible, and easier to remember than the alternative approaches using len(), substitute(), etc. One reason why it's nicer is because there's only one cell reference.
Illegal Characters
There are two characters that are allowed in paths but not in xml: & and '
The equation above will work if these characters are not present, otherwise, they will need to be handled something like this...
=FILTERXML("<A><p>" & SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(J8,"'",""),"&",""),".","</p><p>.")&"</p></A>","//p[last()]")
Example
Suppose we have a nasty file path like this:
C:\Folder1\Folder2\(ugly characters !##$%^()_+={};;,`)\two.dots.LongExt
1.) The Substitution() portion will convert it to an xml string like this...
<A>
<p>
C:\Folder1\Folder2\(ugly characters !##$%^()_+={};;,`)\two
</p>
<p>
.dots
</p>
<p>
.txt
</p>
</A>
2.) Once formatted like this, it's trivial to pick out the last p element using the xpath syntax //p[last()].

How can I insert HTML at repeating intervals in expressionengine?

I have a loop of entries and I want to apply formatting to every second entry - not just applying a class but also some basic HTML markup. How can I do this?
This question seems to come up a lot so I thought I would post a simple example:
{exp:channel:entries channel="whatever"}
{switch="<div class='entry'>|"}
<h2>{title}</h2>
{if count != total_results}{switch="|</div>"}{/if}
{if count == total_results}</div>{/if}
{/exp:channel:entries}
In this example, a div with a class of "entry" is wrapped around every second entry. The switch variable at the front end is pretty straight forward. The back end uses two conditionals: if the entry is the last entry in the loop, close the DIV. If the entry is NOT the last entry in the loop, close the DIV only for every second entry (a reflection of the switch variable at the beginning of the loop).
Important to note here that the switch variable is very sensitive to quotes - so when inserting HTML in this fashion, inside the switch variable, you have to use single quotes rather than double quotes. This is fine for simple insertions, but may be a bit unfriendly if you have more complex formatting in mind. Hopefully this helps some folks and feel free to expand on this idea.
There is also a plugin which might help in this situation GWcode Alternate. I haven't used it myself as I prefer to use the native switch tag mentioned in the other answer.
The example I had used was specifically for those instances in which a DIV is not desired on EVERY entry but rather a wrapping DIV around certain intervals - common when you want to have a jQuery slider that includes 3 entries at a time, for example. This was expressly the purpose of my example. EVERY entry is not what I would consider intervals, which is what my example was intended for. Certainly it is an edge case - but an edge case that occurs often enough to capture a simple solution to the challenge that still uses native functionality with only simple conditionals.

Solr not matching. Threshold setting, or something weird?

I'm using solr to search for articles. I created 2 test "body" sentences which have the common word "tall", but there is no match.
The Query---> Body:"There are tall people outside" AND !UserId:2
Does not match a post with:
Body: the KU tower is really tall
UserId:3
Is this just simply a very low matching score? or is there something else going on here? In the case of a low matching score should it really be that low? The body sentences are very short and share a common word, I would have expected some match.
EDIT: I think the matching isn't happening as a result of having the !UserId: 2 condition. If I try to match body sentences without that, its very liberal. Can anyone explain this? and perhaps how to best structure a query to avoid this type of specific behavior?
Thanks!
I have seen some funky behavior with the ! operator with Solr. I would suggest you use the - (negative indicator) instead as shown in the SolrQuerySyntax Wiki Page. Try changing your original query to Body:"There are tall people outside" AND -UserId:2 to see if that works as you are expecting.
For those who come after me, I found a solution however not necessarily an explanation for its behavior.
The Solr query:
(PostBody:There are tall people outside) AND !UserId:2
worked as I desired above. Note that if the quotes are added around the body, it does not match. I believe Solr attempts to match such a query as a single string rather than individual words.

Resources