Webdriver test doesn't select option from dropdown - java

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

Related

How to select a option from drop down with div tag/class?.selenium

I have tried all the possible things. also, tried searching over tried different combinations and variations. I can get the element clicked which opens the dropdown. but i am not able to select an option in it. I tried with actions, sendkeys, keys.down/enter and multiple things as well. however, it didn't help. That's the only thing I am stuck on.
//selecting office
1.driver.findElement(By.id("DG5QEPn")).click();
Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.xpath("//*[#id="DG5QEPn"]/div/div/div1/div1"))).click();
driver.findElement(By.id("DG5QEPn")).click();
driver.findElement(By.id("DG5QEPn")).sendKeys("RTP HQ"); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Down); driver.findElement(By.id("DG5QEPn")).sendKeys(Keys.Enter);
Html-
In the below image.
You should enumerate through all of the options then click on the one you want. Take a look at this.
EDIT
The easiest way that I have found was to do something along the lines of:
el = driver.find_element_by_id('id_of_select')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'The Options I Am Looking For':
option.click() # select() in earlier versions of webdriver
break
Below code worked for me
WebElement selectMyElement = driver.findElement(this.getObject(By.Id("Id of Your DropDown")));
selectMyElement.click();
Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN, Keys.ENTER)).perform();

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

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.

Set focus on WebElement?

i'm currently testing the GUI of my application, and i wanted to know if it's possible to set the focus to a WebElement ?
I'm using Selenium 2.0 and the webdriver.
So i'm looking for something like that : driver.findElement(xxxx).setfocus();
Thank you.
EDIT :
I've alreardy tested that kind of tricky things
// getting the element WebElement
eSupplierSuggest = driver.findElement(By.xpath("..."));
//get le location and click
Point location = eSupplierSuggest.getLocation();
new Actions(driver).moveToElement(eSupplierSuggest, location.x, location.y).click();
//or
//directly perform
new Actions(driver).moveToElement(eSupplierSuggest).click().perform();
i red somewhere that the click focus the element, but in my case, nothing works. lol
PS : this is a sub-question of that original post Click on a suggestbox, webdriver
I normally send an empty key to the element so it gets focused. So for example:
element.send_keys ""
In order to set focus on the element you can use executeScript method as described below :
JavascriptExecutor js;
js.executeScript ("document.getElementById('x').focus()");
Once the focus is set you can easily use send_keys provided by webdriver api.
Try using cssSelector for the autosuggestion click as shown below and let me know if you are still facing the issue.
// supplier ops, i find and type data into the input
WebElement eSupplier = driver.findElement(By.id("supplier:supplierOps_input"));
eSupplier.sendKeys("OPS1");
sleep(5); // wait the suggestbox
// i find the suggestbox
WebElement eSupplierSuggest = driver.findElement(By.cssSelector("css path of that specific value in the auto suggestion box"));
eSupplierSuggest.click();
sleep(5); // wait the refresh for the next field supplierAddress
There is no function in the WebDriver API to set focus on an element.
If you want to do it you would have to write some JavaScript to set focus and then use a JavaScriptExecutor to run the JavaScript.
Make sure, that you are not changing the frame....
Other wise .click() should do the trick

Getting Java Webdriver 'Down Arrow' action to work

So I have been trying to resolve this for several hours. I have no clue what I am doing wrong.
This is a type ahead field I am looking in is <input type="text" id="id_attendees" name="attendees">. When I type in there a js dropdown is created. When I press the Down Arrow on keyboard it works fine and selects the top choice. When I do keyDown --- id=id_attendees --- \40 in IDE it works fine and also selects the choice.
I cannot get it to do the same in Java webdriver though
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN);
^doesn't work.
driver.findElement(By.id("id_attendees")).sendKeys(Keys.ARROW_DOWN);
^doesn't work
I tried Keys.DOWN in both cases, that doesn't work either. I created a literal String altm = "\u0040"; and all that does is type an # symbol.
I also tried a bunch of other things as well and nothing is working. I have no clue what am I missing.
EDIT 1:
#Roddy Thank you! - Given that link I added the following that did work (after importing DefaultSelenium and WebDriverBackedSelenium.
DefaultSelenium sel = new WebDriverBackedSelenium(driver,vars.siteurl);
sel.fireEvent("//input[#id='id_attendees']", "keydown");
EDIT 2:
--> DOH that doesn't work. I got overzealous apparently.
some time scripts takes some time to load the list so need to add wait,
WebElement ar=driver.findElement(By.id("id_attendees"));
Thread.sleep(1000);
ar.sendKeys(Keys.ARROW_DOWN);
I think your use of Actions is not quite right.
The implementation is a builder pattern. Calling sendKeys doesn't send the event, it only stages the event to be fired when you call perform. Note that the return value of sendKeys is an Actions instance.
Actions actionObject = new Actions(driver);
actionObject = actionObject.sendKeys(Keys.ARROW_DOWN); //ASSIGN the return or you lose this event.
actionObject.perform(); //Should do what you want. Note that this will reset the builder.
Hope that helps.
With Actions class, after defining what it will do for you, you need to first build() it. So in your case it would be like this:
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN).build();
When you want your script to execute that action, you need to perform() it. You can chain it right after your build() method (if you are using it just once, for example) or later in your code whenever you need it, like this:
actionObject.sendKeys(Keys.ARROW_DOWN).build().perform();
OR
actionObject.perform();
Good luck!

Categories