How do I find/match a string in Selenium IDE? - xpath

I am trying to find a matching string and then click the reply button using Selenium IDE.
I keep getting a 'not found' error.
I tried:
/html/body/div[2]/article[2]/div[1]/a[3]/span[contains(text(), 'my-search-string')
sample HTML:
<article class="thread">
<div class="threadline normal first">
<span class="area" title="no location given">---</span>
text? <
<a href="?act=su&handle=my-search-string" target="R" class="handle">
<span class="handle hnd">my-search-string</span>
</a> >
<button class="BlockButton">Block</button>
<time>12:14</time>
</div> <!-- Added by edit -->
</article> <!-- Added by edit -->
or
<div class="threadline normal">
<span class="dotz"> : . . : . . : . . : . . : . . </span>
some text <
<a href="?act=su&handle=my-search-string" target="R" class="handle">
<span class="handle hnd">my-search-string</span>
</a> >
<button class="BlockButton">Block</button>
<time>12:03</time>
</div>
I get the error:
'not found'
How do I use the Selenium IDE to search for the string 'my-search-string' in the expression
<span class="handle hnd">my-search-string</span>
?

The correct XPath expression is
/html/body/div[2]/article[2]/div[1]/a[2]/span[contains(text(), 'my-search-string')]
Its output is:
my-search-string
You were missing a final ] and your index of the a element was off by one.

try this selector:
//span[text()='my-search-string']

You can use the "verifyAttribute":
Command: verifyAttribute
Target: target#class
Value: value
Example:
Command: verifyAttribute
Target: /html/body/div[2]/article[2]/div[1]/a[3]#href
Value: ?act=su&handle=my-search-string
OBS: I don't know if this works in the new IDE, i use the old version, 2.9.1.

Related

How to properly get the value contained inside a section using XPath?

having the following HTML (snippet grabbed from the web page I wanted to scrape):
<div class="ulListContainer">
<section class="stockUpdater">
<ul class="column4">
<li>
<img src="1.png" alt="">
<strong>
Buy*
</strong>
<strong>
Sell*
</strong>
</li>
<li>
<header>
$USD
</header>
<span class="">
20.90
</span>
<span class="">
23.15
</span>
</li>
</ul>
<ul>...</ul>
</section>
</div>
how do I get the 2nd li 1st span value using XPath? The result should be 20.90.
I have tried the following //div[#class="ulListContainer"]/section/ul[1]/li[2]/span[1] but I am not getting any values. I must said this is being used from a Google Sheet and using the function IMPORTXML (not sure what version of XPath it does uses) can I get some help?
Update
Apparently Google Sheets does not support such "complex" XPath expression since it seems to work fine:
Update 1
As requested I've shared the Google Sheet I am using to test this, here is the link
What you need is :
=IMPORTXML(A1;"//li[contains(text(),'USD')]/span[1]")
Removing section from your original XPath will work too :
=IMPORTXML(A1;"//div[#class='ulListContainer']/ul[1]/li[2]/span[1]")
Try this:
=IMPORTXML("URL","//span[1]")
Change URL to the actual website link/URL

Parsing through response created with XPath

Using Scrapy, I want to extract some data from a HTML well-formed site. With XPath I am able to extract a list of items, but I am not able to extra data from the elements in the list, using XPath
All XPath's have been tested using XPather. I have tested the issue using a local file that contains the webpage, same issue.
Here goes:
# Get the webpage
fetch("https://www.someurl.com")
# The following gives me the expected items from the HTML
products = response.xpath("//*[#id='product-list-146620']/div/div")
The items are like this:
<div data-pageindex="1" data-guid="13157582" class="col ">
<div class="item item-card item-card--static">
<div class="item-card__inner">
<div class="item__image item__image--overlay">
<a href="/www.something.anywhere?ref_gr=9801" class="ratio_custom" style="padding-bottom:100%">
</a>
</div>
<div class="item__text-container">
<div class="item__name">
<a class="item__name-link" href="/c.aspx?ref_gr=9801">The text I want</a>
</div>
</div>
</div>
</div>
</div>
When using the following Xpath to extract "The text I want", i dont get anything:
XPATH_PRODUCT_NAME = "/div/div/div/div/div[contains(#class,'item__name')]/a/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()
The output is empty, why?
Try the following code.
XPATH_PRODUCT_NAME = ".//div[#class='item__name']/a[#class='item__name-link']/text()"
products[0].xpath(XPATH_PRODUCT_NAME).extract()

Thymeleaf switch block returns incorrect value

