可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have my login screen in app now. Each time the app is launched in screen the mobile number is pre filled with the older text.
I just want to know I have tried:
WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.clear // Not working
I have tried :
String Mobile
mob="";
but still it cannot delete the pre filled text.
I am trying to automate my android app using appium, please help me with this.
回答1:
It is definitely not efficient, could be improved and there's probably a better way...
But, using adb's shell input key event codes I simply called "dpad right" to move the cursor all the way to the right. Once there, send the key code "DEL" to start deleting all the way back...
So... two for-loops. This was mainly used for short texts:
public void cleatTextFully(WebElement element) {
int stringLength = element.getText().length();
for (int i = 0; i < stringLength; i++) {
mDriver.sendKeyEvent(22); // "KEYCODE_DPAD_RIGHT"
}
for (int i = 0; i < stringLength; i++) {
mDriver.sendKeyEvent(67); // "KEYCODE_DEL"
}
}
mDriver is the AppiumDriver instance.
Hope this helps some what.
回答2:
I had trouble with this too. A main issue I found was that in order to delete the area by pressing the delete key was that it needed to tap at the end of the line. This works for me:
public void clearTextField(WebElement element) {
double x = element.getLocation().getX() + element.getSize().width - 5;
double y = element.getLocation().getY() + ((double) element.getSize().height / 3);
preciseTap(x, y, 0.1, 1);
while (!element.getText().isEmpty()) {
pressDeleteKey();
}
}
public void preciseTap(double x, double y, double duration, int touchCount) {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> tapObject = new HashMap<String, Double>();
tapObject.put("x", x);
tapObject.put("y", y);
tapObject.put("touchCount", (double)touchCount);
tapObject.put("duration", duration);
js.executeScript("mobile: tap", tapObject);
}
public void pressDeleteKey() {
HashMap swipeObject = new HashMap();
swipeObject.put("keycode", 67);
((JavascriptExecutor) driver).executeScript("mobile: keyevent", swipeObject);
}
It is a lot slower than just clearing it all out but I have not figured out how to do this yet. Would be ideal to double tap or tap and hold until everything is selected.
回答3:
A click on to the textBox before you clear should do with the latest libs:
WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.click();
mob.clear();
OR from the sample jUnitTest here
WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
text.sendKeys("12");
text.clear();
I guess
回答4:
We can use selectAll if tests are executing on android.
- longPress on textfield
- Locate and tap on element of selectAll (Android select all button)
- sendKeyEvent(67)
It will select entire text and will press delete.
回答5:
I was able to resolve this issue using the code below as "textField().clear()" didn't appear to work on some devices and emulators such as Genymotion.
while (!textField().getText().isEmpty()) {
TouchAction touchAction = new TouchAction(driver);
touchAction.longPress(textField());
driver.getKeyboard().sendKeys(Keys.DELETE);
}
The code would loop through until textfield is cleared. Hope this helps, works for both Android and iOS.
回答6:
you might want to refrain from using appium's built in clear method.
you can try using adb shell input keyevent 67 which sends a delete keyevent to android.
this works on all versions pretty much.
you can just click the textfield twice to highlight the text:
WebElement mob = driver.findElement(By.tagName("editText"));
that would locate the first textField avilable, as other methods in selenium bindings,
you can try findElements as well, and get all the textfields in an array, and you can choose whichever you meant to manipulate.
then, send an input keyevent 67 to delete the whole field.
回答7:
If the text field contains any pre-specified mobile number, please do in the following way.
WebElement mob = driver.findElement(By.name("xxxxxxxxxx"));
mob.clear();
xxxxxxxxxx: mobile number that is pre-specified while opening the application.
Else use some other locating techniques like By.xpath , By.id(if you are testing android and Selendroid as capability) etc.
回答8:
This method worked for me, although I am trying for a better solution. In the meantime please use this method.
public void clear(String locatorType, String locator, long... waitSeconds)
{
WebElement we = getElementWhenPresent(getByLocator(locatorType, locator), waitSeconds);
String text = we.getText();
int maxChars = text.length();
//we.clear() is not working
for (int i = 0; i < maxChars; i++)
((AppiumDriver)driver).sendKeyEvent(67);
}
回答9:
To avoid recent iOS 8.x errors I use the following (where by is an instance of By representing your textfield/textview):
int stringLength = driver.findElement(by).getText().length();
if (stringLength > 0) {
driver.findElement(by).clear();
}
If you try and call clear() against an already clear textfield I was tending to get errors in appium.
回答10:
I faced similar issue recently. What I did, get the text from edit text if equals ""
, then send keys else called clear()
method.
回答11:
I have faced the same issue, but i found one proper solution.
public void clearTextBox(WebElement element) throws Exception
{
element.click();
element.sendKeys(Keys.CONTROL + "a");
element.sendKeys(Keys.DELETE);
}
It will work for sure.
回答12:
This normally happens when the opened screen in your app, is a "WebView".
When faced with similar problem, I followed the below steps:
- Got the value of the textfield, i.e., prefilled text, using the "getAttribute" method. And, then retrieved the length of the string, so as to know how many characters are present to delete
- Tapped on the textfield.
- Deleted the characters inside the textfield using the "Delete button" element of the iOS Keyboard.
- Filled the necessary value in the textfield.
Below, is the code that might just help you out in resolving your problem:
WebElement ele = driver.findElement(By.xpath("//Locator of the textfield")); //Locating the textfield
int characters_to_delete = ele.getAttribute("value").length();//Counts the number of characters to delete
ele.click(); //Tapping or Clicking on the textfield
// Deleting or Clearing the textfield off of the prefilled values
for(int i=0;i<characters_to_delete;i++){
driver.findElement(By.xpath("//Locator of the Keyboard button 'Delete' ")).click();
}
ele.sendKeys("value to be filled in the textfield"); //Filling the textfield with the value
回答13:
driver.findElement(By.name("Mobile Number")).clear();
this works for me. I am using find element by id and then calling clear.
wd.findElement(By.id("username")).clear();
This clears the pre-entered data in the field user name in my case.
回答14:
I faced the same issue.
Tried to click, clear and send keys, it worked.
See this code
driver.findElement(By.name("address").click();
driver.findElement(By.name("address").clear();
driver.findElement(By.name("address").sendKeys("something");`
回答15:
It's about login screen so i would suggest to restart the app each time you run your script, go to appium and check Full Reset in Android Settings.
回答16:
I'm working with python and this is what I use:
self.driver.find_element_by_id(element_id).click()
self.driver.find_element_by_id(element_id).send_keys('')
I hope that it helps.
回答17:
For iOS, I can perform backspace/delete by using below code of snippet:
//Get the keyword
String name = driver.findElement(by).getText();
WebElement e1 = driver.findElement(by);
System.out.println("Length of Keyword = " +name.length());
int count = 0;
int keywordlength = name.length();
//click on keyword textbox
e1.click();
while (count < keywordlength) {
//Clear the keyword
TouchAction ta = new TouchAction(driver);
ta.longPress(e1);
driver.getKeyboard().sendKeys(Keys.DELETE);
count++;
}
回答18:
This thing can be overcome by using the latest version of appium. In older version clear sometimes doesn't clear all the text-fields in case of longer text.
回答19:
WebElement element2 = appiumDriver.findElement(By.xpath(element));
element2.sendKeys(Keys.CONTROL + "a");
element2.sendKeys(Keys.DELETE);
回答20:
List<WebElement> x=driver.findElements(By.className("enter widget details")); //You can get widget details via android UIautomator
driver.findElementById("enter widget details").click(); //You can get widget details via android UIautomator
x.get(index).clear();
x.get(index).clear();
Please note this worked for me but you may have to maneuver your code as per your requirements, thanks.
回答21:
def Clear(self):
self.driver.keyevent(123)#Move Cursor Last one
time.sleep(2)
for i in range(10):
self.driver.keyevent(67)#Deleted Keyboard
self.driver.implicitly_wait(60)
回答22:
What helped for me was to add an empty sendKeys before the clear.
Like this:
public void sendKeysToElement(WebElement element, String keys) {
element.sendKeys("");
element.clear();
element.sendKeys(keys);
driver.hideKeyboard();
}
This method is used for all our input fields. Without the empty sendKeys it was not clearing all the fields properly.
回答23:
I use cucumber
and ruby
to write my tests and this one works good for both iOS and Android for me.
arg1
and arg2
are the input values that I give in the gherkin
steps.
And(/^I enter "([^"]*)" on identifier "([^"]*)"$/) do |arg1, arg2|
Input = find_element(id: arg2)
Input.click
Input.clear
Input.send_keys(arg1)
end
回答24:
Is working for me:
action.longPress(WebElement).perform();
WebElement.clear();