I am trying the code by JAVA - Selenium for the WebApplication - java

Can anyone select any item from the drop down list box of "Furniture Type". I got the clicking on the the furniture type by
driver.findElement(By.xpath("(//select)[1]")).click();
But I am unable to select any one text from the drop down list box. By Select class I tried, But I couldn't get the solution.
The url is " http://bangalore.quikr.com/post-classifieds-ads/?postadcategoryid=218 "
The exception I am getting is "no such element exception".

Usually you should first check the path using developer tools or similar browser extension, or record using Selenium IDE. In this case for instance, I don't see any select element on that page: Furniture Type is an input with type=text and the selectable options are input with type=radio. Also (//select)[1] is not a valid xpath.
So you can find it by
WebElement typeBox = driver.findElement(By.id("Furniture_Type_searchBox"));
Since Furniture Type is an input with type=text, you can then focus on it like this:
typeBox.sendKeys("");
That should also open a drop-down, from which you should select a radio button like this (selecting 'Bed Set' for example):
WebElement radio = driver.findElement(
By.xpath("//div[#class='attr-container']//input[#lblval='Bed Set']"));
And you click to select it
radio.click();

Related

How to get value from nested xpath in selenium

I am trying to test an application with Selenium and Java, what I want to achieve, is the following.
Click on a select box (which works) and then select from a multiple selectoption the one equaling my input. So if my Feature file says dog I want from the options dog to be selected.
My code to select the option after opening the selectbox looks like this:
WebElement xyz = driver.findElement(By.xpath(String.format("//*[#id='xyz']/div[2]/ul/li[text()='%s']",selectElement)));
If I delete /li every option is selected in my console in the browser, so I thought with /li[text()='%s'] I could dynamically set the option name. But I am getting a nosuchelementexcetion, what could be my mistake? If i right 1 instead of text() = '%s' I am being able to select the option
For the select box, you need to create a Select object which targets the select box and then select by value to get the item you want to select. For example:
Select drpDown = new Select(driver.findElement(By.xpath(//pathToElement]")));
drpDown.selectByValue(selectElement);
Hope this helps :)

Can't select the drop-down option

Using the select method I can't select the dropdown
I tried using normal and findelement method and also index selection method
Here am using input value in different file
click(driver,"id",prop.getProperty("state"));
click(driver,"xpath",prop.getProperty("voption"));
and
Index selection method
and
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
Expected result :
need to click the dropdown
Actual result :
"stale element reference: element is not attached to the page document"
it showing error message like this
Try locating the WebElement first, then select by visible text.
WebElement dropDown = driver.findElement(By.id("state"));
new Select(dropDown).selectByVisibleText("ANTARCTICA");
If this does not work, but you dont get the StaleElementReferenceException change the selection option to selectByIndex() or selectByValue().
If you get StaleElementReferenceException pointing to line with driver.findElement(...) it means that something on your page has changed so some kind of wait mechanism should be introduced. In such case I suggest to locate the dropDown using FluentWait.

How to select an item from a CSS custom dropdown list using Selenium WebDriver with java?

I want to select an item in a CSS dropdown but there seems to be no way to do it, we can take as an example the google hotel review page here how can I select by most recent reviews programmatically through selenium ?
Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically.
I've tried this way :
Select select = new Select(driver.findElement(By.xpath("//*[#id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
select.deselectByIndex(1);
but I'm getting an exception(org.openqa.selenium.support.ui.UnexpectedTagNameException) saying :
Element should have been "select" but was "g-dropdown-menu"
Is there a way to simulate a click on a CSS dropdown element like this with Selenium web driver ?
Analysis:
Selenium Java API: Select.class only suitable for dropdown which use HTML select tag. For dropdown implment by other way, like JQuery dropdown plugin, Select class not support, for such dropdown you need to click on the drop down to make the options to display out, then choose the option you wanted.
Solution:
public void selectSortby(String sortBy) {
// click on dropdown to expand options
driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
// choose option
driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).click();
}

To Automate a dropdown box without Select tag using Selenium

I'm trying to automate a drop down box in home page of HDFC bank in which by default 'Net banking' is selected. It does not have a Select tag and it has div tag. Also I tried to click on the drop down first and select the values one by one, but I'm getting error like
Element can't be clicked
Please help me with this.
driver.findElement(By.className("selectedvalue")).click(); //Error for this statement
try below
driver.findElement(By.cssSelector("div.loginwrap > div.selectWrapper > div.selectedvalue")).click();
driver.findElement(By.id("prepaidcard")).click();
I generated this from Selenium IDE for firefox. It is a plugin for Firefox which you can record user actions and export the test case to Java ( among many other languages).
This is the link Selenium IDE
You can also try for :
driver.findElement(By.xpath("//div[contains(text(),'NetBanking')]")).click();
driver.findElement(By.xpath("//li[#id='prepaidcard']")).click();

How to insert text in smart textbox in selenium webdriver?

I am trying to automate the code of submit resume page, it consists on smart textboxes, which gives suggestions below as soon as you type few text in it. you need to select and input into the textbox from the suggestions given. Below is the code and the url:
WebDriver w= new FirefoxDriver();
w.get("https://www.hrmantra.com/LetsLead/18_Recruitment/SubmittResume.aspx?cn=LetsLead");
w.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");
Only sendkeys command is not working as the entered value has to be selected and the control needs to be closed.
The text box has a select drop down box which appears dynamically on entering the city from it the user selects his city but this select box is inside an iframe (iframe id : SpeedTyperFrameID)so we need to switch to it and then access the select box
Below is the code
WebElement city = driver.findElement(By.xpath("//*[#id='StCityName_txtSpeedName']"));
city.click();
city.sendKeys("chennai");
//wait for the iframe to load and then switch to it
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("SpeedTyperFrameID")));
Thread,sleep(3000);//added just to show u the effect remove it
WebElement byValue = driver.findElement(By.id("SelectList"));
//using select class to select the element by its text
Select select = new Select(byValue);
select.selectByVisibleText("Chennai");
//switch back to default content inorder to access other elements outside the iframe
driver.switchTo().defaultContent();
I have tested the above code it is working fine
Kindly get back if you have any queries.
Ideally, After you type partial text into input you have to find all suggestions from dropdown list and click on it. Perhaps, try to use enter key, but I am not aware that it will help
element.sendKeys("Mumbai" + Keys.ENTER)
It's because there is no ID of id=StCityName_txtSpeedName on the page. You don't use id=... for the ID, you just type the ID.
See below.
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");

Categories

Resources