sendKeys to contentEditable div not working in Internet Explorer - java

I'm having an issue with InternetExplorerDriver in Selenium WebDriver. The image below shows part of a Sharepoint site, with Audience Area and Location as fields which can either accept a manually typed input or a choice from a popup list on clicking the button at the far right of the field. In the example below, I have manually input an invalid entry in Audience Area and a valid entry in Location.
contentEditable divs in IE
However, when running scripts in Selenium, the sendKeys command does not work on these elements. According to the xpath, these elements are identified as divs with the following properties:
<div id="AudienceArea_$containereditableRegion"
class="ms-rtestate-write ms-taxonomy-writeableregion ms-inputBox ms-inputBoxActive"
contenteditable="true"
role="textbox"
aria-autocomplete="both"
aria-haspopup="true"
aria-multiline="true"
disableribboncommands="True"
allowmultilines="false"
restrictpastetotext="True"
rtedirty="false">
The most recent attempt to code sending text to the element is as below:
Webelement element = driver.findElement(By.xpath("//html/body/form/div[4]/div/div[1]/div/div[2]/div[2]/div[2]/table/tbody/tr/td/div/div/div/div/table/tbody/tr/td[1]/span/table[1]/tbody/tr[5]/td[2]/div/div[1]/div/div"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys("SomeText");
actions.build().perform();
When I run this in Firefox it identifies the element and sends the required text. When I attempt this in Internet Explorer, it successfully identifies the element and performs sendKeys, but nothing appears in the text field. No exception is returned.
Has anyone encountered anything like this before? Is there a way around this, such as an alternative to sendKeys?
I am running InternetExplorerDriver v2.53.1.0 with IE v11

You should try to setValue after click,
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].textContent= arguments[1];", element,"SomeText");

Related

How to select a value from input type dropdown using selenium webdriver?

I am trying to select a value available in a read only drop down and I have tried so many options but still failing to select the desired option. The drop down has two values available ValueOne and ValueTwo. By default the ValueOne is selected and in my case I need to select ValueTwo. I used firebug to get the below code when I click on the drop down and do Inspect Element with firebug
The Code is :
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="ValueOne" name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl02$EditFormControl$rcbControllerType1" autocomplete="off">
</td>
So far I have tried
1----------
Select DropDown = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")));
DropDown.selectByVisibleText("ValueTwo");
and I get an exception as
:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
2------------
WebElement Dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input"));
Select clickThis = new Select (Dropdown);
clickThis.selectByVisibleText("ValueTwo");
Get Exception:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
I also tried selectByIndex but still get the above exception message.
3--------------
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo");
Nothing happens and the case is marked as Pass. No error no exception.
Also I am running my webscript on firefox 38.0.5 with selenium 2.46.0 with eclipse TestNG.
I have confirmed the frame is not an iframe.
Please suggest the solution.
The root problem might be, that this is not a standard select box but an javascript based solution. The code above just shows the 'host' html element. I am pretty sure that there will be a) a previous hidden element that becomes visible or b) a newly created element in your DOM that holds the values. You have to find those (dev tools or firebug) to interact with.
Some pseudo code that might appear (just to get a hint):
<ul>
<li id="element1">ValueOne</li>
<li id="element2">ValueTwo</li>
</ul>
And after it appears (wait for in selenium) you just have to click the desired element.
Find your xpath through firepath addon in firefox.
driver.findElement(By.xpath(".//*#id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']")).click();
Select value in dropdown ->goto firpath by right click and copy xpath
driver.findElement(By.xpath(".//*#id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']/span[3]")).click();
hope you will find your solution :-)
You can use this :-
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)
you can use the following code. Here what I have done is find the dropdown, click on it and find the option to select and send down key until we see the element to select and after click on it.
public class InputDropdownselect {
#Test
public void login() throws Exception
{
System.setProperty("webdriver.chrome.driver", "G:\\drivers\\chrome\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://yourwebsite.com");
driver.manage().window().maximize();
driver.findElement(By.id("txtuser")).sendKeys("123456");
driver.findElement(By.id("txtpassword")).sendKeys("Abc!#1");
driver.findElement(By.id("log-btn")).click();
Thread.sleep(2000);
driver.findElement(By.id("enrollment")).click();
//driver.findElement(By.xpath("//*[#id=\"enrollment\"]")).click();
driver.findElement(By.xpath("//*[#id=\"studentBasicForm\"]/div[2]/div[9]/div/div/input")).click();
Actions action= new Actions(driver);
WebElement joiningYear=driver.findElement(By.xpath("//input[#placeholder=\"Joining Year Group\"]/following::ul[1]/descendant::li/following::span[contains(text(),\"8\")]"));
do {
action.sendKeys(Keys.ARROW_DOWN).perform();
} while (!joiningYear.isDisplayed());
joiningYear.click();
}
}

