I'm trying to select a button with id of "some-id" that also contains this text: "some-text", however, this selector isn't working am I'm not sure why:
//button[contains(#id, "some-id") and contains(., "some-text")]
What's the proper selector to do this?
Here's a DOM example:
<main>
<button id="some-id">
<svg>...</svg>
<i />
some-text
</button>
</main>
The selector returns no elements and I'm not sure why. Could it be related to new lines within the <button /> element?
This selector does select the element:
//button[contains(#id, "some-id")]
Related
Alright, I have a item which has this class class="country" and there are 12 elements with the same class. Now I want to get a element on its value. For example Italy. And now I want to click on a link in this item. The class of the link is class="link". So basically I want to click the link of the item with the name Italy
My code at the moment:
cy.get('.country').should('have.text', 'Italy').click();
HTML
<div class="countries">
<div class="text">
<h3></h3>
<div class="country">Italy</div>
<h4>Yala</h4>
<p>test</p>
<a class="link" href="/mysite">Show details</a>
</div>
</div>
Should() is an assertion and won't select the element you want.
you probably want the contains() function.
cy.get('.country').contains('Italy').click()
Best
When executing the test case, this error is shown:
Button with locator 'xpath=//*[#id="stBanner"]/div[2]/a[2]' not found.
The element doesnt't have an id, and I have to use Xpath, but it can't be found.
This is the code when I inspect the element:
<div class="stRight">
<span id="mobileSearchIcon" class="glyphicon glyphicon-search"></span>
<!-- Logged in --><!-- Logged out -->
<a class="user-sts-link" href=" uri=nm:oid:Z6_72A2IA80O0US40QOM4JF0F30O3">REGISTER</a>
<a class="user-sts-link" href="?uri=nm:oid:Z6_72A2IA80O0CSB0Q4ODDFDQ0081">LOGIN</a>
</div>
This is the xpath:
//*[#id="stBanner"]/div[2]/a[2]
This is the testcase:
SeleniumLibrary.Open Browser #{tst3Url}[0] firefox
SeleniumLibrary.Click Button xpath=//*[#id="stBanner"]/div[2]/a[2]
Don't use the Click Button keyword - it is strictly for html elements of the <button> type.
Instead, use Click Element - your target element is an <a>, and with Click Elements the browser will execute the click on it.
Given following markup
<div>
<a>Username1</a>
</div>
<div>
<button>Unblock</button>
</div>
<div>
<a>Username2</a>
</div>
<div>
<button>Unblock</button>
</div>
<div>
<a>Username3</a>
</div>
<div>
<button>Unblock</button>
</div>
How do I select button element which is a cousin of a element with text Username2?
I can select the a element with //a[contains(., 'Username2')], so I thought that //a[contains(., 'Username2')]/following-sibling::/div/button would select the correct button, but that does not work. I think that it's not even valid XPATH.
You were close:
//a[contains(., 'Username2')]/../following-sibling::div[1]/button
To navigate to the cousin you first have to go to the parent (..) and then to its sibling.
Note that the following-sibling:: axis selects all following siblings, not only the first one. This means you must use [1] if you just want the first.
This would also work:
//a[. = 'Username2']/../following-sibling::div[1]/button
So would this:
//div[a = 'Username2']/following-sibling::div[1]/button
This is the code:
<li>
<a>
<h1>Quorn Stukjes</h1>
<p class="price">
</a>
<form>
<button type="submit">+</button>
</form>
</li>
I want to create a locator that finds the first <h1> that has an sibling element <p> with an attribute "price". Easy so far. But now I also want that <h1> to share its grandparent with a <button> class with the attribute type "submit".
What I created was the following:
//a/p[#class="price"]/preceding-sibling::p/preceding-sibling::h1
I'm wondering if this is the most sensible solution (it does work), or if there is something more elegant and robust.
(//*[form/button[#type = 'submit']]/*[p[#class = 'price']]/h1)[1] should do (assuming a submit button only makes sense in a form parent element).
I'd like to select the following HTML in a document, based on the content of TARGET. I.e. if TARGET matches, select everything. However, I'm not sure where to go after: id('page')/x:div/span/a='TARGET' – How to use parent, child, and sibling expressions to get the containing div, the a preceding that div, and the two br tags following the div
<a></a>
<div>
<br />
<span>
<a>TARGET</a>
<a></a>
<span>
<span>
<a></a>
</span>
<a></a>
<span></span>
</span>
<span>
<a></a>
</span>
</span>
</div>
<br />
<br />
Use a single XPath like:
"//*[
(self::a and following-sibling::*[1][self::div and span/a='TRAGET']) or
(self::div and span/a='TARGET') or
(self::br and preceding-sibling::*[1][self::div and span/a='TARGET']) or
(self::br and preceding-sibling::*[2][self::div and span/a='TARGET'])
]"
Do note that your document is not well formed due to unclosed br tags. Moreover, I didn't include any namespace, which you can add if necessary.
Probably, you should first find all divs (not sure about conditions should be met):
//div[span[a[text()="TARGET"]]][preceding-sibling::*[1][name()="a"]][following-sibling::*[1][name()="br"]]
after that - all related elements for each div:
./preceding-sibling::a[1]
./following-sibling::br[1]
./following-sibling::br[2]