I have a requirement to pass the text "English (United States)" into a text box. But when I am using the below command:
driver.findElement(By.id("defaultLang")).sendKeys("English (United States)");
It is entering only "English United States) into the text box. the Open Parentheses is missing when WebDriver is writing the text into the textbox.
By using this you can enter the left parenthesis in text box.
String a = (Keys.chord(Keys.SHIFT,"9"));
String b = "AAA";
String c = "BBB)";
driver.findElement(By.id("ID")).sendKeys(b+a+c);
Resultant O/P is : AAA(BBB)
You should use escape to send special characteres in selenium web driver,
I think you can try something like this :
driver.findElement(By.id("defaultLang")).sendKeys("English \(United States\)");
I hope it may help you
This looks like a known bug. See issue 6822 here.
What you can do now, is to set it by injecting JavaScript directly.
// untested Java code, provides only the logic, you need debug yourself
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].setAttribute('value', arguments[1]);", driver.findElement(By.id("defaultLang")), "English (United States)");
Related
I want to automate a test using selenium java in which I need to check whether a specific text is NOT present on the entire page.
The page has many elements where this text may be present. In general, if it were a few elements, I could resolve this via standard driver.findElement(By.xpath("//somepath")).getText(). But, I want to write an efficient test that doesn't have tons of locators just for this test.
You can use XPATH selector '//*[contains(text(), "YOUR_TEXT")] to find a text that you need.
Example of the code on Python:
def find_text_on_the_page(text):
selector = '//*[contains(text(), "{}")]'.format(text)
elements = browser.find_elements_by_xpath(selector)
assert len(elements), 'The text is not on the page'
Hope, this will help you.
You could try to locate it via xPath selector, but I would not recommend test like that.
Surely You know where to look for some test? At least a part of web page.
Here is code sample how to achieve this:
List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
Assert.assertTrue("Text not found!", list.size() > 0);
or
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue(bodyText.contains(text));
or in method:
public boolean isTextOnPagePresent(String text) {
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();
return bodyText.contains(text);
}
Hope this helps, but as I mentioned try defining a smaller scope for testing.
How can I change the text within my textview based on the language set on the device without using string resources? I don't want to use string resources. As an example could someone please tell me what code I can use if the device language is in French and whether or not I'd need to use else if or else for including other languages.
TextView txt = (TextView)v.findViewById(R.id.WCBank_textView1);
txt.setText(Html.fromHtml("<font color='#E32017'>Red apples,</font>" +
"<font color='#FFD300'> yellow bananas,</font>" +
"<font color='#00782A'> green grapes</font>"
));
I still prefer string resources for i18n (not for the individual words, but for the whole expression). That said, you could try something like this:
if (Locale.getDefault().equals(Locale.FRENCH) || Locale.getDefault().equals(Locale.FRANCE)) {
// do the french stuff...
txt.setText(...);
} else if ...
https://code.google.com/p/selenium/issues/detail?id=6074
I am using Selenium and jQuery UI Autocomplete. I am trying to send a word with the ampersand in it but the ampersand is omitted. If I type in the ampersand manually on the interface, it works fine. The problem occurs specifically with Firefox but not with Chrome.
I noticed this was raised as a bug in Selenium 2 years ago (2013) but there doesn't seem to be any update on it.
A workaround to this problem which was suggested in the above link was something like this:
textBox.sendKeys("R/&d")
textBox.sendKeys(Keys.LEFT);
textBox.sendKeys(Keys.LEFT);
textBox.sendKeys(Keys.BACK_SPACE);
This is not an option as the String could be dynamic and this would require knowing the exact position of the ampersand. It's a really unnecessary hack! It's not like we are dealing with Internet Explorer here.
Is there really no solution for this after 2 years?
You could try using javascript to enter the text, something like(in C#):
public void SendKeysUsingJS(IWebElement element, string text)
{
IJavaScriptExecutor exec = (IJavaScriptExecutor)driver;
exec.ExecuteScript("arguments[0].value = '" + text + "';", element);
}
Obviously driver is your webdriver instance.
Recenty I migrated from Selenium RC to Webdriver. Typing text containing delimiter TAB used to work fine on Selenium RC. But when using webdriver, typing tab moves focus to next input.
Sample text :
Name Age
Mark 35
I did the following :
if(text.contains("\t"))
{
data = text.split("\t");
for (String str : data)
{
element.sendKeys(str);
element.sendKeys(Keys.TAB);
}
}
else
{
element.sendKeys(text);
}
I tried using elements.sendKeys("\t") as well as elements.sendKeys("\\t").
Any suggestions on how to achieve this?
Thanks in advance.
You can use the Actions class for advanced operations. Refer this site http://www.guru99.com/keyboard-mouse-events-files-webdriver.html
As already said in the comments, the use of sendKeys will work as if you were a normal user of the browser. That means the browser will focus on the next input. If you really need to add a TAB in the input, I think the solution is to execute JavaScript.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('" + inputId + "').value = '\\t';");
I am trying to set the caption of a checkbox and i want to break at a certain point in the string to create a new line without waiting for the word wrap:
When i added the simple \n character to caption string it did not work.
private CheckBox completeCheckbox;
completeCheckbox.setCaption("why wont this\n break");
I just had the same problem and the simple solution for this was to call "setCaptionAsHtml(true)" on the CheckBox instance and to set the caption using HTML code.
private CheckBox completeCheckbox = createMyCheckboxSomehowFunction();
completeCheckbox.setCaptionAsHtml(true);
completeCheckbox.setCaption("why wont this<br/>break");
The above code snippet should do what you want. At least it worked fine for me.
i think the solution is to use an Option Group
I just read that here