xpath not finding text inside <div><span> - selenium

.//*[contains(text(), "Apply")]
<input type="hidden" value="false" name="needsValidation"/>
<input type="hidden" value="false" name="fullValidationPerformed"/>
<div class="loadingBox hidden">
<div class="paneContent">
<div class="topButtons">
<div class="rightSide">
<div id="saveChangesButton" class="majorButton">
<div>
<div>
<div>
<span class="hidden"/>
Apply Changes
<span class="down"/>
</div>
</div>
</div>
</div>
</div>
Why is it that the xpath string I created doesn't find "Apply" here? It appears that my xpath statement only fails when the text I want to find is inside a "span" tag inside a "div" tag like this.
Can someone help me understand what I'm missing here please?

The reason that contains(text(), 'Apply') does not work is that the element has several text nodes, and the contains function in XPath 1.0 ignores all but the first. (XPath 2.0 would give you an error if there is more than one).
If you want to get an element whose string value contains 'Apply' without also returning its ancestors, the simplest way is to get the last element containing this string:
(//*[contains(., 'Apply')])[last()]

Related

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

Customize selection for collection of elements - Selenide

How can I select the on of element, and this element contains a collection of elements, How can I select or pass through this element's collection?
the default search method WebDriverRunner.getWebDriver().findElements(By.tagName("*"));
is $$("*")
they consider that default space to search is the whole document, how can I specify the region to search? Assume that I need the a tags from the region or from specific element.
I have the following schema for my page:
<div class="class1">
<a href="" />
<a href="" />
<img src="img.png" />
</div>
<a href="" />
<a href="" />
<div class="class2">
<a href="" />
<div class="class4"> <div>
</div>
How can I pass all elements of div class="class1", ex: after select div.class1 element, I need to see all elements inside this div.
You should find first div class="class1" then using this object find all element under this like below code:
WebElement el = WebDriverRunner.getWebDriver().findElement(By.className("class1"));
List<WebElement> elements = el.findElements(By.tagName("*"));
Let me know if this will not work!!!
Thanks
Sadik

Referencing v-model

I am trying to reference a v-model in one of my html files.
I've gone ahead and created a jsbin with a small example of what I'm trying to achieve:
https://jsbin.com/saqirekasa/edit?html,js,output
Essentially, what seems to be happening is that Vue gives an error like this in my actual project:
[Vue warn]: Invalid expression. Generated function body: scope.lookForUser({{scope.input_field}})
The problem (I believe) appears to be when I introduced this line:
<input type="text" class="form-control input-lg" placeholder="email-address" id = "button_email_submit" v-model = "input_field"/>
And then tried to reference the v-model 'input field' as such:
<button class="btn btn-info btn-lg" type="button" v-on= "click: lookForUser(#{{input_field}})">
Any ideas why Vue doesn't like this statement?
I figured this out with a bit more fiddling around -- the issue was I was not supposed to use #{{input_field}} but rather simply pass input_field text into the arguments.
Thanks! Here's my example code in case it helps anyone.
<div v-repeat="company: companies">
<div class="col-xs-12 col-md-6 col-lg-6">
<a href="#" v-on="click: selected_company_id = company.id">
</div>
</div>
I kept wanting to wrap the company.id in mustache brackets:
<a href="#" v-on="click: selected_company_id = {{company.id}}">
but I'm assuming that since it is already part of an expression, you don't need to do that.

Find out correct xpath

I have an html page like this:
...
<div class="container">
<div class="title">Meat</div>
<div class="someclass">
<div class="tile AAA">
<div class="text">AAA</div>
<img class="image" src="somewhat.jpg" />
</div>
</div>
<div class="someclass">
<div class="tile BBB">
<div class="text">BBB</div>
<img class="image" src="somewhat.jpg" />
</div>
</div>
</div>
<div class="container">
<div class="title">Fish</div>
<div class="someclass">
<div class="tile AAA">
<div class="text">AAA</div>
<img class="image" src="somewhat.jpg" />
</div>
</div>
<div class="someclass">
<div class="tile BBB">
<div class="text">BBB</div>
<img class="image" src="somewhat.jpg" />
</div>
</div>
</div>
...
I need to be able to get selenium click an img for a specific element (for example, i want Meat-BBB's img) but I can't find a way to get it by xpath
Any help will be appreciated...
This will click on the Meat-BBB's img:
driver.findElement(By.xpath("//div[.='Meat']/following-sibling::*[2]//img")).click();
It finds the 2nd sibling to the div element having text/innerHtml as "Meat", and then traverses to the first "img" element in it.
This will be the xpath for your Meat-BBB's img /html/body/div[1]/div[3]/div/img
You can find xpath of any element by following steps.
1. Open your HTML file with any browser
2. Press F12 and inspect that element by clicking on that element
3. Now right click in html panel on the element and click on Copy XPath
Done! Now you have XPath.
The xpath will be - yourText
//div[div[text()='yourText']]/descendant::img
To make this Xpath more Generic , we can define a string like
// assign the value under which you want to click image Meat or Fish
String classTitle = "Meat";
and chage the xpath as follows:
//div[.='+classTitle+']/following-sibling::*[2]//img")

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.