Select DropDown option using selenium - java

If someone could help me with this mystery.. Here is the url
Here if you can see the Date of Birth Fields I am able to select the Month and Date but I am not able to select Year
I Tried selecting Using Value, Index it did not work the same code worked for Month and Date
Below is My Code:
WebElement W = driver.findElement(By.xpath("//html/body/form[#id='aspnetForm'][contains(#action,'ActivateAccount.aspx?key=')][#method='post'][#name='aspnetForm']/div[#class='border4']/div[#id='page']/div[#class='IE-SCroll-mid']/div/div[#class='change-info-contain']/div/div/div/label[3]/select"));
Select dropdown = new Select(W);
dropdown.selectByValue("1997");

Try implementing explicit wait with that. That will make sure the elemenet is present before start looking for that.
By byId = By.id("ctl00_mcp_ddlYear");
//use explicit wait to make sure the element is there
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(byId));
Select dropdown = new Select(myDynamicElement);
dropdown.selectByValue("1997");
You can also use the cssSelector directly wait for the specific option to be present with the following code
//explicit wait
By byCss = By.cssSelector("#ctl00_mcp_ddlYear>option[value='1997']");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(byCss));
element.click();

Related

Unable to identify and click on the "Next" button in date field

I am trying to automate the flight booking of http://www.mercurytravels.co.in using Selenium+java.
The issue that I am facing is that I'm unable to click on the ">" (next button) of the "Date of Return" date field in the Book Flights Online page.
Getting error message that --> Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
The code that I've written is:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
//Retrieving current year value
String currentReturnMonthYear = driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[2]")).getAttribute("innerHTML");
driver.findElement(By.xpath("//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")).click();
Request any inputs on issue that I'm facing?
org.openqa.selenium.ElementNotInteractableException Indicates that a click could not be properly executed because the target element was obscured in some way. ElementNotInteractableException is Thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.
Using Explicit wait:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
next.click();
Using Actions:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
Actions action = new Actions(driver);
action.moveToElement(next).click().build().perform();
Using JavascriptExecutor:
driver.findElement(By.cssSelector("input[name='returnDate']")).click();
WebElement next = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(
"//div[#class='datepicker dropdown-menu'][4]/div[#class='datepicker-days']/table[#class=' table-condensed']/thead/tr[1]/th[3]")));
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].click();", next);

How to select last option on dynamic list using Selenium and Java

How to select last option on dynamic list Using Selenium and Java if I have a list with option that going to expend with new options every time.
List options WebElement examples:
//div[#id='transaction-history-view']/div[#id='accordion']/div[1]/div[#role='tab']//a[#role='button']
//div[#id='transaction-history-view']/div[#id='accordion']/div[2]/div[#role='tab']//a[#role='button']
//div[#id='transaction-history-view']/div[#id='accordion']/div[3]/div[#role='tab']//a[#role='button']
//div[#id='transaction-history-view']/div[#id='accordion']/div[4]/div[#role='tab']//a[#role='button']
See if this works. Get all the options in the list and size()-1 will get the last index to select the last element.
Select select = new Select(driver.findElement(By.xpath("your xpath for select element")));
List<WebElement> allOptions = select.getOptions();
int index = allOptions.size();
select.selectByIndex(index-1);
Based on your example I wrote this:
String xpathLastDiv = "(//div[#id='transaction-history-view'] /div[#id='accordion'] /div)[last()]"
String xpathLastButton = xpathLastDiv + "/div[#role='tab'] //a[#role='button']";
WebDriverWait webDriverWait = new WebDriverWait(driver, 10); //seconds
WebElement lastButton = webDriverWait.until(
ExpectedConditions.elementToBeClickable(
By.xpath(xpathLastButton)
)
);
lastButton.click();

Unable to click on submenu using Selenium Java

I'm trying to click on a submenu in a webpage. Submenu will be displayed when I just hover over the main menu. I have referred many solutions and it sill doesn't work.
And there are no iframes on the page.
Below is the code:
Actions ac = new Actions(d);
WebElement we = d.findElement(By.xpath("(//ul[#id='menu-top-menu']//li)[1]"));
ac.moveToElement(we).perform();
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement we2 = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("(//ul[#id='menu-top-menu']//li//ul//li[2]//a)[1]")));
ac.moveToElement(we2).click().perform();
Can someone please help me out with it?.
If it's not necessary to simulate mousein and click, you can get the href value of a element and direct access with driver.
You just need to check if element is present.
String xpathAction = "//ul[#id='menu-top-menu'] /li[1] /ul /li /a[text() = 'Action']";
WebDriverWait wait = new WebDriverWait(driver, 10);
String hrefAction = wait.until(
ExpectedConditions.presenceOfElementLocated(By.xpath(xpathAction))
).getAttribute("href");
driver.get(hrefAction);

