Actions Class in Selenium Webdriver - java

I am trying to perform the Control+A operation using the Actions class in Selenium using the following query:-
driver.get("https://jqueryui.com/datepicker/");
new Actions(driver).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
.build().perform();
However , instead doing Control+A for the contents on the Webpage , it is performing the same operation in the URL bar. Could someone please let me know what is the error here.Moreover the issue what i see is the control stays in the URL bar and it doesnt come down to the Webpage.

I think there is an issue with pressing keys in selenium 3.0 which is reported here Actions sendKeys UnsupportedCommandException with geckodriver
You can try following alternative way to do that -
driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a"))

Related

Selenium Webdriver Java: Unable to perform click operation by Actions in Chrome

I am using Selenium Webdriver(Java) for my Automation. For one of my use-case, I need to click based on co-ordinates. I am using following code to perform this operation:
Actions act = new Actions(driver);
act.moveByOffset(236, 92).click().perform();
Above code is working perfectly in Firefox(Gecko driver). But with Chrome driver, it is not working. Any idea?
Is there any other way to perform this.
I think you can try what Santosh has suggested
act.moveByOffset(236, 92).click().build().perform();
However, this should not make much of a difference as the perform() already contains the build action but this might be a workaround for your problem.
If you can locate the webelement, you can use JavaScript to perform the click this way:
JavaScriptExecutor js = (driver)JavaScriptExecutor;
js.executeScript("arguments[0].click();", element);

Selenium element can not be found within iframe

I am trying to retrieve a JSON element, the problem is that in the source code it doesn't exist, but I can find it via inspect element.
I have tried with
C.driver.findElement(By.id("ticket-parsed"))
and via XPath
C.driver.findElement(By.xpath("//*[#id=\"ticket_parsed\"]"));
and I can't find it.
Also
C.driver.switchTo().frame("html5-frame");
System.out.println(C.driver.findElement(By.id("ticket_parsed")));
C.driver.switchTo().defaultContent();
i get
[[ChromeDriver: chrome on XP (1f75e50635f9dd5b9535a149a027a447)] -> id: ticket_parsed]
on
driver.switchTo().frame(0) or driver.switchTo().frame(1)
i get that the frame doesn't exists
and at last i tried
WebElement frame = C.driver.findElement(By.id("html5-frame"));
C.driver.switchTo().frame(frame.getAttribute("ticket_parsed"));
an i got a null pointer exception
Here's an image of the source:
what am I doing wrong?
Well!
The element #ticket-parsed is in iFrame. So, you can click it without getting into an iframe.
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
or
driver.switchTo().frame(frame_index);
In your case,
driver.switchTo().frame("html5-frame");
After switching into the iframe, you can click that element using either XPath or CSS.
C.driver.findElement(By.id("ticket-parsed"))
NOTE:
After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();
I didn't found a solution with my excising setup,but i did found a js command which gets the object correctly
document.getElementById("html5-frame").contentDocument.getElementById("ticket_parsed")
you can integrate js commands like this
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(*yourCommandHere*);
if you want to get the output of the command just add the word return before your command (in this specific situation it didn't work but in any other situation it did)
*TypeOfData* foo = js.executeScript(return *yourCommandHere*);
at last because of limited time i had to use unorthodox methods like taking screenshots and comparing the images if they are exactly the same
Thanks for the help

How to switch to Telerik Radwindow using webdriver (java)

We are testing an application build using Telerik.
A demo of Telerik is available here: http://demos.telerik.com/aspnet-ajax/window/examples/radwindowobject/defaultcs.aspx
Our application is build in a similar way.
In this demo you can see a window with Bing in it. I want to switch to it using WebDriver (Java) to perform actions on objects within it.
I have tried to switchto iframe but WebDriver comes back saying it is not an iframe.
Also tried to get window handles and switchto window but with no luck, it is not treated as a new window. Any suggestions please?
The following code worked for me. I was able to enter text in the bing search field.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demos.telerik.com/aspnet-ajax/window/examples/radwindowobject/defaultcs.aspx");
driver.switchTo().frame("RadWindow1");
driver.findElement(By.name("q")).sendKeys("Hello this is my text");
Let me know if this helps you.

Accessing drop down list- selenium webdriver & java

I am trying to write a few test cases for automation testing as a part of practice assignment in selenium webdriver using java.
The java version is 1.6 and selenium webdriver version is 2.39 while firefox browser version is 29.0.1.
I am trying to access the drop down titled CARSIZE in the following link:
http://www.carrentals.com/
I am not able to manipulate it.
I have tried the following code...
driver.get("http:\\www.carrentals.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Select dropdown= new Select(driver.findElement(By.xpath("//*[#name='carType']")));
dropdown.selectByValue("carsize-1");
With the above code, it seems that I am able to find the element(as no exception is thrown) but not change a value. When I try to change a value by SELECTBYVALUE method, I get an exception saying element not visible. Can someone help me?
The Html code for the above can be seen in firebug and just for information I have also tried using ID and name instead of XPath for the concerned select box but I get the same exception.
Try this:
Select dropdown= new Select(driver.findElement(By.xpath("//select[#id='cartype']")));
dropdown.selectByVisibleText("Small Cars");
Updated.
Try this code. It works for me.
String dropdownXpath = "//label[#for='cartype']/following-sibling::div[#role='listbox']";
WebElement textInDropDown = webDriver.findElement(By.xpath(dropdownXpath + "//div[#class='text']"));
textInDropDown.click();
webDriver.findElement(By.xpath(dropdownXpath)).sendKeys("Small Cars");
It locates text in dropdown element and then sends value which you want to select.

Webdriver test doesn't select option from dropdown

i have a registration form for checkout,I have to select a state from drop down,for which i have written a script:
WebElement wb = driver.findElement(By.name("user_data[s_state]")) ;
Select selwb = new Select(wb) ;
selwb.selectByValue("KR");
driver.findElement(By.name("dispatch[checkout.update_steps]")).click() ;
but after executing this script,it is not selecting given value from dropdown.hence i am unable to proceed on next step. Plz help me out....
I can give you a suggestion since I have worked with Selenium webdriver for sometime. The webdriver executes very fastly. Selenium driver doesnt take care whether the page loaded or not. So I recommend you to add code just before the click/selection events to make sure that the page loaded completely. There are options like waitForPageLoad() or checkifComponent exist to make sure that page is loaded properly before the events are triggered. Hope this will help you.
If any wait doesn't help - like suggested before - try to select eg. by index or text.
I've had such wired cases where one of ByValue/ByText/ByIndex method didn't work although others did with particular dropdown.
If you are about to select the drop down use the following:
First make the webdriver wait
Then try choosing the element with the help of
driver.findElement(By.xpath("xpath of the value").select
OR
you can use like the following:
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("the xpath of the value"))).click();

Categories