Not able to Select the month for Date of Birth.
Code I am using is:
driver.findElement(By.xpath(".//*[#id = 'BirthMonth']/div")).click();
Thread.sleep(3000);
WebElement months = driver.findElement(By.xpath(".//[#id='BirthMonth']/div[2]/div[#id=':1']"));
Thread.sleep(2000);
months.click();
I also tried with by using DropDownList case. But Not able to set any Month.
Please Say me the Solution.
You can use keyboard event replace mouse.
WebElement birthMonth = driver.findElement(By.id("BirthMonth"));
birthMonth.click();
Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).perform();
We can use sendKeys directly:
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]")).sendKeys("July");
You can wrap this up in a function
public void selectBirthMonth(int monthIndex)
{
driver.findElement(By.cssSelector("#BirthMonth > div")).click();
driver.findElements(By.cssSelector("#BirthMonth > div.goog-menu > div.goog-menuitem")).get(monthIndex - 1).click();
}
and then call it like
selectBirthMonth(9); // 1 = January
WebElement month = wd.findElement(By.xpath("//[#id=\"BirthMonth\"]/div[1]"));
month.click();
Thread.sleep(3000);
//wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//fetch months from the dropdown and store it in a list
List<WebElement> month_dropdown = wd.findElements(By.xpath("//[#id=\"BirthMonth\"]/div[2]/div/div"));
//iterate the list and get the expected month
for (WebElement month_ele:month_dropdown){
String expected_month = month_ele.getAttribute("innerHTML");
// Break the loop if match found
if(expected_month.equalsIgnoreCase("August")){
month_ele.click();
break;
}
}
It is not drop down value , you have to click on drop down arrows and then click on any value which you have to pass from script.
Code is below:
System.setProperty("webdriver.chrome.driver", "E:/software and tools/chromedriver_win32/chromedriver.exe");
WebDriver driver= new ChromeDriver();
//FirefoxDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://accounts.google.com/SignUp");
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='BirthMonth']/div[1]/div[2]")).click();
driver.findElement(By.xpath(".//*[#id=':7']/div")).click();
it is workable code for Birthmonth
Please find below link for same type of Question
Not able to select the value from drop down list by using Select method in Selenium
Related
I'm trying to click and select date from calendar dropdown for birthdate input .It's in the twitter signup page and it's a popup.
driver.findElement(By.xpath("//div[#aria-label='Date of birth']/descendant::select[1]']")).click();
Tried this xpath but the element is not getting clicked. I'm new to this. How to do this?
If the dropdown comes with "select tag" you can use Select Class as well as select options using selectByValue , selectByIndex and selectByVisibleText
Select month = new Select(driver.findElement(By.id("SELECTOR_1_LABEL")));
month.selectByValue("1");
This is the complete code:
WebDriverManager.chromedriver().setup();
WebDriver driver= new ChromeDriver();
driver.get("https://twitter.com/i/flow/signup");
Thread.sleep(2000);
driver.manage().window().maximize();
driver.findElement(By.xpath("//span[contains(text(),'phone')]")).click();
Select month = new Select(driver.findElement(By.id("SELECTOR_1")));
month.selectByValue("4");
Select day = new Select(driver.findElement(By.id("SELECTOR_2")));
day.selectByValue("3");
Select year = new Select(driver.findElement(By.id("SELECTOR_3")));
year.selectByValue("2023");
I am trying to click on dropdown value to select city in from field in Make my trip http://www.makemytrip.com/. But getting Stale element reference exception. Ids are getting changed on page load.
Tried below code:
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='ui-id-1']"));
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[#class='ui-menu-item'][2]"))));
To click on a dropdown value e.g. Mumbai you can use the following solution:
Code Block:
driver.get("https://www.makemytrip.com/")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='input_fromto checkSpecialCharacters ui-autocomplete-input' and #id='hp-widget__sfrom']"))).click();
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[#class='ui-menu-item'][starts-with(#id,'ui-id-')]//span[#class='autoCompleteItem__label']")));
for (WebElement element:myList)
if(element.getText().contains("Mumbai"));
element.click();
Browser Snapshot:
You can use this working code:
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='ui-menu-item'][2]/div/p[1]/span[1]"))).click();
I have fixed the xPath of dropdown list element. Always try to specify the exact element yo want to interact with. For example if you want to click on button, try to find <span> or <button> tag, for a link <a> tag and for input fields <input> tag.
You can try this code :
I do not see any use of xpath in this scenario. I have converted some of the xpath to either css selector or id. and have kept only one. Though I have not faced any stale element reference.
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://www.makemytrip.com/");
WebElement from = wait.until(ExpectedConditions.elementToBeClickable(By.id("hp-widget__sfrom")));
from.click();
from.clear();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul[class*='ui-widget-content hp-widget__sfrom']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(#aria-label,'Top Cities : Mumbai, India ')]"))).click();
The below code works fine for me and it is parameterized as well, it works for any input value without changing the xpath. In this example, I took mumbai as test data.
driver.get("https://www.makemytrip.com/");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).clear();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).click();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).sendKeys("Mumbai");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//div[#class='autoCompleteItem']/p/span[contains(text(),'Mumbai')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();
I am writing code for multi select using selenium(java) where i need the following task to be performed :
select multiple options in drop down
to click on button first selected which will print the first selected option from drop down.
to click on button get all selected which will print the all selected options in order .
I have this code which returns me undefined as a result for 2nd task.
public class MultipleSlectList {
public static WebDriver driver ;
#BeforeTest
public void startbrowser () throws Exception {
System.out.println("launching browser");
System.setProperty("webdriver.gecko.driver", "H:\\Selenium3\\geckodriver-v0.19.1-win32\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
}
#Test
public void selectlist () throws Exception {
WebElement ele1 = driver.findElement(By.id("multi-select"));
Select se= new Select(ele1);
se.selectByValue("New Jersey");
Thread.sleep(2000);
se.selectByValue("Texas");
Thread.sleep(2000);
se.selectByValue("Florida");
Thread.sleep(2000);
//Thread.sleep(10000);
WebElement btn1= driver.findElement(By.id("printMe"));
btn1.click(); // it is supposed to return New Jersy
WebElement firstOption = se.getFirstSelectedOption();
System.out.println("The First selected option is::" +firstOption.getText());
List <WebElement> oSize = se.getAllSelectedOptions();
int iListSize = oSize.size();
// Setting up the loop to print all the options
for (int i = 0; i < iListSize; i++)
{
// Storing the value of the option
String sValue = oSize.get(i).getText();
// Printing the stored value
System.out.println(sValue);
}
}
}
Please help me to proceed further.
I have tried with jquery as well but no luck .The result is same as "undefined" in both case .
Thanks !
Steps to select all dropdown options:
Find the SELECT WebElement by webdriver.
Create Select class which is used to operate dropdown list.
Get all options list in the dropdown list.
Loop the options list, get each option value and use
Select.selectByValue(optionValue) to select it.
Then all the dropdown option has been selected.
Below article has the code example to implement above scenario.
http://www.dev2qa.com/select-dropdown-list-selenium-webdriver/
I am trying to test a trip planner website using selenium webdriver.
Say for e.g. I am testing cleartrip.com.
How to select a city from the lookup field using selenium?
For instance, I have to select the source city.
If I type DE, it gives me options like "Delhi, "Dehradun" based on what I have typed and out of these options, I have to select say Delhi.
How to achieve it using Selenium? Kindly suggest.
I am new to selenium and any help would be appreciated.
This code works for me
WebDriver driver = new FirefoxDriver();
driver.get("http://www.cleartrip.com/");
WebElement From = driver.findElement(By.id("FromTag"));
From.sendKeys("Del");
WebElement autoComplete = driver.findElement(By.id("ui-id-1"));
try{
(new WebDriverWait(driver, 5/*sec*/)).
until(ExpectedConditions.presenceOfElementLocated((By.cssSelector("li.list")))); }
catch(org.openqa.selenium.TimeoutException e){
System.out.println(e.getMessage());
}
List<WebElement> autoCompleteList = autoComplete.findElements(By.className("list"));
if(autoCompleteList.size()==0) {
System.out.println("autoComplete list NOT found");
}
else {
System.out.println("autoComplete list Found with elements: "+autoCompleteList.size());
}
for (WebElement ac: autoCompleteList){
if(ac.getText().contains("Delhi")){
ac.click();
break;
}
}
driver.close();
Please note this is just example, please improve the code adding verifications that objects not a null, remove the hardcoded strings, etc.
You can simply achieve it by putting up some wait and then clicking the desired city, like:
public CurrentPage selectFrom(String cityCode, String cityName){
driver.findElement(By.id("FromTag")).sendKeys(cityCode);;
new WebDriverWait(driver, 5).until(
ExpectedConditions.elementToBeClickable(
By.xpath(".//*[#id='ui-id-1']//a[contains(.,'" + cityName + "')]")))
.click();
return this;
}
I am running Selenium Web-driver using JAVA and facing an issue with auto-suggest input text field. When I enter a String "books" in the text field, an option would show up. Then I want to click or select the input populated on the auto suggest menu.
Below is the code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.amazon.com/");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("books");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("gwcswTooltip")));
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));
for (WebElement webElement : findElements)
{
System.out.println(webElement.getText());
}
You need to just pick the right locator.
Add the following line
List<WebElement> findElements = driver.findElements((By.xpath("//div[#id='srch_sggst']/div")));
instead of
List<WebElement> findElements = driver.findElements((By.id("gwcswTooltip").name("books on")));