Selenium Webdriver: spawning Javascript Calendar window - java

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;");

Related

Call function result missing 'value' while automating a certain page using Selenium (Java)

I am trying to automate a certain page. I want to make the mouse click anywhere in the page, but I am getting the following error:
call function result missing 'value'
The elements in the page is:
<div id="b1" iframegroupid="P1" class="btn_act"><img src="theme/images/hi.png" draggable="false"><span>K</span></div>
Another element is:
<div draggable="false" role="presentation" tabindex="0" class="xc" widgetid="SCREEN_field_Submit" id="CLOSE_1" title="">
The code that I have is:
WebElement close_frame = Driver.driver.findElement(By.id("P1"));
//driver.get(Constant.geturl());
driver.switchTo().frame(close_frame);
WebElement btn1 = Driver.driver.findElement(By.id("CLOSE_1"));
btn1 .click();
I dont understand what is going wrong.
i think you should have to update your chromedriver with latest verion.
update to update chromedriver to 2.36 or greter, older versions have such issues.
i am also confused about your Driver.driver. and directly driver. methods.
try using
driver.switchTo().frame(driver.findElement(By.id("P1")));
wait.until(ExpectedConditions.elementToBeClickable(By.id<CLOSE_1>));
driver.findElement(By.id("CLOSE_1")).click();

Locate an element created by javascript using selenium webdriver

I'm new in Selenium and Java, i'm work in a Automation testing project using selenium (java) , today i have a problem with driver.findElement, the element i want to find was created by javascript, i'm sure about using WebDriverWait to wait to target element to present, even i use thread.sleep and wait for it for about few minutes but webdriver still can not locate that element.
This is my problem, in html/js code i have a button, click this button javascript will create a div and i need use selenium to locate it to make Automatio Testing work.
After click a button, javascript makes a div look like :
<div id="zm-view-frame" class="fade in"><div class="zm-view-header"><div class="zm-view-back"><a onclick="WFLandingpage.closePreview();" id="zm-view-close" class="button" href="javascript:void(0);"><span>Close</span></a></div><div id="zm-view-button"><span>Desktop</span><span>Tablet</span><span>Mobile</span><span>Rotate</span></div></div><iframe id="zm-display-iframe" src="page=mvc_landingpages&act=templatepage&preview=1&templateId=landingpage3" style="padding-top: 63px; margin: 0px auto;" scrolling="yes" width="100%" height="609"></iframe></div>
then, in java code i wrote :
this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("zm-view-frame")));
Then :
Tester.Browser.findElement(By.id("zm-view-frame")).click();
of course, i have defined this.wait :
public WebDriverWait wait = new WebDriverWait(Tester.Browser, 100);
and i get a wait timeout exception
I even use many type of By such as Xpath, css selector, class ... but still no luck.
I also use Selenium IDE for firefox to get element, export it to java code and view in my editor then do the same but it's not work for me again.
i'm really stuck on this, can you give me some help ?
and sorry for my bad english, many thanks !
UPDATE - this is my html structure :
html structure
UPDATE - I found the bug and have a solution
Reason : Before above code i have few line of codes to check if a iframe available, the code like :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).
I found the bug and have a solution
Reason : Before above code i have few lines of code to check if a iframe available, the code like below :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).

sendKeys to contentEditable div not working in Internet Explorer

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");

Selenium - webdriver can find element but click action is not performed

I am trying to automate functional testing of a web application using Selenium webdriver and Java.
In the AUT, there is a 'Submit' button defined by the following html code
<button id="submitbtn" class="btn btn-primary" type="submit">Submit</button>
I use the following command to click the button.
driver.findElement(By.id("submitbtn")).click();
When I run the code, the webdriver can find the button but the click action is not performed (I can understand that webdriver can find the button because no exception is thrown and the I can see a selection on the button when the code is run). I tried different waits
new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn"));
but not getting any positive result. If I use,
Thread.sleep(3000);
it works fine (but I want to avoid this code). I tried all other types of waits and action class,
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.id("submitbtn"));
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn")));
action.click().perform();
but no luck. Is there any way to achieve this?
How about JavascriptExecutor?
WebElement element = driver.findElement(By.id("submitbtn"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
A submit() is an option driver.findElement(By.id("submitbtn")).submit();. More information here

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();
}
}

Categories