Selenium: delete contents from a textbox - selenium

Through selenium. how to delete contents from textbox.
I have to delete the last 2 characters from text box using selenium command.
Ex.ABCD to AB.

Try this -
selenium.type("text_box_object", "ABCD");
selenium.typeKeys("text_box_object", "\b");
selenium.typeKeys("text_box_object", "\b");

The keyPress event of selenium can be helpful:
selenium.sendKeys("text1", "ABCD");
selenium.sendKeys("text1", "\b");
selenium.sendKeys("text1", "\b");
This will Click Backspace key twice.

Read the current value and store it as a variable. Then 'Type' out the value that you want in the target field (using a substring of the stored value).

For firefox, the backspace event works only if you setCursorPosition at the END of the text in the textarea, otherwise the typeKeys event will type at the begining of the text.

Click onto it, hit end key and backspace twice

Related

Pressing enter key then inputting a text on a single line or condition

Inputting a text then pressing the enter key on a single line or using a single statement if possible .
tried separating the input of text and the pressing of the enter key
tried this but is it possible to make it just one statement?
WebElement department = driver.findElement(By.xpath("//input[#class='txtdepartment txtenter_event']"));
WebElement department1 = driver.findElement(By.xpath("//input[#class='txtdepartment txtenter_event']"));
department.sendKeys("10");
department1.sendKeys(Keys.CONTROL,"a");
Code
WebElement department = driver.findElement(By.xpath("//input[#class='txtdepartment txtenter_event']"));
department.sendKeys(Keys.chord(Keys.CONTROL, "a"), "10");
I want to input the 10 first before the keys.control or pressing enter key
sendKeys method accepts variable number of arguments of type CharSequence
You can use
department.sendKeys("10", Keys.ENTER)
Or
department.sendKeys("10", Keys.RETURN)
Selenium will send the key sequence as first argument, followed by second and so on...
Java Documentation
You can just send them one by one. (I changed it to python code :) )
department = driver.find_element_by_xpath("//input[#class='txtdepartment txtenter_event']")
department.sendKeys("10")
department.sendKeys(Keys.CONTROL)

odoo button definition not working before setting value to a mandatory field

I have a button say 'calc_val' when I click on the button I had to pass values to a mandatory field.But my button definition is not working while the mandatory field is empty.Only if I put some values to the mandatory field and after when I click on the button , the button works.The whole thing is in a 'wizard'
Please help.Thanks in advance.
Because in odoo after clicking on any button create or write function called before executing the working function of your button and for create and write you have to fill mandatory fields thats why you have to fill mandatory fields before executing any button functionality.
Hello, vbt
You have to set some default value in that specific field.
After set default value, you can use that button behavior.
And if you want to change that value, they can change also.
May Help this answer.
Thank you.
I have done this by removing the mandatory field and then adding a validation while clicking on the final button in the wizard (For not keeping the mandatory field empty).
If any other answers are available,feel free to post.Thanks

How to use auto suggest text box in selenium?

Code I found on net for the same is :
driver.findElement(locator).click();
driver.findElement(locator).sendKeys(value);
driver.findElement(locator).sendKeys(Keys.TAB);
But I don't know which value I have to enter and what is Keys in 3rd line
driver.findElement(locator).click();
driver.findElement(locator).sendKeys(value);
driver.findElement(locator).sendKeys(Keys.TAB);
driver.findElement(locator).sendKeys(Keys.ENTER);
You have to enter a value to suggest from text box.
Ex: If you need to suggest your state, you can enter state name first letters.
Then the "key" mean selenium webdriver command. you can specify which key should press.
Ex: Manually you have to enter the value and press down arrow twice and press enter.Like this you can specify the keyboard keys.

Selenium, using xpath to click a button with a similar content

I want to click a button that contains "Add" as text.
ie:
driver.find_element_by_xpath("(//a[contains(text(),'Add')])").click()
however it's not practical to do this:
driver.find_element_by_xpath("(//a[contains(text(),'Add')])[1]").click()
It would be fine, except the page has a button with text "Add User", and it clicks that instead. Is there a way to only click it if it is EXACTLY "Add" and not just contains "Add"?
You can also try :
driver.find_element_by_link_text("Add").click()
link text will match links who's text equals Add and it doesn't match text which contains Add as part of it.
This should work for you:
driver.find_element_by_xpath("//a[text()='Add']").click()
You should change your xpath to require an exact match instead of a partial match.
driver.find_element_by_xpath("//a[text()='Add']").click()
Using text()= requires the text on the button to be equal to 'Add' and will not find other buttons that happen to contain the text 'Add'

How to get entered text from a textbox in Selenium

I enter a value in TextBox or a Combobox, and would like to retrieve the value I have just entered. I see that Selenium Weblement method getText() doesn't retrieve the value, it seems the entered text doesn't get pushed into DOM.
Any solutions?
The getText() method is for retrieving a text node between element tags for example:
<p>Something</p>
getText() will return "Something"
In a textbox typed text goes into the value attribute so you can try something like:
findElement(By.id("someid")).getAttribute("value");
ComboBox is a bit different. But if you're using the Select object you can use the method:
Select selectItem = new Select(findElement(By.id("someid")));
selectItem.getFirstSelectedOption().getText();
Try getValue if it is a Text Field or Dropdown box
String lastname=selenium.getValue("//*[#id='lastName']");
System.out.println(lastname);
This is how we can fetch the text written in a textbox using Selenium + Python:
text = driver.find_element_by_xpath("Type_Xpath_Here").get_attribute('value')
print(text)