Selenium Unable to Find Element

I am trying to find an element on a website using Selenium. The page I am looking at is:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
Using an explicit wait worked for me (tried both chrome and firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into:

Selenium Webdriver: spawning Javascript Calendar window

In testing a simple website, I find that when using the Firefox webdriver I am unable to get a Javascript calendar window to appear when the button is clicked. I am able to spawn the window in the Selenium IDE but when I run the Java code, the click is registered, but no window is spawned.
The Code I am using to click the Javascript element is:
WebElement element = driver.findElement(By.cssSelector("img[alt=\"Pick a date\"]"));
element.click();
Additional info: The 'cal.gif' image is also not shown when using the webdriver. The problem is NOT switching to the calendar window or selecting and element within, it is simply getting the window to spawn at all.
This is the website under test: Parking Meter
I have searched quite a bit for the solution, either I am not searcher the right keywords or I am missing something obvious, any help would be appreciated.
edit: HTML code for the JS calendar:
<a href="javascript:NewCal('EntryDate','mmddyyyy',false,24)"
<img height="16" width="16" border="0" alt="Pick a date" src="cal.gif"></img>
</a>
What your doing here is wrong. You need to click on the a tag not on the img tag.
Look at the below code, which is working fine for me:
#Test
public void testSO() throws Exception
{
driver.get("http://adam.goucher.ca/parkcalc/index.php");
Thread.sleep(2000);
driver.findElements(By.tagName("a")).get(0).click();
}
Change the index to 0 or 1 accordingly.
Id be using the JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("<JavaScript.click;");

How to Manipulate Search Bar That's Not Present in HTML Source Using Selenium?

I'm trying to use Selenium (Java) to automate some searches.
When I go to the page and click Inspect Element on the search bar, I see that it has the id searchBox and name q, both of which are potentially useful. Nevertheless, these properties are nowhere to be found in the HTML when I go to View Source.
When I try to use
WebElement search = driver.findElement(By.id("searchBox"));
or
WebElement search = driver.findElement(By.name("q"));
both come back as unable to be found.
How do I proceed with populating the search field then hitting submit (the submit button also is missing in the source page) if I can't find either of these elements?
EDIT:
As requested, here's a more detailed picture of what's wrong:
The URL of the page accessed by WebDriver is http://www.ycharts.com using the line
driver.get("http://www.ycharts.com/login");
If you go to this page with your actual web browser, right click on the search bar and choose Inspect Element, you'll see that its ID is "searchBox" and its name is "q". However, if you go to view the same page's source, you'll see that there's no such element in the HTML. Clearly, this is why WebDriver can't find it.
driver was initiated as follows:
WebDriver driver = new HtmlUnitDriver();
When I try something like
WebElement search = driver.findElement(By.id("searchBox"));`
The exception returned is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: searchBox
So, back to the original question: clearly, the element is there, but it's not in the HTML – how do you interact with it?
The problem is being caused by the fact that the search box is added to the html post page load by javascript.
The HTML returned by http://ycharts.com/ does not contain the searchbox, therefore when Selenium thinks the page has finished loading (i.e DOM ready state), there is no search box.
In order to interact with the search box, you need to tell Selenium to wait until the box appears in the DOM.
The easiest way to achieve this is with WebDriverWait;
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

Cannot find element in the popup window with Selenium Webdriver

I'm using InternetExplorerDriver with Selenium Webdriver in Java.
I load a URL which simultaneously opens another window which has the login box.
Using the method mentioned here
I could select the required window and also send keys into the focussed box.
popup.getKeyboard().sendKeys("yeuiryuiryweuiryeuiyterui");
However, the following:
popup.findElement(By.id("userName")).sendKeys("user")
is unable to find the element and throws an Element-not-found exception (something similar to this).
Where am I getting it wrong?
I will suggest you to try with xpath
String locator = "//*[#class='Class name of user name text box'and text()='if any']"
you can find the user name through firebug
List<WebElement> element = Driver.driver.findElements(By.xpath(locator));
thn use click functions on web element , do the same on password and then click on login directly using Driver.selenium.click(xpath); and provide the xpath of login button in case id's are not present.

Categories