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.
Related
I am using the select class of selenium to select the webelement from the dropdown list. when I select the webelement and submit it..the page is refreshing and the webelement is reset to default value. I need to select the next webelement after refreshng the page? How can I do
if the webpage is getting refreshed after your first select code execution then automatically then value of your earlier select action:
may be persisted.
if it is persisted then move to next select option. Please paste your code here and the error you are getting.
Reset to default.
if it is resetted to default ask your dev to change the code. because as per as I know there is no was of doing that in selenium.
I want to click on drop down button and select one among one and click on it.I tried by many ways but it is not working. Attached image of html code, Anyone help me regarding this.
Below is my code which i tried
driver.findElement(By.id("homepages_dropdown")).click();
/*List<WebElement> allElements = driver.findElements(By.xpath("//*[#id=\"homepages_item_AccountManagerDashboard\"]"));
for (WebElement element: allElements)
{
System.out.println(element.getText());
}*/
Just use "Select" to create an object that handles dropdowns etc. You can use "Select" to select element by it's text, index and a bunch of other options. It's the easiest way to handle such elements as dropdown and comboboxes.
new Select(driver.findElement(By.id("homepages_dropdown"))).selectByVisibleText("Your option's text");
This isn't the sharpest way to do it, but if you can't get developers to make it a specific select element via HTML it may have to do. I have stuff like this in my solution where there are custom dropdowns.
You can start by creating a dropdown element and then click a single record in it if that's what you're trying to do, in your case it looks like it is the span within the highlighted button element, so you can do something like the following:
driver.findElement(By.id("homepages_dropdown")).click();
driver.findElement(By.xpath("//li//a[#id='homepages_item_AccountManagerDashboard']").click();
The above statements will click the dropdown then click the element with the id of homepages_item_AccountManagerDashboard.
You can also create a function that clicks any li of based off of a parameter you pass into a method you create, consider this if you may select many options in your dropdown.
public void SelectItem(string itemText)
{
var dropdownElement = driver.findElement(By.id("homepages_dropdown"));
var selectionItem = driver.findElement(By.xpath("//li[text()='" + itemText + "']");
dropdownElement.Click();
selectionItem.Click();
}
This is only really a valid option if you are working through the Page Object Model, but it's the best way to make things work consistently long term.
To click on Dropdown button and Select the Option with text as Analytics you can use the following code block :
driver.findElement(By.xpath("//div[#id='homepages']/button/span[#id='homepages_dropdown']")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='homepages']/ul/li/a"));
for (WebElement element : allElements)
if(element.getAttribute("innerHTML").contains("Analytics"))
{
element.click();
break;
}
System.out.println("Option with text as Analytics is selected");
I found solution myself,Below code worked
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("homepages_dropdown")));
// drop down
driver.findElement(By.id("homepages_dropdown")).click();
Thread.sleep(2000);
// selecting configuration
driver.findElement(By.id("homepages_item_ConfigurationHomepage")).click();
Thread.sleep(2000);
I am using firebug for finding xpath, this is the error that is displayed in the error console.
no such element: Unable to locate
element:{"method":"xpath","selector":".//*[#id='select2-contact_id-result-v0w5-258']"}
driver.findElement(By.xpath(".//*[#id='select2-contact_id-result-v0w5-258']")).click();
html is as follow
id="select2-contact_id-result-v0w5-258" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Single contact
There could be following reason for it :-
May be your id is dynamically generated, so you need to try with different locator to create By object as below
By by = By.className("select2-results__option select2-results__option--highlighted");
or
By by = By.cssSelector(".select2-results__option select2-results__option--highlighted");
or
By by = By.xpath("//*[contains(., 'Single contact')]");
or if id is not dynamically generated
By by = By.id("select2-contact_id-result-v0w5-258");
May be your element is inside a frame or iframe, If it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("frame id or name or index");
May be element is not being fully loaded on the page due to slow internet, so you need to implement WebDriverWait to wait until element is visible and clickable as below :-
WebDriverWait wait = new WebDriverWait (driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(by)); //use anyone of the above by object
Now after successfully getting element you need to perform click as below :-
el.click();
Note :- if your element is actually a dropdown with select tag, then you need to create Select() object to work with dropdown as below :-
Select sel = new Select(el);
//now perform step to select an option by visible text from dropdown
sel.selectByVisibleText("your visible option text");
or
sel.selectByIndex("your option index");
or
sel.selectByValue("your option value");
Hope it helps you..:)
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");
Link: http://www.bbc.com/weather/
Scenario: Type "reading" on the find a forecast text box. This shows 2 options. How do I select one option using Selenium WebDriver?
I am using the following command to type "reading"
driver.findElement(By.id("locator-form-search")).sendKeys("Reading");
I added explict wait as you suggested and this worked with a small change in the css path. This is what I did:
WebElement suggestedList = new WebDriverWait(driver, 15)
.until(ExpectedConditions.elementToBeClickable(By
.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)")));
driver.findElement(
By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] >ul >li:nth-of-type(1)"))
.click();
Use the following locator to click on the first element (first suggestion displayed after typing):
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(1)")).click();
Similarly you can click on the other element by changing the locator. For example to select the second element:
driver.findElement(By.cssSelector("div[class='locator-suggestions locator-suggestions-default'] li:nth-child(2)")).click();
Just to add here, you may need to wait for the elements/suggestions (which you're trying to select) to be visible, before trying to click on those. Use/search for a appropriate wait method for your testing.
So,
first you need to type "reading" in the field as you are doing by using driver.findElement(By.cssSelector("input#locator-form-search")).sendKeys("Reading");
then wait for element to be visible
Then try the above mentioned code to select the desired element.
Hope this helps.