Not getting value from gettext() - selenium

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

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.

How to handle Autosuggestion text field having dropdown list in Selenium?

There is a text field called "Choose an Industry" on clicking inside it will show list of dropdown values.
Attaching the screen shot for ref
There are a couple issues with the code you posted. With a little clean up it may work but there are methods provided by Selenium via Select that you should take advantage of.
String expectedValue = "Adverstising Services";
Select dropDown = new Select(driver.findElement(By.xpath(".//*[#id='select2-chooseInd-container']")));
dropDown.selectByVisibleText(expectedValue);
You can read more about Select and the methods available in the docs, here: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html

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

Checkbox does not get checked

Hi I am automating a webpage which contains multiple checkbox.
It clicks on a some checkbox and misses some checkbox.
This is my code.
Should I put a wait statement before the click to avoid this problem.
IWebElement ClickElement = Wait.Until((d) => webDriver.FindElement(By.Id(parameter1)));
ClickElement.Click();
Can you try making this change in your code -
In wait until function you are checking if the element exists by using findElement(By.Id(parameter1))
After finding the WebElement check if this is displayed by using isDisplayed() method with in the waitUntil function.
You can also check if it has already checked or not by using isSelected() method.

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());