Accessing drop down list- selenium webdriver & java - 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.

Related

Actions Class in Selenium Webdriver

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

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

java selenium - hidden input value

First Stack post, so don't be harsh if I get it wrong.
Im using selenium and java for some automated testing. All was going well until I tried to set the value of a hidden input.
When using 'type' in the Firefox IDE, it works a treat, but when I use the line below, it doesn't play at all. It just fails.
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Can anyone point me in the right direction.
Many Thanks.
Edit:
WebDriver driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("$([name=startDate_submit]).attr('type', 'text');");
Thread.sleep(3000);
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Should this be the way I do this? I just cannot get it working.
just try with this command,
driver.findElement(By.xpath("path")).sendKeys("value");
but make sure you have clicked that path before providing input value. come back if you still have any problem.
Try with available javascript commads like document.getElementById, then set the value.

WebDriver : SendKeys(Integer) NOT working in Firefox 29

I am using Firefox 29 and WebDriver java 2.41.0 bindings to automate test scenarios. Have one scenario to input an integer to a input-box which was working FINE with Firefox 28 and now failing with v29 i.e latest FF version. The code I wrote for the same is:
int inputString = 123456;
driver.FindElement(By.Id("tinymce")).SendKeys(inputString);
Please help me getting through of this.
This will be the result of this issue:
https://code.google.com/p/selenium/issues/detail?id=7291
Fixed by this revision in the Selenium code:
https://code.google.com/p/selenium/source/detail?r=afde40cbbf5c
Quick test below worked for me. I understand JS is not the right way to do browser simulation, one should always FIRST use webdriver methods since they use browsers native api, but thought it would unblock you while the bug is fixed in selenium
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get("http://yizeng.me/2014/01/31/test-wysiwyg-editors-using-selenium-webdriver/");
WebElement frame = driver.findElement(By.id("tinymce-editor_ifr"));
driver.switchTo().frame(frame);
WebElement body = driver.findElement(By.id("tinymce"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].innerHTML = '<h1>Heading</h1>Hello There'",body);
Can you check following points
Can you make sure there are no application changes, I mean the locator is returning single node element.
Can you also post the exception that you are getting. To troubleshoot this problem, need these details.
One thing I do before sending a value to a box is to clear it, also, the value I send is always a string, the parsing should be done by the page/code as you need to validate what the user has entered.
But I do agree with skv, we need to see the actual error being thrown.

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