I am trying to automate Goibibo Website in Selenium using Java. After Clicking on Search options there are lot of tabs with BOOK option.
On Inspect, All tabs have same X-Path. How to select one of them?
Following is the Code:
<input type="button" value="BOOK" class="button orange fr fn ft_bookbtn ">
Use indexes
List<WebElement> books = driver.findElements(By.cssSelector("[value='BOOK']"));
books.get(1);
if you want to take the first on you can also use
WebElement book = driver.findElements(By.cssSelector("[value='BOOK']"));
if you want random selection you can do this:
List<WebElement> books = driver.findElements(By.cssSelector("[value='BOOK']"));
int r = new Random(books.length).nextInt();
WebElement book = books.get(r);
Related
Recently I was trying to use the traditional drag-and-drop operation using 'Action' object, I tried it using it straight-forward afterwards I understood that the elements of which I want to drag are changing dynamically.
before the element looks like this:
<div data-v-a030b586 tabindex="-1" class="v-list-item theme--light">...</div>
after clicking on the element it changes to:
<div data-v-a030b586 tabindex="-1" class="v-list-item theme--light" draggable="false">...</div>
When holding the element it changes to:
<div data-v-a030b586 tabindex="-1" class="v-list-item theme--light sortable-chosen" draggable="true">...</div>
I tried to do this by using this code:
List<WebElement> elems = driver.findElements(By.cssSelector("div[class*='row']:nth-child(2) [class*='col-md']:nth-child(3) div[tabindex='-1'] div[class='v-list-item__icon icon-item-wrap'] i"));
Actions builder = new Actions(driver);
for(WebElement elem: elems){
System.out.println(elem.getText());
elem.click();
}
elems = driver.findElements(By.cssSelector("div[draggable]"));
WebElement sourceElm = elems.get(2);
WebElement destElm = elems.get(0);
builder.clickAndHold(sourceElm);
builder.clickAndHold(destElm);
builder.clickAndHold(sourceElm).moveToElement(destElm).click().release(destElm).build().perform();
But as I tried it nothing is happening. What is wrong ? or what is the correct way of doing it, if possible of course...?
**************************** EDIT *********************************
This is how the dom looks when you hold an element of if clicking on it as I described above:
After clicking one hyperlink in my application. There is a Dojo popup appears mentioned below:
From the dropdown I need to select QA/SIT environment value.
HTML code for this is mentioned below:
<input class="dijitReset dijitInputInner" type="text" autocomplete="off" data-dojo-attach-point="textbox,focusNode" role="textbox" tabindex="0" id="dijit_form_FilteringSelect_0" aria-required="true" value="" aria-invalid="false">
Now, I tried below code to select the value from dropdown But I am getting no such element exception.
Select drpCountry = new Select(driver.findElement(By.xpath("//*[#id=\\\"dijit_form_FilteringSelect_0\\")));
drpCountry.selectByIndex(4);
I am getting below No such element exception error.
Can anyone please explain what I am missing here. How to Automate Dojo select dropdown
You can try below code to select your option. First click on drop down arrow present in there
:
String option = "QA/SIT"
driver.findElement(By.xpath("//input[#class='dijitReset dijitInputField dijitArrowButtonInner']")).sendKeys(option ) # Choose a valid xpath if its not correct
optionXpath = "//div[text()='"+option+"']"
optEle = driver.findElement(By.xpath(optionXpath ))
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(""arguments[0].click();",optEle );
Problem Description:
I am trying to automate the filling of the form in which there's a
locality dropdown list. I am facing problem in selecting the option
which are suggest in the suggestion list. The options in the
suggestions list are provided by an api response i.e its dynamic.
I am not able to select one of the option from the suggested list.
String Locality ="//label[contains(text(),'Locality')]/following-sibling::input";
public void insertData(DataTable str) throws InterruptedException {
List<List<String>> list = str.asLists(String.class);
super.identifyElement(locators.xpath, Locality), list.get(1).get(5));// value sendkey= Akurdi;
HTML Element:
<label _ngcontent-c4="" for="Location" class="active">Locality</label>
<input _ngcontent-c4="" autocapitalize="off" autocorrect="off" class="mb-0 ng-valid ng-dirty ng-touched" formcontrolname="locality" placeholder="" spellcheck="off" stype="locality" type="text" ng-reflect-klass="mb-0" ng-reflect-ng-class="[object Object]" ng-reflect-name="locality" ng-reflect-model="Akurdi" autocomplete="off">
Sendkey value form cucumber screnario:
Akurdi...
Display Input dropdown list UI
In these cases I am working with these drop-downs as with usual List. Clicking on drop-down opening, waiting for drop-down to appear, then iterating in the list of it's elements, searching what I need by name, then clicking on it.
Solution
WebDriver driver = new ChromeDriver();
driver.get("http://connexistech.net/godamwale/warehouse");
WebElement locality = driver.findElement(By.xpath(".//div[#id='location']//input"));
locality.sendKeys("Akrudi");
//Here at the end [1] can be any item from 1 to 5 since its showing only 5 suggestions all the time
String xp = "//div[contains(#class, 'pac-container')]//div[contains(#class, 'pac-item')][1]";
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//div[contains(#class, 'pac-container')]/div[contains(#class, 'pac-item')]")));
WebElement element = driver.findElement(By.xpath(xp));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("xp")));
element.click();
I have a little problem selecting options out of drop down lists with selenide (java).
Here's a little snippet of the HTML code and my try to select the option by the value:
HTML snippet
[Java code]
String dateRangeSearchFor = "YESTERDAY";
ElementsCollection ListOfOptions = $(By.id("searchMaskForm:jobSearch_dateRange_input")).$$(By.tagName("option"));
logger.info("selecting option");
for (SelenideElement listElement : ListOfOptions)
{
String valueOfElement = listElement.getAttribute("value");
if (valueOfElement.equals(dateRangeSearchFor))
{
//$(By.xpath("//*[#id='searchMaskForm:jobSearch_dateRange_input']/option[contains(., '"+dateRangeSearchFor+"')]")).setSelected(true);
listElement.setSelected(true); break;
}
}
For some reason the code is not working, neither with the text nor with the index. Any suggestions?
Edit: .click(); and selectOption(); aren't working neither
SelenideElement have method selectOptionByValue(java.lang.String... value)
The piece of code below will help:
String dateRangeSearchFor = "YESTERDAY";
Select select = new
Select($(By.id("searchMaskForm:jobSearch_dateRange_input")));
select.selectByValue(dateRangeSearchFor);
In my case it did.
BTW, if the automation test suite you're creating is a part of automation, that includes functional and load testing, this link will help you to combine those tools in one system, check it out - How to automate Selenium and jmeter testing .
Selenide provides the following methods for a selecting an option in dropdownlist.
selectOptionByValue(value)
selectOption(text)
selectOption(index)
selectOptionContainingText(text)
If you know the index of the element in dropdown menu then you can just use built-in methon selectOption().
It will look like this:
$(CSS-selector).selectOption(index-of-element);
!!REMINDER: CSS-selector MUST point to the <select> element in HTML.
<select class="switch-version" id="switch-version-select" onchange="switchVersionSelect()">
<option value="/v1.x/demo/my_boss_is_in_a_hurry/flexigrid">Flexigrid</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/datatables">Datatables</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap" selected="selected">Bootstrap V3 Theme</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v4">Bootstrap V4 Theme</option>
<option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v5">Bootstrap V5 Theme</option>
</select>
In the example of the html code above, to select one of the value options with Selenide simply implement the code below.
public class HomePage {
SelenideElement bootstrapElement = $(By.cssSelector("#switch-version-select"));
public void selecionarDropDown(){
$(bootstrapElement).selectOptionContainingText("Bootstrap V4 Theme");
}
}
Good afternoon everyone,
So my issue is I am trying to select fields in drop down menu's that are hidden. Below is the HTML code for one of the said drop down menus.
<div id="mainForm:defectPriorityField" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="width:120px">
<div class="ui-helper-hidden">
<select id="mainForm:defectPriorityField_input" name="mainForm:defectPriorityField_input">
<option value="1-Urgent">1-Urgent</option>
<option value="2-Very High">2-Very High</option>
<option value="3-High">3-High</option>
<option value="4-Medium">4-Medium</option>
<option value="5-Low">5-Low</option>
</select>
Now I have read there are a couple ways I can do this. Below seeming to be the most popular.
String cssSelector= ...
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
The xpath for the drop down menu is //*[#id="mainForm:defectPriorityField_input"]
I am relatively new to this and am unsure how to generate or get a cssSelector. Please if anyone could point me in the right direction it would be greatly appreciated.
Here's the approach I'd take which is to first click on the drop down menu then select an option:
WebElement dropdown = driver.findElement(By.xpath("//div[#id=\"mainForm:defectPriorityField\"]);
dropdown.click();
WebDriverWait waitForOptions = new WebDriverWait(driver, 3);
waitForOptions.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id=\"mainForm:defectPriorityField_input\"]")));
WebElement option = driver.findElement(By.xpath("//div[#id=\"mainForm:defectPriorityField_input\"]/option[text()=\"1-Urgent\"]"));
option.click();
Please excuse any syntax errors but that's essentially the idea. In the last statement for the option element you can substitute the text "1-Urgent" with a variable or whatever you like.
If an element is Hidden, you should not be using it. As the user would not either.
If it is not then why not use the SelectElement?
SelectElement sel = new SelectElement(driver.FindElement(By.Id("Id")));
sel.SelectByValue("Value");