protractor using xpath //*[contains('text')] error element not visible - xpath

Hi I have this element from a dropdown menu I try to select:
<div class="tt-suggestion tt-selectable">
<strong class="tt-highlight">Auto Customer</strong>
</div>
If I use element(by.xpath("//strong[contains(text(),'Auto Customer')]")).click(); I can select it no problem. But if I use element(by.xpath("//*[contains(text(),'Auto Customer')]")).click(); I get "Failed: element not visible"
Can someone explain this to me please?
Thank you

Because the * in //*[contains(text(),'Auto Customer')] means any tag, not only the strong Tag. But //strong[contains(text(),'Auto Customer')] must be strong Tag.
//*[contains(text(),'Auto Customer')] should find more then one elements on page, and the first one is not visible. You can try this xpath in Chrome DevTool's Element Tab to see how many elements it can find and the first one is visible or not.

Related

How to check if div contains text (XPath)

I use https://autorefresh.io/expressions/ chrome extension for page refreshing. The next level for me is checking if a specific page part contains the predefined text. This extension supports XPath.
I was trying to use //*[contains(text(),"Brak towaru")]. It worked until I put this cell "address" instead of *.
/html/body[#class='b--desktop breakpoint-xl']/div[#class='body-inner']/
section[#class='pt-2']/div[#id='main']/div[#class='shop']/
div[#class='row mt-4 justify-content-center'][1]/div[#class='col-4 col-md-2 pt-3 text-center']
I was trying to insert this "address" in many ways... No success.
Any hints?
screenshot of XPath extension
Instead of using contains(text()) try using contains(.,). This will check for text content in the element itself and in it's child nodes.
So, instead of
//*[contains(text(),"Brak towaru")]
Try using
//*[contains(.,"Brak towaru")]
And in case this is a div element use
//div[contains(.,"Brak towaru")]
Thank you very much. Works :)
//*[#id="main"]/div/div[3]/div[3][contains(.,"Brak towaru")]

Can't get xpath to locate element

I have this piece of HTML and I'm trying to select the <a href> link using xpath.
<li class="footable-page-nav" data-page="next" aria-label="next"><a class="footable-page-link xh-highlight" href="#">›</a></li>
I need the selector to be reasonably specific since "footable-page-link" exists in multiple places in the HTML.
I've tried this:
//li[#class='footable-page-nav']/a[#class='xh-highlight']//#href
Selenium throws an error: selenium.common.exceptions.NoSuchElementException
If I shorten the xpath expression to //li[#class='footable-page-nav'] just to see if I'm on the right track then I get
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size
What am I missing?
Try changing your xpath expression to
//li[#class='footable-page-nav']/a[contains(#class,'xh-highlight')]//#href
and see if it works.

How to get number of list element (ul tag) of HTML using Get matching XPath count?

I'm kind of new to XPATH-query. I use RF and selenium2library and the XPath Helper-plugin in chrome to test a certain website page. I'm new to HTML/CSS/JavaScript as well.
The web page consists of two ULs (lists) for left and right sides of the page and each one has a few LIs which have few divisions comprised of widgets (JPEG images etc).
I need to count this list rows (number of LIs in each UL). I have already done the samething in a drop down menu to count its elements with no problem (perhaps because it was considered
a web element). But right now I use the same "Get Matching Xpath Count" which returns almost the whole page HTML source instead of a number and it then fails.
All my program is based on getting the number of LIs in a UL (of drop down menu, page, table,...). so I wonder what to do now. Here is an example of the HTML code of the page:
<ul class="rqcol" id="col8a580456553ae">
<li class="rqportlet" id="por8a58045655">
<div id="hdrpor8a580" class="rqhdr" onmouseover="RQ.util.showTools(this)" onmouseout="RQ.util.hideTools(this)"> </div> </li>
<li class="rqportlet" id="por8a580456" >
<div id="hdrpor8a581" class="rqhdr" onmouseover="RQ.util.showTools(this)" onmouseout="RQ.util.hideTools(this)"> </div></li>
</ul>
and my code was:
Get Matching Xpath Count | //ul[#id="ccol8a580456553ae"]/li
which does give me some texts plus HTML code.i also tried:
Get Length | //ul[#id="ccol8a580456553ae"]
which doesn't give me 2 but a big number.
An XPath 2.0 expression to count the 'li' for the specific '' would be:
//ul[#id="col8a580456553ae"]/count(li)
Try this new chrome extension
https://chrome.google.com/webstore/detail/relative-xpath-helper/eanaofphbanknlngejejepmfomkjaiic
You've made a typo in the id value - an extra "c" char in the beginning; otherwise the xpath is correct:
${count}= Get Matching Xpath Count //ul[#id="col8a580456553ae"]/li
By the way, the keyword Get Matching Xpath Count is deprecated in the latest version of the SeleniumLibrary, in favour of Get Element Count

How to access second element using relative Xpath

Given this page snippet
<section id="mysection">
<div>
<div>
<div>
<a href="">
<div>first</div>
</a>
</div>
<div>
<a href="">
<div>second</div>
</a>
</div>
</div>
</div>
</section>
I want to access the second a-element using relative Xpath. In FF (and locating with Selenium IDE) this
//section[#id='mysection']//a[1]
works but this does not match
//section[#id='mysection']//a[2]
What is wrong with the second expression?
EDIT: Actually I do not care so much about Selenium IDE (just use it for quick verification). I want to get it going with selenium2library in Robot Framework. Here, the output is:
ValueError: Element locator with prefix '(//section[#id' is not
supported
for the suggested solution (//section[#id='mysection']//a)[2]
You can use this. This would select the anchor descendants of section and get you the second node. This works with xslt processor, hope this works with Selenium
//section[#id='mysection']/descendant::a[2]
Try this way instead :
(//section[#id='mysection']//a)[2]
//a[2] looks for <a> element within the same parent. Since each parent <div> only contains one <a> child, your xpath didn't match anything.
With this:
//section[#id='mysection']//a[1]
you are matching all first 'a' elements within any context (inside one div, for example), but with this
//section[#id='mysection']//a[2]
you are trying to match any second 'a' element with any context, but you dont have more than one 'a' element in any of nodes.
The icrementing sibling node thus should be a parent div node to those 'a' tags.
Very simple:
//section[#id='mysection']//a[1] - both elements
This is why previous answer with paranthesis around the whole thing is correct.
//section[#id='mysection']//div[1]/a - only first element
//section[#id='mysection']//div[2]/a - only second elemnt
Other way to mach each 'a' separately:
//section[#id='mysection']//a[div[text()='first']]
//section[#id='mysection']//a[div[text()='second']]
Other ways to reach to the second a-element can be by using the
<div>second</div>, call this bottom-up approach
instead of starting from section-element
<section id="mysection">, call this top-down approach
Using the div child of a-element, the solutions should look like this:
//div[.='second']/..

I have a button where the text is in span, how to find it?

Actually its not a button ( it acts as a button) but a text(i.e a domain name) where when a user click on it, it will go to next section .
This is the html:
<div id="domainCon2TitleLabel" class="tLabel">
<span>qa.xyz.com</span>
</div>
I have tried giving the xpath as
wd.findElement(By.xpath("//span[contains(text(),'qa.xyz.com')]")).click();
but showing error as
Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'qa.xyz.com')]"}
can any let me know how to identify the xpath for that span.
Try the xpath expression:
//*[text()[contains(.,'qa.xyz.com')]]

Resources