How to insert text in smart textbox in selenium webdriver? - java

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

Related

Selenium Testing - Text addition via pop up window is not getting saved

I am trying to automate a website using selenium web driver.
On the website, there is a popup form for creating a gate name with a text field to enter the text. Using selenium I tried to do, the pop up came but the given text was not getting saved. To pop up it's working correctly. Need guides to complete the action.
Methods I used are given below:
Alert alert=driver.switchTo().alert();
driver.switchTo().alert().sendKeys("New Gate");
alert.accept();
System.out.println(alert.getText());
Elements are identified by the Xpath element locator.
I also gave text along with URL, another method that I got from the stack.
If its an alert then right way handle as per your requirement steps should be
switch to and alert
enter text
get entered text
accept
Alert alert=driver.switchTo().alert();
alert.sendKeys("New Gate");
System.out.println(alert.getText());
alert.accept();`
If its a model popup then try to locate popup element and directly used send keys
sample code :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_popup_locator));
driver.findElement(By.you_textbox_locator).sendKeys("expected text");
You need to switch to pop-up first. Then you can access elements in the pop-up.
// save your main window handle
String MainWindow=driver.getWindowHandle();
// Get all window handle
Set<String> handles = driver.getWindowHandles();
Now, for each childHandle in handles,
driver.switchTo().window(childHandle );
// check if your textField present.
// If found, do your actions and break;
// else switch to next window
At last, switch to main window again.
driver.switchTo().window(MainWindow);

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.

Dropdown not working with selenium for this site

Hi I tried using the below code to use the dropdown from the foodpanda site and choose the city name but it's not working for me.
public static void main(String args[]) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.foodpanda.in/restaurants/city/pune?gclid=CIbFi5iEvdMCFdeFaAodujsK5w");
Thread.sleep(5000);
WebElement drp =driver.findElement(By.id("cityId"));
Select drp2 = new Select(drp);
drp2.selectByVisibleText("Bangalore");
It gives out the error-
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
That <select> element has the CSS display: none;. Its just a place holder.
I think the site uses some fancy <span>s to get the awesome look of the drop down. Can you please check that?
So I was trying to figure out how the dropdown works & here's what I found. The select is just a placeholder, it has no role on the webpage. The dropdown is populated using a javascript call when the input box (see below) is clicked.
Once you click inside the input box, some DOM elements are added which contain <p> tag with city names, which when clicked, selects the target city you want to select.
Based on my observation, a span with class name tt-suggestions is visible when the input element is clicked. This span has a div within it which has all the p elements within it. You should aim to click the corresponding p element which has the text of your desired city name.
Here's a working code:
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(
By.xpath("//input[#class='form-control twitter-typeahead tt-input']")
).click(); // this will click inside the input element
WebElement drp = (WebElement) wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//span[#class='tt-suggestions']"))); // this is the span element which gets populated
System.out.println(drp.getAttribute("innerHTML")); // just in case you want to see the above span's HTML code
drp.findElement(By.xpath("//div/p[text()[contains(.,'Bangalore')]]")).click(); // This will select Bangalore from the Dropdown

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

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

Get ID, Class of Currently Focused Element in Selenium

There are many ways to select or focus an element in Selenium, for example using TAB key we can focus on next element.
But, is there any way in Selenium to get all details of current focused element such as id, class, href, text etc ?
i want to focus on Like, Comment or Share button of a post https://www.facebook.com/pitbull/photos/a.440436327400.230702.95051637400/10153236215477401/?type=3&theater of Facebook page of Pitbull, But nothing works for me, i tried xpath, class, id but unable to Focus on share button. i can focus on share button using Tab key about 161 times but how will i confirm that focused element is "Share" button or somethong else? ;)
Here is my sample code
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("myemail#yahoo.com");
driver.switchTo().activeElement().sendKeys(Keys.TAB);
after TAB key, you know focus will be go to Password field from email field, so how can i get id, class or other details of focused element in my selenium code? in my example it is Password field.
You can use
WebElement activeElement = driver.switchTo().activeElement();
String className = activeElement.getAttribute("class");
String id = activeElement.getAttribute("id");

Categories

Resources