How to Select a Date from Date dropdown in Selenium - java

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

Related

creating gmail account using selenium

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

Unable to Select the date in a date picker which is having two month blocks-- http://www.cleartrip.com/flights

Unable to pick a date from the website [][1]http://www.cleartrip.com/flights
Below is the code that i have tried. Please help me out with this. Thank you in advance.
Error while running the code: Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with tag name == table (WARNING: The server did not provide any stacktrace information)
//Select a date from the date picker-- Simple(Select July 18 2015)
driver.findElement(By.id("DepartDate")).click();
//Wait for some time
Thread.sleep(1000);
//table div
WebElement tableWholeBlock = driver.findElement(By.id("ui-datepicker-div"));
List<WebElement> tableWholeBlockVals = tableWholeBlock.findElements(By.tagName("div"));
WebElement lastBlockTable = tableWholeBlockVals.get(1);
//WebElement lastMonthblk = tableWholeBlock.findElement(By.className("monthBlock last"));
//WebElement lastMonthblk = driver.findElement(By.className("monthBlock last"));
WebElement monthTable = lastBlockTable.findElement(By.tagName("table"));
List<WebElement> tableRow = monthTable.findElements(By.tagName("tr"));
List<WebElement> tableCells = monthTable.findElements(By.tagName("td"));
for (WebElement cells : tableCells) {
if(cells.getText().equals("18")){
cells.click();
}
}
You can skip the looping and go straight to selecting a date if you use the correct XPath expression.
Example XPath to select the 18th day of the second month on display:
WebElement myDay = driver.findElement(By.xpath("//div[contains(#class, 'monthBlock') and contains(#class, 'last')]/table[#class='calendar']//td[normalize-space()='18']"));
When you click on Date Picker,it will display dialog which has two months i.e., current month and next month. You can use below xpath to select 29th date(today's date) of current month.
driver.findElement(By.xpath(".//div[#id='ui-datepicker-div']/div[1]/table/tbody/tr[5]/td[1]/a"));

Calendar Element not getting selected- Selenium Webdriver

I am learning automation with selenium Webdriver(Java) and I wanted to practice some stuff on this webpage.
I am having trouble selecting a particular date using date picker. Here is my code which attempts to do that:
String parentWindow = driver.getWindowHandle();
String subWindow = null;
driver.findElement(By.xpath(".//*[#id='ns_7_CO19VHUC6VU280AQ4LUKRK0IR7_fmOutboundDateDisplay']")).click(); //Clicking on datepicker icon
// Change to a new window
String parentWindow = driver.getWindowHandle();
String subWindow = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator1 = handles.iterator();
while (iterator.hasNext()){
subWindow = iterator.next();
}
driver.switchTo().window(subWindow);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#class='calendarBodyContainer']/tr[2]/td[3]/span")).click(); //Departure Date- 10Feb/2015
driver.findElement(By.xpath(".//*[#class='calendarBodyContainer']/tr[4]/td[4]/span")).click(); //Arrival Dtae- 25 Feb/2015
driver.switchTo().window(parentWindow);
However, I am getting the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[#class='calendarBodyContainer']/tr[2]/td[3]/span"}
Command duration or timeout: 3.12 seconds
Please help.
The problem is you mistook the calendar widget as new window and automated accordingly, which resulted in the element not found, rightly suspected by #alecxe
Please try the below code and see if it works out for you.
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Navigating to the site
driver.get("http://www.lufthansa.com/online/portal/lh/us/homepage");
//Clicking on the Departing field to select date
driver.findElement(By.id("ns_7_CO19VHUC6VU280AQ4LUKRK0IR7_fmOutboundDateDisplay")).click();
//Selecting Feb 10, 2015 for departure date
driver.findElement(By.xpath("//td[#dojoattachpoint = 'calRightNode']//span[.='10']")).click();
//Waiting for the return calendar with "Return" as the header to appear
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#dojoattachpoint='calHeadlineNode' and contains(text(),'Return')]")));
//Selecting Feb 26, 2015 for returning date
driver.findElement(By.xpath("//td[#dojoattachpoint = 'calLeftNode']//span[.='26']")).click();
NOTE: I have added explicit wait for waiting for the Return text in the "Return Calendar widget" because it overlaps the Departing/Outbound Calendar, and hence selenium needs a little time to detect the change in DOM.
I would try two things:
try the following xpath (relying on the table element and span's text):
//table[#class='calendarContainer'][1]//span[. = '09']
explicitly waiting for an element:
WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[#class='calendarContainer'][1]//span[. = '09']"))).click();
I'm also not sure about the necessity of switching windows here.

WebDriver press button Google play

The task is press button "More" in https://play.google.com/store/apps/category/FINANCE/collection/topselling_paid
This is screenshot
http://c2n.me/i9LC1O
My code is:
String url = "https://play.google.com/store/apps/category/FINANCE/collection/topselling_paid";
WebDriver driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(100000, TimeUnit.SECONDS);
driver.get(url);
//Collects ONLY first 60 link. `enter code here`But I need more....
ArrayList<WebElement> linksToApp = (ArrayList<WebElement>) driver
.findElements((By.className("title")));
ArrayList<String> urlToApp = new ArrayList<String>();
Please, help me.
You need to do something like
driver.findElement(By.id("id-of-show_more-button")).click();
You can find the element id byt right clicking on the element, click on inspect element and then checking for the id attribute in the HTML

How to get selected option using Selenium WebDriver with Java

I want to get the selected label or value of a drop down using Selenium WebDriver and then print it on the console.
I am able to select any value from the drop down, but I am not able to retrieve the selected value and print it:
Select select = new
Select(driver.findElement(By.id("MyDropDown"))).selectByVisibleText(data[11].substring(1 , data[11].length()-1));
WebElement option = select.getFirstSelectedOption();
But all my efforts were in vain. How do I get the selected option?
You should be able to get the text using getText() (for the option element you got using getFirstSelectedOption()):
Select select = new Select(driver.findElement(By.xpath("//select")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem );
Completing the answer:
String selectedOption = new Select(driver.findElement(By.xpath("Type the xpath of the drop-down element"))).getFirstSelectedOption().getText();
Assert.assertEquals("Please select any option...", selectedOption);
In Selenium Python it is:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
def get_selected_value_from_drop_down(self):
try:
select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'data_configuration_edit_data_object_tab_details_lb_use_for_match'))))
return select.first_selected_option.get_attribute("value")
except NoSuchElementException, e:
print "Element not found "
print e
On the following option:
WebElement option = select.getFirstSelectedOption();
option.getText();
If from the method getText() you get a blank, you can get the string from the value of the option using the method getAttribute:
WebElement option = select.getFirstSelectedOption();
option.getAttribute("value");
short answer
Select select = new Select(driver.findElement(By.xpath("//select")));
System.out.println("selected items from the dropdown"+ select.getFirstSelectedOption().getText());
var option = driver.FindElement(By.Id("employmentType"));
var selectElement = new SelectElement(option);
Task.Delay(3000).Wait();
selectElement.SelectByIndex(2);
Console.Read();

Categories

Resources