Selenium XPath of preceding element - selenium

I have a scenario wherein the html tags are generated dynamically through ajax as shown below
<div>
<span class="rptName">QLMS</span>
<span id="button_id_91">//generated dynamically
<span id="button_span_right_91">
<span id="button_91"/>
</span>
</span>
<span class="rptName">QLRS</span>
<span id="button_id_92">//generated dynamically
<span id="button_span_right_92">
<span id="button_92"/>
</span>
</span>
</div>
The span elements are generated dynamically, in such scenarios how do we get the xpath based on the text search(QLMS,QLRS present in span).
I need to do an click event on the
<span id="button_92" or "91"/>
Please let me know how to achieve in such scenario.

Seems pretty tricky. Using following-sibling should help. I am also filtering out the span with the partial id right. Try this:
//span[contains(text(),'QLRS')]/following-sibling::span//span[not(contains(#id,'right'))]

Related

Finding an element with a xpath that changes

Hello I am new to selenium and trying to find an element on a dropdown that expands to a list. The xpath constantly changes so cannot use it. Also tried the full xpath and did not work either. Other drop downs that I have used I have been able to use the cssselector but really do not have much to work with in here. I have tried with 'Account Maintenance' text but it did not work or did not do it properly. If I need to provide any additional information or more html please let me know.
<li class="k-item" role="treeitem" data-uid="706aa3e9-cd95-45c3-9ad0-e990887f9484" aria- selected="true" aria-expanded="false">
<div class="k-mid">
<span class="k-icon k-i-expand">
::before
</span>
<span class="k-in k-state-selected">Account Maintenance</span>
</div>
<ul "THIS IS THE LIST THAT EXPANDS AFTER CLICKING ON THE 'Acount Maintenance' text above but not my issue </ul>
</li>

Selenium and Xpath - accessing elements adjacent to each other

In the example below I'm trying to click on Follow Me where the adjacent(ish) <div> is equal to David. This is one of many <div class='_1m' on the page all with the same structure.(does that make sense?)
Sorry first time posting a problem and a forgot one major detail. I know that I'm looking for David but I don't know what the 'Follow Me' Value will be. It changes on each record.
<div class="_1m">
<div class="_8h"><img src="/2323.GIF" /></div>
<div class="_1j">
<div class="_1c">David<span class="_13">abc</span></div>
<div>
<span class="_1v">ABCD</span>
<span class="_1v">1234</span>
</div>
<div>7890</div>
</div>
<div class="_3h">
<div class="_n0">
<span class="_bn"><span class="_la">Follow Me</span></span>
</div>
</div>
</div>
To click on Follow Me where the adjacent <div> contains the text David you can use the following Locator Strategy:
Using xpath and following:
//div[contains(., 'David')]//following::span[3]/span
Using xpath, following and the text Follow Me:
//div[contains(., 'David')]//following::span[3]/span[text()='Follow Me']
Use below xpath //div[#class='_1c'][contains(.,'David')]/../following-sibling::div//span[#class='_la'][contains(.,'Follow Me')]
Explaination:
//div[#class='_1c'][contains(.,'David')] loate the David
/.. move to one parent node of David because follow me emement is sibling of that parent div
/following-sibling::div locate immediate following sibling div
//span[#class='_la'][contains(.,'Follow Me')] Loate the span which has Follow me text

Xpath for div with span having a css class get its sibling anchor link

I want all div's anchor link whose span having css class ='block-diff-neutral'
<div class="file-info" xpath="1">
<span class="diffstat tooltipped tooltipped-e" aria-label="0 ">
<span class="block-diff-deleted"></span>
<span class="block-diff-deleted"></span>
<span class="block-diff-neutral" style=""></span>
<span class="block-diff-neutral"></span>
<span class="block-diff-neutral"></span>
</span>
someText
</div>
<div class="file-info" xpath="1">
<span class="diffstat tooltipped tooltipped-e" aria-label="0 ">
<span class="block-diff-deleted"></span>
<span class="block-diff-deleted"></span>
</span>
someText2
</div>
Here div contains 2 subtags i.e. span and a
if span contains a css class as 'block-diff-neutral' only then get the a tag's title attribute => xpath with this condition is required
Expected output is => someText or file1
Your markup looks invalid (it's missing some closing tags) and it looks over convoluted in places (multi-layered <span> tags).
However this should do what you want if I've understood your requirements correctly.
//div[#class="file-info"][./descendant-or-self::span[#class="block-diff-neutral"]]/a
if the span with a missing closing tag is actually a parent of the anchor this would be better:
//div[#class="file-info"][./descendant-or-self::span[#class="block-diff-neutral"]]/descendant-or-self::a
This will find a div with a class of file-info that has a descendent <span> element with the class block-diff-neutral and then find the anchor inside that div element.
To get the title attribute out of the WebElement you would find with this XPath you will need to use .getAttribute("title")
Try the following Xpath.
//span[#class='block-diff-neutral']/parent::span/parent::div/a
Or use following sibling
//span[#class='block-diff-neutral']/parent::span/following-sibling::a
If you are using python use following code and use any of the xpath locator above.
driver.find_element_by_xpath("//span[#class='block-diff-neutral']/parent::span/parent::div/a").text
driver.find_element_by_xpath("//span[#class='block-diff-neutral']/parent::span/parent::div/a").get_attribute("title")
If you are using java try below code and any locator mentioned above.
driver.findElement(By.xpath("//span[#class='block-diff-neutral']/parent::span/following-sibling::a").getText()
driver.findElement(By.xpath("//span[#class='block-diff-neutral']/parent::span/following-sibling::a").getAttribute('title')

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.

how to click on a button when specific image is asscoiated with it

I am using selenium for testing my application.
In my application there are 5 buttons, each have a different image associated with it.
I want to click on button which have a specific image associated.
Currently i am using a while loop to get the node of image and then replacing this node into xpath of button to select it.
is there any way either with xpath or css to do this directly.
Providing more information-this is like submit button is there and then below this image is there. submit button and images are sibling element and need to click submit button when the next element is specific image
<div class="select">
<span class="sysTxtBtn submit xxs">
<span class="btnTagDummy">
</span>
<div class="specialRateMarking">
<img width="79" height="11" alt="Marking2" src="someimages"/>
</div>
<div class="select">
<span class="sysTxtBtn submit xxs">
<span class="btnTagDummy">
</span>
<div class="specialRateMarking">
<img width="79" height="11" alt="Marking1" src="someimages"/>
</div>
Could you include a snippet of your HTML? Below is an example of an image in a form and a few ways of locating it using Selenium, but these may not be relevant depending on your implementation:
<input id="submitForm" name="imgbtn" type="image" src="images/submit.png" />
id=submitForm
name=imgbtn
//input[#src='images/submit.png']
//input[contains(#src, 'submit.png')]
css=input[src='images/submit.png']
UPDATE:
Given the HTML:
<div class="select">
<span class="submit">
<div class="marking1"></div>
<div class="select">
<span class="submit">
<div class="marking2"></div>
You can locate the 'submit' span parent of the 'marking2' div using the following XPaths:
//div[#class='marking2']/..
//div[#class='marking2']/parent::*
//div[#class='marking2']/parent::span
UPDATE 2:
Based on the HTML now included in the question, you can locate the span with the class of submit related to the image many ways, a few examples follow:
//div[//img[#alt='Marking2']/span[contains(#class, 'select')]
//img[#alt='Marking2']/../../span
//div[img[#alt='Marking2']]/preceding-sibling::span
I hope this gives you some ideas. I'd certainly recommend XPath over CSS for locating these elements as it's much better at these sorts of relationships.