Selenium WebDriver and xpath locating inside WebElement - selenium

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'.

Related

How to retrieve the text of a hidden element using selenium

Here we know the Birimingam value. Based out of that value need to retrieve the text "BHM". Have indicated this in the image. How to retrieve hidden text which is "BHM" using Birimingham value xpath?
Try this xpath to access the element
//span[text()='Birmingham']/../../preceding-sibling::span/span

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')]

How to get XPath of an element when it is in sub tab (Salesforce Lightning component)

In the following snippets, XPath for the drop down element in last column of accounts table is similar in Subtab and Main page.
I am using the XPath expression
//table/tbody/tr[2]/td[10]/span/div/a[2][#role="button"]
to click on drop down element. But unable to do so as it happens to appear in the previous page too.
Please help me in identifying unique irrespective of tabs.
You can use this XPATH :- (//table/tbody/tr[2]/td[10]/span/div/a[2][#role="button"])[1]

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

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

Get text associated with check box in selenium where html code does not have value or name property

I have a Radcombobox in my application which has several check boxes in the combo box. What I wish to do is to fetch the text associated with the check box. I searched on internet and came to know that text can be fetched from name or value property in HTML code, but problem is that my HTML code does not have such properties.
HTML code:
input class="rcbCheckAllItemsCheckBox" type="checkbox"
Check All
What i wish to do is to fetch value "Check All".
Using code x = driver.findElement(By.xpath("/html/body/form/div[1]/div/div/div/input")).getText();, value returned is blank.
You can get the text through JavaScript API of Rad Controls. You can check the official documentation- http://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/client-side-programming/overview
Basically, you first locate the element in JS and then use the official control method, in your case get value method. You can also perform several other useful operations if you need to.
You can execute JS in WebDriver with the following code:
IWebDriver driver;
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");