How to write xpath based on element's text in Robot Framework? - selenium

I am using the Robot Framework and Selenium2Library
The button has a text of "Save" and there is nothing more unique in it's xpath, so I'm trying to write an xpath based on element's text.
How to write an xpath based on element's text with that piece of html:
<button class="slds-button slds-button--brand cuf-publisherShareButton NARROW uiButton" type="button" data-aura-rendered-by="1571:2954;a" data-aura-class="uiButton">
<span class=" label bBody truncate" dir="ltr" data-aura-rendered-by="1574:2954;a">Save</span>
</button>
(this is in the middle of the document).
EDIT:
It appears that there are few elements with the same text on the next tab (which was invisible at the moment).
How should I write the xpath for the second element with this text? I mean with index=1.

Click Button //button[.//text() = 'Save']
Is the "Robot Framework" way of finding a button with the text "Save" and clicking it.
Fixed from the help of #Tomalak
<3

Try searching for a button which contains a span with your required text
WebElement saveButton = driver.findElement(By.xpath(".//button/span[text()='Save']")

Use following xpath to find a button which have text as save -
//button[contains(.,'Save')]

Use following keyword, which will first verify whether element is present or not and then it will click on element located by locator
Element should become visible xpath=//button/span[text()='Save']
Click Button xpath=//button/span[text()='Save']
There is another built in keyword
Click Element xpath=//button/span[text()='Save']

Try using below xpath:
xpath=//span[contains(text(),'Save')]

Try this -
//span[text()='Save']/ancestor-or-self::button

Related

selenium send key for xpath not working

I want make automation for this web site
in this web site 3 text box are here check image
1st text box x path is /html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]
here is my code
driver.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]")).sendKeys("rio salon");
when I run this code I got this error
Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
How can i fix it? I hope my xpath is correct.
The field has aria-hidden=true attribute, so you get ElementNotInteractableException. You need to click on the dropdown first to change the attribute to true
WebElement dropdown = driver.findElement(By.xpath("//*[#id='search-form']/div/div[1]/div/div[1]/span"));
dropdown.click();
WebElement textField = dropdown.findElement(By.xpath("./parent::div/following-sibling::input[contains(#class, 'ui-select-search')]"));
textField.sendKeys("rio salon");
You can click in an input field with a div or span tag, but you cannot type in the field. So, your XPath must be written with an input tag if you want to sendkeys or type in an input field. For example:
//input[contains(#placeholder,'Site')]

Selenium xpath - Unable to find the xpath

I need to get the text "No results found, click tab to use entered text" from below html.
<div id="boundlist-1520-listEl" class="x-boundlist-list-ct" style="overflow: auto; height: auto;">
<ul></ul>
No results found, click tab to use entered text
</div>
When I tried with div id, its throwing the error.
Can anyone help me to get the xpath of this.
Many Thanks.
Regards,
Sudha Banakar
I guess Your id is changed dynamically so that you need to identify it by start with or contains method.Following XPath will work for you
"//div[starts-with(#id,'boundlist-')]
You can verify XPath by pasting it on your google chrome console
$x("//div[starts-with(#id,'boundlist-')]")
The xpath is :-
//ul[contains(text(),'No results found, click tab to use entered text')]
or best is
//*[contains(text(),'No results found, click tab to use entered text')]
If text displayed is not getting changed then you can use below xpath
//div[text()='No results found, click tab to use entered text']

How to select absolute link if two links are having same text in them?

I am having two link
Engagement Rings
Rings
The above links are check box under dropdown menu
how can i select Rings using selenium xpath using text() method
Below is what i tried, but it is selecting "Engagement Ring"
//li//a[contains(text(),'Rings')]
but it is selecting Engament Ring as it appears before Rings in menu
My Code:
//Select Sub-Menu item from collection dropdown
WebElement selectSub = driver.findElement(By.xpath("//div[#class='popover fade right in']//div[2]//li//a[contains(text(),'Rings')]"));
This is selecting Engagement Rings because you are using contains() method
it will locate the element which contains match text So your first checkbox has "Rings" text
Just change your xpath to :
//li//a[text()='Rings']
OR
//li//a[normalize-space()='Rings']
Please try below xpath i hope it will work
//li//a[starts-with(text(),'Rings')]

Selecting the correct "Add" button

I have a field that has a "+" Add button in case you want to add more lines. I want to add 2-3 lines with it, then to click on the "+" button from a newly created line to create 2-3 more. The problem is that all buttons are declared the same:
<button class="ng-scope" ng-if="formData.order_request_status == STATUSES['OPEN']" ng-click="addImportMaterial()" style="margin-left: 3px;" type="button">+</button>
I have written the following xpath:
//button[#ng-click='addImportMaterial()']
but this selects all plus buttons and I want only the third one to be pressed. Any ideas? Thanks!
You should try using xpath with index then as below :-
I want only the third one to be pressed
(//button[#ng-click='addImportMaterial()'])[3]
So, (Assuming your using java) use above xpath to locate third button and click as :-
driver.findElement(By.xpath("(//button[#ng-click='addImportMaterial()'])[3]")).click()
Since you will be getting a List of buttons, to press the third one you just have to do buttons.get(2).click();

Selenium WebDriver and xpath locating inside WebElement

I have a page containing multiple forms with their own submit buttons and other elements. While testing the page, I locate the second form
WebElement form = getDriver().findElement(By.id("form2"));
and then field and submit button
form.findElement(By.name("text")).sendKeys("Adding some text here");
form.findElement(By.xpath("//input[#type='submit']")).click();
However these xpath locations take effect on the first form. Is it really so that the xpath doesn't work inside a specified element?
Try a relative path:
form.findElement(By.xpath(".//input[#type='submit']")).click();
In fact Selenium works with the first found by xpath element. If you know exact order number you can add such a number to your xpath //input[#type='submit'][2]. Please note that numbering in xpath starts from 1 but not 0. So given xpath will found for you the second input with #type='submit'.