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

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");

Related

How to add text in pop up window in selenium

I am trying to test a website using selenium(with java). On my website, there is an option to add a new class. When we click on the add new option button, a pop-up window will come and we can enter our new class name. But using selenium I cannot enter alphabets into the pop-up window. I can only enter numbers in that field. The field accept both alphabets and numbers when we enter the data manually. How can I add alphabets to the pop window in selenium?
Here I attach my code below :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"modal-add\"]")));
driver.findElement(By.xpath("//*[#id=\"class\"]")).sendKeys("LKG");
driver.findElement(By.xpath("/html/body/section/div/div[3]/div/div/form/div[2]/button[2]")).click
screenshot of the field - Class_field_screenshot
As mentioned above you are not able to add alphabets in textfield using sendkeys function. I haven't heard this type of issue before please confirm that either that field accept alphabets or not. if that feild accept alphabets then try using another way like javascript executor to send text as mentioned below
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#id').val("sendtext")");
Can you try Clipboard class for copy and paste string value in class field ? Make sure you click first in class text field and then use paste code. As you say by keyboard you are able to enter alphabet so try to use keys class too.In both cases your keyboard is going to use so may be it helps.

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

Not getting value from gettext()

I have used getText(); but not working please help me out
Text Box 1: I need to get the value from this text box.
String value = driver.findElement(By.xpath("html/body/div[5]/div/div/div/div/div[2]/div/div[1]/input")).getText();
Text Box 2: I wanted to put it here but value is not coming here.
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/div/div/div[2]/div[1]/div[1]/form/div/input")).sendKeys(value);
Any help is appreciated, thanks in advance.
getText() is not working in many occasions. You can use .getAttribute("value") to get the text.
Make sure your first text box is not inside an iframe and is visible accessible. If it is, then its possible that your xpath might not be correct. Instead of using system generated paths or the full path , try using css and other locators to identify the element. What browser are you using ? IE is sometimes slow to respond to the sendkeys method. Use chrome instead and if that is not an option, try copy pasting using the below code
.sendKeys(Keys.CONTROL,"v");
public void copyToClipBoard(String data) {
// TODO Auto-generated method stub
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
StringSelection selectedData = new StringSelection(data);
clipboard.setContents(selectedData, null);
}

How do I create a logic for href followed by text

Passed
How do I find the element based on the text followed by the href?
I have tried this
IWebElement ele = Driver.driver.FindElement(By.XPath("//a[contains(#href,'grdCoverageSelected' and text(),'Passed']"));
ele.Click();
Also, there are some other controls like
Passed,TBD,Pending,and failed
Also, need to implement a logic if I find passed then it will go to next page and if I will find any other of the tags then it will click the hyperlink and change from TBD to PAssed or failed to passed or pending to passed from the drop-down list
Please try with below solutions.
using xpath = //a[#href='href url'][contains(text(),'text')]
using css = div:contains("Click here") or div>.className:contains("Click here")

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