Automate/handle 5th dropdown url https://jedwatson.github.io/react-select/ named as Github users(Aysnc with fetch.js)

I have problem to automate this drop down using selenium web driver using Java
This is the link - Go to 5th drop down named as Github users (fetch. js)
I am not able to enter the data into search field.
I am using send keys after perform click but it throws an exception like this " element is not interact able"
Steps I follow
driver.findElement(By.xpath("xapth")).click
drop down opens with no options because it is searchable and options are coming dynamically after entering key word into the search field.
driver.findElement(By.xpath("xapth")).sendkeys("Test");
Sendkeys are not working in this case because of drop down closed when perform send keys action.
<div class="Select-placeholder">Select...</div>
Below is the code which is working.
Please do optimize the code by removing thread.Sleep and putting some meaningful waits as per your requirement.
driver.Navigate().GoToUrl("https://jedwatson.github.io/react-select/");
IWebElement element1 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']"));
IWebElement element2 = driver.FindElement(By.XPath("//span[#id='react-select-6--value']/div[2]/input[1]")) ;
element1.Click();
Thread.Sleep(2000);
element2.SendKeys("Test");
Thread.Sleep(1000);
element2.SendKeys(Keys.Tab);
Please note that element2 gets activated once you click on element1.
Try the following code:
public void typeAndSelect() {
WebElement searchBox = driver.findElement(By.xpath("//div[#class='section'][5]//div[#class='Select-control']"));
searchBox.click();
WebElement inputField = driver.findElement(By.xpath("//div[#class='section'][5]//input[#role='combobox']"));
inputField.clear();
String searchWord = "test";
inputField.sendKeys(searchWord);
WebElement selectDropdown = driver.findElement(By.xpath("//div[#class='Select-menu-outer']//div[#role='option'][text()='" + searchWord +"']"));
// wait for search results.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(selectDropdown)).click();
}
Correct the following xpath part
"//div[#class='section'][5]"
to your implementation of the dropdown

Select options from Autopopulate text boxes using Selenium webdriver

Driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
WebElement dropdown = (new WebDriverWait(Driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='client']")));
Driver.findElement(By.xpath("//*[#id='collapseClientInfo']/div/form/div[3]/div[2]/ul/li[1]/a")).sendKeys(Keys.ENTER);
Could you please help me to select auto populate value from drop down list:
We've Client textbox which is an auto-populate box.
When I enter "ho" text in the client field, it shows me the drop down which has values related to my entered text i.e. ho, then I have to select those values which are available under list.
In above code I've tried with Press Enter but unable to select the value.
Could you please check the above code and help me out for the same?
You should try as below :-
WebDriverWait wait = new WebDriverWait(Driver, 60);
//wait until loader invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loaderDiv")));
//this sleep is required because after invisibility of loader focus goes to first input which is Requisition Number
//If you are filling form from first input no need to for this sleep
//if you want to input directly to client field need to sleep to avoid focus first
Thread.sleep(3000);
//Now find the client input and set value
WebElement client = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("client")));
client.sendKeys("Ho");
//Now find all the showing option
List<WebElement> dropdownOptions = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("ul.dropdown-menu a")));
//Now select the first option
dropdownOptions.get(0).click();
Below approach might be helpful:
// Enter text in auto complete text box
driver.findElement(By.xpath("//*[#id='client']")).sendKeys("Ho");
// Wait for options to display
Thread.sleep(5000);
// Option to select
String optionToSelect = "Honda";
Boolean isOptionSelected = Boolean.FALSE;
// Get the options displayed
List<WebElement> options = driver.findElements(By
.cssSelector("ul.dropdown-menu a"));
// Select option
for (WebElement webElement : options) {
if (webElement.getText().equalsIgnoreCase(optionToSelect)) {
webElement.click();
isOptionSelected = Boolean.TRUE;
}
}
if (isOptionSelected) {
// Option is selected
} else {
// Expected option is not displayed. Fail the script
}
Try this:
Select drpCountry = new Select(driver.findElement(By.id("searchOptions")));
drpCountry.selectByVisibleText("By author");

Categories