I'm trying to select partially an element with xpath in my selenium code.
My xpath is
//iron-pages[#id='pages']//span[.='s8718216']
what I want is to select any element starting with s, the element just after span.
I tried this:
//iron-pages[starts-with(span[.='s'])
It doesn't work for me.
Can someone help me.
I think this xpath should work //iron-pages[starts-with(text(),'s')]
Or second try:
//iron-pages[starts-with(.,'s')] <- . instead of text() checks element for more properties. Not only text.
There are many properties that might contain text like innerText, innerHTML etc.
EDIT:
I just read your question again. You want to select element right after span so:
//iron-pages[#id='pages']//span[starts-with(text(),'s')] <- it will select span elements starting with text s.
If you want child elements you can use
//iron-pages[#id='pages']//span//*[starts-with(text(),'s')]
Your xpath should be
//iron-pages[starts-with(span[.='s'])//following-sibling::i[1]
it will get the next element that start with span with text s
Related
I have multiple CSS webelements whose ids have the following form:
cell_[number]-button_[number]
I want to select ALL such webelements by asking Selenium to find all elements that start with cell and contain button:
var elements = Util.driver.findElements(By.cssSelector("[id^='cell_'] and [id*='button']"));
Is this the correct way to do it? Running this code returns a list with zero elements, so something is wrong.
This worked for me:
"[id^='cell_'][id*='button']".
I have a really simple ask, i have a div in a html page that i can acces by his XPath which is : //div[2]/div/div[2]/div[2]/div[2].
I want a XPath that would give all the child of this div but i can't find it.
I need to get the element with the findElements method of Selenium, but what i've tested does not work:
My HTML code looks like this :
<div>
<input/>
<span/>
<p></p>
</div>
And the XPath I want to use like this :
//div[2]/div/div[2]/div[2]/div[2]/child
And my Java Selenium script like this :
List<WebElement> listElement = driver.findElements(By.xpath(xpath));
for(WebElement element : listElement) {
System.out.println(element.getAttribute("id"));
}
What XPath should I put to get the child of the div?
EDIT 1: I did use the * and it's working, but when i count the number of element it prints me 6. Does the * consider the children of his children as his own children ??
If the div in the HTML fragment in your question is located at
//div[2]/div/div[2]/div[2]/div[2]
then the input child element would be here:
//div[2]/div/div[2]/div[2]/div[2]/input
and all of the children elements would be here:
//div[2]/div/div[2]/div[2]/div[2]/*
Update:
EDIT 1: I did use the * and it's working, but when i count the number
of element it prints me 6. Does the * consider the children of his
children as his own children ??
No, div/* selects only the immediate children elements of the parent div.
If you're being surprised by a greater number of children than expected, it may be that the base XPath is selecting multiple elements, and you're then selecting the children of more than just the targeted div element.
Update 2:
If you cannot post a MCVE, and you're still puzzled about the number of children returned by,
//div[2]/div/div[2]/div[2]/div[2]/*
try challenging yourself on the XPath you've provided as the base:
//div[2]/div/div[2]/div[2]/div[2]/
First, try
//div[2]
Does it really select the single div as you expect?
Then try
//div[2]/div
Again, see if this one really selects the single div you expect.
Continue in this manner until you get to a place where the reality of the selected elements deviates from your expectations. There your answer will be obvious, or your have a more specific question to ask.
Without seeing your XML/HTML, this is about as good as the advice can get.
List<WebElement> childs = driver.findElements(By.xpath("//div[2]/div/div[2]/div[2]/div[2]/*"));
In that case selenium searchs for all child elements from path div[2]/div/div[2]/div[2]/div[2]/ and set them in list collection as WebElement object.
I'm having a problem in this element (drop-down image), I have tried this elements but none of this works maybe because the two elements are the same and their id's are dynamic.
I tried getting the absolute Xpath and it works. (But this this is not a good idea)
Here's the element i have tried so far:
//div[#class='drop-image']
//div[contains(#class,'drop-image')]
Any help will be much appreciated.
Thanks guys!
Your parent div id is different.. You can use same..
Your 1st element parent id have sub-string as BaseCurrencyCombo while your 2nd element have TargetCurrencyCombo
1st element
//div[#id=ct100.....BaseCurrencyCombo]//div[#class='drop-image']
2nd element
//div[#id=ct100.....TargetCurrencyCombo]//div[#class='drop-image']
OR
//div[#class='drop-image'][1]
Please enter the correct/complete id in above both Xpath .. It's too long ;)
Hope it will help you :)
You said the div id's are dynamic ....however have u observed for the first part(USD) there is something called BaseCurrencyCombo under div ID whereas for the second part(EUR) it is TargetCurrencyCombo. So I don think so these two will get changed.
You can try these xpaths :
1>//div[contains(#id,'BaseCurrencyCombo')]//div[contains(#class,'drop-image')]
2>//div[contains(#id,'TargetCurrencyCombo')]//div[contains(#class,'drop-image')]
Well...I think I still haven't figured out how xpath works. I am trying to select the below element so that I can click on the second page. But what I am trying does not work. What is wrong here? I am selecting the a tag and then trying to find the one which contains ref whose value is next.
element = driver.findElement(By.xpath("//a[contains(#ref,'next')]"));
I probably see a typo there. The attribute you are trying to use is rel which has value next
So the correct xpath will be
//a[contains(#rel,'next')]
I want to select only the atomic values inside a node. For example, the "here" text in the following:
here
When I use Xpath in Java, it returns some sort of object/array, such as
[DomNode[here]]
I just want the text only.
Is this possible, and how? Thanks!
You can use the text() node test. For example, if you want to select the text of all anchors with an href attribute you could do the following XPath query:
//*/a/#href/../text()
Use:
//a[#href='']/text()
This selects all text nodes that are children of any a element in the document, that has an empty href attribute.