Find my span text using xpath - selenium

<body>
...
<div class="first-class">
<ul class="container-class>
<li>
<a><span>Not it</span></a>
</li>
<li>
<a><span>Target</span</a>
</li>
I want to create an Xpath to find the word "target". The number of list elements can vary so I can't use the id because it will change dynamically.
Here's what I've tried:
".//*[#class='first-class']/ul[li a//(text)[contains(.,'Target')]]"

You can try below XPath:
//a[.="Target"]
or
//a[span="Target"]

//a[contains(#span,'Target')]
Should be all it takes.

Related

XPATH Testing string value of child and parent has empty string value

I have html that I am searching against in a Selenium test where I want to find the child by its text only if the parent has no text:
Here's the HTML:
<ul>
<li>
<div>
"4"
<span>630</span>
</div>
</li>
<li>
<div>
<span>630</span>
</div>
</li>
<li>
<div>
"6"
<span>630</span>
</div>
</li>
</ul>
Selecting for the text string '630' yields three results using "//span[.='630']" or "//span[text()='630']", but I only want that middle one with an empty parent. I've tried a whole lot of constructions that don't work "//span[.='630' and parent::div[.='']]" or "//span[text()='630' and parent::div[text='']]" and even "//div[text()='']/*[1] | //span[text()='630']" but these all result in either 3 results or none.
Note that while "//span[text()='630'] and .//div[contains(text(),'')]" detects the text, it returns true, and I need it to return the element so that I can click it.
//span[text()='630']/parent::div[not(text())]
try this.
This worked for me:
//div[#innertext='']/span[text()='630']

How can I find out what number in <ul> some <li> element is? In Selenium

I have a < ul > with unknown number of < li > elements. Each < li > has some text and also a button. How can I identify which < li > contains the text I'm looking for, in order to click the button in that same < li >?
<ul>
<li class="ot-queryitem row-fluid">
<div class="ot-queryitem__info ot-queryitem__info--left span7">
<span class="ot-queryitem__name">Äänikysely (Voice Activity and Participation Profile)</span>
</div>
<div class="ot-queryitem__info ot-queryitem__info--center span3"></div>
<div class="ot-queryitem__info ot-queryitem__info--right span2">
<button class="ot-button ot-button--undefined pull-right ot-queryitem__button">
<i class="icon-pencil fa ot-icon ot-icon--default" style="margin-right: 8px;"></i>
<span>Täytä</span>
</button>
<span class="ot-queryitem__logo"></span></div></li>
<li class="ot-queryitem row-fluid">
<div class="ot-queryitem__info ot-queryitem__info--left span7">
<span class="ot-queryitem__name">Nikotiiniriippuvuustesti</span>
</div>
<div class="ot-queryitem__info ot-queryitem__info--center span3"></div>
<div class="ot-queryitem__info ot-queryitem__info--right span2">
<button class="ot-button ot-button--undefined pull-right ot-queryitem__button">
<i class="icon-pencil fa ot-icon ot-icon--default" style="margin-right: 8px;"></i>
<span>Täytä</span>
</button>
<span class="ot-queryitem__logo"></span></div></li>
//span[text()='Nikotiiniriippuvuustesti'] will give you a <span> with the given text
/ancestor::li axis will give you the parent <li> tag
/descendant::button axis will give you the <button> you're looking for
Putting everything together:
//span[text()='Nikotiiniriippuvuustesti']/ancestor::li/descendant::button
Demo:
References:
XPath Tutorial
XPath Axes
XPath Operators & Functions
Try the following xpath.
xpath: //ul//li[contains(.,'Nikotiiniriippuvuustesti')]//button
OR
xpath: //ul//li[contains(.,'Äänikysely')]//button
However You can pass this Nikotiiniriippuvuustesti or Äänikysely as string variable in robotframework.Hope this helps.
Both the element with the text and the button are child elements under each <li>. You need to go up the html tree from the text and then look for a button under a sibling element
//div[span[contains(., 'Äänikysely')]]/following-sibling::div/button
To identify li tag which contains text you looking for as -
${YOUR_TEXT} Nikotiiniriippuvuustesti
## this keyword will give first matching element
${elements}= Get WebElement xpath://span[contains(text(),'${YOUR_TEXT}')]/ancestor::li
### wait for element to be present on page
Wait Until Page Contains Element ${elements}
## now click on button inside li tag found in above lines
Click Button xpath://span[contains(text(),'${YOUR_TEXT}')]/ancestor::li//button
note: locators are taken from the html sample you provided
You don't have to count the number of <li> within the <ul>.
To click on the respective button according to the text e.g. Äänikysely, Nikotiiniriippuvuustesti etc you can use the following solution:
To click for Äänikysely:
Click Button xpath://div[contains(#class, 'ot-queryitem__info--left')]/span[contains(.,'Äänikysely')]//following::button[#class='ot-button ot-button--undefined pull-right ot-queryitem__button']//span[text()='Täytä']
To click for Nikotiiniriippuvuustesti:
Click Button xpath://div[contains(#class, 'ot-queryitem__info--left')]/span[contains(.,'Nikotiiniriippuvuustesti')]//following::button[#class='ot-button ot-button--undefined pull-right ot-queryitem__button']//span[text()='Täytä']

