I have the following code snippet and the screenshot attached.
String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\"))" +
".scrollIntoView(new UiSelector().text(\"Test Group\"))";
driver.findElementByAndroidUIAutomator (query).click ();
What I want is to find an element with the text "Test Group" using UISelector, but inside the RecyclerView only (not searching the whole app source). What I get is the element inside search field instead (not in the RecyclerView).
Please advice. I know that I can get all searched elements using findElements(By.id("name")). But I want to use UI selector in this case.
With UiSelector you can use chaining:
String query = "new UiScrollable(resourseIdMatches(\".*recycler_view\")).scrollIntoView(resourseIdMatches(\".*recycler_view\")).childSelector(text(\"Text Group\")))";
In addition new UiSelector... part can be omitted. Appium does support this syntax.
I'm automating our application using Selenium 2.0 and Java. I would like to get a clearer understanding how can I overcome the problem with generating random ID for my WebElement and then click on it.
I have a list of elements in my drop down that all differs only in endings:
driver.findElement(By.id(""uxMiniFinderVoyageSelect_chzn_o_1")
driver.findElement(By.id(""uxMiniFinderVoyageSelect_chzn_o_2")
driver.findElement(By.id(""uxMiniFinderVoyageSelect_chzn_o_3")
driver.findElement(By.id(""uxMiniFinderVoyageSelect_chzn_o_4")
and so on till 250.
What I did is I called Random class where I declared a random variable within the range 1 to 250
Random random = new Random();
int x = random.nextInt(250) + 1;
Now I'm searching for my element this way
private WebElement cruiseSailing = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));
That's all OK and is working as expected. The problem I'm facing is sometimes error message appears after selecting some of those elements from drop down. According to my test case, I need to catch this error, capture the screenshot and choose another element from the drop down. But once I set up cruiseSailing element, it chooses the same element over and over.Please see code example below:
private WebElement cruiseSailingDropDown = driver.findElement(By.id(Some ID));
private WebElement errorMessage = driver.findElement(By.xpath("some xpath expression"));
private WebElement cruiseSailing = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));
cruiseSailingDropDown.click();
cruiseSailing.click();
Thread.sleep(2000);
if(errorMessage .isDisplayed){
System.out.printLn("Error message is displayed")
cruiseSailingDropDown.click();
cruiseSailing.click();
}else{
proceed further to the next step
Please advise how can I generate another ID for my cruiseSailing webelement.
The reason why it is choosing the same element again in case of failure is you are not reassigning the cruiseSailing value to new one .
There are 2 ways which i can think of :
Assign a new value to cruiseSailing inside the "If" block. You can do something as below inside "If" block.
cruiseSailing = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));
Call the Orignial method again which sets cruiseSailing value to new value.
Note: You might want to remove below lines from If block if you are going on with 2nd approach.
cruiseSailingDropDown.click();
cruiseSailing.click();
For Taking screenshot you can create a method and call it inside If block.
Code for taking screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\error\\screenshot.png"));
Please vote up if this helped you. Thanks :)
Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.
In WebDriver, if I use sendKeys it will append my string to the value that already exists in the field. I can't clear it by using clear() method because the second I do that, the webpage will throw an error saying that it has to be between 10 and 100. So I can't clear it or an error will be thrown before I can put in the new value using sendKeys, and if I sendKeys it just appends it to the value already there.
Is there anything in WebDriver that lets you overwrite the value in the field?
You can also clear the field before sending it keys.
element.clear()
element.sendKeys("Some text here")
I think you can try to firstly select all the text in the field and then send the new sequence:
from selenium.webdriver.common.keys import Keys
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
Okay, it is a few days ago...
In my current case, the answer from ZloiAdun does not work for me, but brings me very close to my solution...
Instead of:
element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
the following code makes me happy:
element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55");
So I hope that helps somebody!
In case it helps anyone, the C# equivalent of ZloiAdun's answer is:
element.SendKeys(Keys.Control + "a");
element.SendKeys("55");
This worked for me.
mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE);
Use this one, it is trusted solution and works well for all browsers:
protected void clearInput(WebElement webElement) {
// isIE() - just checks is it IE or not - use your own implementation
if (isIE() && "file".equals(webElement.getAttribute("type"))) {
// workaround
// if IE and input's type is file - do not try to clear it.
// If you send:
// - empty string - it will find file by empty path
// - backspace char - it will process like a non-visible char
// In both cases it will throw a bug.
//
// Just replace it with new value when it is need to.
} else {
// if you have no StringUtils in project, check value still empty yet
while (!StringUtils.isEmpty(webElement.getAttribute("value"))) {
// "\u0008" - is backspace char
webElement.sendKeys("\u0008");
}
}
}
If input has type="file" - do not clear it for IE. It will try to find file by empty path and will throw a bug.
More details you could find on my blog
Had issues using most of the mentioned methods since textfield had not accepted keyboard input, and the mouse solution seem not complete.
This worked for to simulate a click in the field, selecting the content and replacing it with new.
Actions actionList = new Actions(driver);
actionList.clickAndHold(WebElement).sendKeys(newTextFieldString).
release().build().perform();
Use the following:
driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value");
This is something easy to do and it worked for me:
//Create a Javascript executor
JavascriptExecutor jst= (JavascriptExecutor) driver;
jst.executeScript("arguments[1].value = arguments[0]; ", 55, driver.findElement(By.id("id")));
55 = value assigned
The original question says clear() cannot be used. This does not apply to that situation. I'm adding my working example here as this SO post was one of the first Google results for clearing an input before entering a value.
For input where here is no additional restriction I'm including a browser agnostic method for Selenium using NodeJS. This snippet is part of a common library I import with var test = require( 'common' ); in my test scripts. It is for a standard node module.exports definition.
when_id_exists_type : function( id, value ) {
driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 )
.then( function() {
var el = driver.findElement( webdriver.By.id( id ) );
el.click();
el.clear();
el.sendKeys( value );
});
},
Find the element, click it, clear it, then send the keys.
This page has a complete code sample and article that may help.
This solved my problem when I had to deal with HTML page with embedded JavaScript
WebElement empSalary = driver.findElement(By.xpath(PayComponentAmount));
Actions mouse2 = new Actions(driver);
mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].onchange()", empSalary);
WebElement p= driver.findElement(By.id("your id name"));
p.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");
I've been racking my head with this...
I've got a localized strings.xml file in a values-en folder with this example string:
#string/my_string
The string has the following text stored in English: "My String"
When accessing the localized string via a layout, it works fine.
When I try to change it in code, that's where I get problems.
I store the string into an array of strings for later use. The 'context' is passed from my activity to a data class and used with this line of code:
dataStrings = new String[] { (String) context.getResources().getString(R.string.my_string) };
Later, I try to display this string, like so:
buttons[0].setText(dataStrings[0]);
It displays:
#string/my_string
How do I get it to display the string without '#string/', the proper localized string?
You can run getString() directly on the Context object; you don't need to run getResources(). However, this should do the same thing as you're currently doing so I don't think that's the source of your problem.
The first thing to confirm is that what you think is happening is happening. Either use the debugger to check that buttons[0] contains "#string/my_string" or try calling setText() with a hard-coded value to make sure the text is actually being updated on the correct button - e.g. buttons[0].setText("StackOverflow!");