I have a switch block in my thymeleaf page where I show an image depending on the reputation score of the user:
<h1>
<span th:text="#{user.reputation} + ${reputation}">Reputation</span>
</h1>
<div th:if="${reputation lt 0}">
<img th:src="#{/css/img/troll.png}"/>
</div>
<div th:if="${reputation} == 0">
<img th:src="#{/css/img/smeagol.jpg}"/>
</div>
<div th:if="${reputation gt 0} and ${reputation le 5}">
<img th:src="#{/css/img/samwise.png}"/>
</div>
<div th:if="${reputation gt 5} and ${reputation le 15}">
<img th:src="#{/css/img/frodo.png}"/>
</div>
<div th:if="${reputation gt 15}">
<img th:src="#{/css/img/gandalf.jpg}"/>
</div>
This statement always returns smeagol (so reputation 0), eventhough the reputation of this user is 7: example
EDIT:
I was wrong, the image showing was a rogue line:
<!--<img th:src="#{/css/img/smeagol.jpg}"/>-->
but I commented it out. Now there is no image showing.
EDIT2:
changed my comparators (see original post) and now I get the following error:
The value of attribute "th:case" associated with an element type "div" must not contain the '<' character.
EDIT3:
Works now, updated original post to working code
According to the documentation, Thymeleaf's switch statement works just like Java's - and the example suggests the same.
In other words: you cannot do
<th:block th:switch="${reputation}">
<div th:case="${reputation} < 0">
[...]
but would need to do
<th:block th:switch="${reputation}">
<div th:case="0">
[...]
which is not what you want.
Instead, you will have to use th:if, i.e. something like this:
<div th:if="${reputation} < 0">
<img th:src="#{/css/img/troll.png}"/>
</div>
Change
<div th:case="0">
<img th:src="#{/css/img/smeagol.jpg}"/>
</div>
to
<div th:case="${reputation == 0}">
<img th:src="#{/css/img/smeagol.jpg}"/>
</div>

How to use preceding sibling in xpath

Here i want to Accept button. Here is the HTML.
<div class="friend-request no-pad ng-scope" ng-if="notifications.friendInvites.length > 0">
<p class="rem-head mzero small">
<div class="reminder-lst lst-box ng-scope" ng-repeat="friendInvite in notifications.friendInvites | limitTo:limit">
<span class="img-frame img-circle">
<span class="pull-left rem-detail-a">
<a class="pull-left rem-detail-a pzero" href="friend#/friends/friendprofile/b6c70e4f-bfe1-440d-836c-2e8fdc88540e">
<span class="frndact pull-right">
<a class="ignore" ng-click="ignoreNotification(friendInvite, 'friend')" href="javascript:void(0)">
<a class="accept" ng-click="acceptNotification(friendInvite, 'friend')" href="javascript:void(0)">
<i class="fa fa-lg fa-check-circle green"></i>
</a>
I have tried using below xpath but not working. Can anyone plz help me?
#FindBy(xpath=".//a[ng-click='acceptNotification(friendInvite, 'friend')']/preceding-sibling::i[#css='.fa.fa-lg.fa-check-circle.green']").
Thanks in advance
Assuming that you are looking for the 'A' tag of class accept, you can try
//i[#class="fa fa-lg fa-check-circle green"]/preceding-sibling::a[#class="accept"]
or
//i[#class="fa fa-lg fa-check-circle green"]/preceding-sibling::a[#ng-click="acceptNotification(friendInvite, 'friend')"]
a couple of things:
as TT noted your xpath was missing the # for the attribute selector
the sample you posted is not a well formed xml, expect troubles with xpath if you don't have an xhtml compliant source.
if you use the second example mind to escape either the " or the ' quotes, if you use it inside another expression

Unable to click on list element from drop down

Using Selenium WebDriver, I'm trying to click on Manual Testing option from Testing menu from http://guru99.com/ Manual Testing option appears after hovering mouse on Testing. In order to achieve above, I did following -
testing = $driver.find_element(:xpath, "//li[#class='item118 parent']")
mt = $driver.find_element(:xpath, "//li[#class='item119']/a")
$driver.action.move_to(testing).move_to(mt).perform
mt.click
But, sometimes error for line #3 above code -
Selenium::WebDriver::Error::MoveTargetOutOfBoundsError: Offset within element ca
nnot be scrolled into view: (85, 21): http://guru99.com/software-testing.html
and sometimes error for line #4 in above code -
Selenium::WebDriver::Error::ElementNotVisibleError: Element is not currently vis
ible and so may not be interacted with
Please help me to resolve this issue without executing javascript.
Here is HTML code -
<li class="item118 parent">
<a class="item" href="/software-testing.html"> Testing </a>
<span class="dropdown-spacer"></span>
<div class="dropdown columns-1 " style="width:180px;">
<div class="column col1" style="width:180px;">
<ul class="l2">
<li class="item119">
<a class="item" href="/software-testing.html"> Manual Testing </a>
</li>
.
.
.
You can try with partial_link_text:
testing = $driver.find_element(:partial_link_text, "Testing")
$driver.action.move_to(testing).perform
mt = $driver.find_element(:partial_link_text, "Manual Testing")
mt.click

Resources