robot framework selenium get all names in a list

I'm fairly new to selenium. Please bear with me.
I have a html code as shown below. I'm producing only the relevant port.
<div id="id1" style="display: block;">
<ul>
<li id="id2" title="title1">
<ins class="icon"> </ins>
<a href="#" class="">
<ins class="icon"> </ins>
row1
</a>
</li>
<li id="id3" title="title2">
<ins class="icon"> </ins>
<a href="#" class="">
<ins class="icon"> </ins>
row2
</a>
</li>
.
.
.
</ul>
</div>
Using Robot framework selenium library, I have to display the names of the rows [row1, row2 etc...].
I am unable to find a suitable keyword which does this.
(note: the answer here refers to the original question. It was edited to ask something completely different so this answer no longer applies to the question in its present form)
You can use Get element count.
Using the html in the question as an example, it would look like this:
${count}= get element count //div[#id='id1']//li
That xpath says to find a div with the id "id1", then find any "li" elements anywhere under that div.

Selenium XPATH for dynamic class for dropdown

I am new to selenium and I am having trouble to write an xpath selector for this dropdown element:
<li style="" class="dropdown open" data-bind="visible: currentServers().length > 0">
<a id="employeeList" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true" data-bind="text: currentServerName()">employee servers</a>
<ul role="menu" class="dropdown-menu">
I have tried various xpath and it was not able to click on the dropdown.
Can any one help me with the xpath? I have used xpath with id , class, cssselector . But none of them worked.
If the root issue is the li element changes its class when opened - as stated in the comments, then the solution would be not to go after an exact match in it, but rather containing the looked for string. I.e.:
//li[contains(#class, "dropdown")]
Prepend/append any other elements as needed (e.g. an a element beneath it, if it is the one to relieve the click, etc)
Try to do this way.
Explanation: Your html inside the li tag, so start your xpath with li tag and then move ahead with a tag along with text method.
//li/a[contains(text(), 'employee servers')]

Webdriver: How to select anchor link under DIV?

In below code when I use //a[text()='My Process1' in Firebug it evaluates xpath and returns correctly, however when I try to use the same for finding an element using WebDriver I get NoSuchElement exception....any ideas what I might be doing wrong?
<div class="abbd" style="">
<ul class="New-type">
<li class="abcmenuitem" id="yui-gen10" groupindex="0" index="3">My Process1</li>
<li class="abcmenuitem" id="yui-gen11" groupindex="0" index="4">My Process2</li>
</ul>
</div>
webDriver.findElment(By.linkText("My Process1"));