The driver is not unable to locate the element defaultCurrency by xpath.! - selenium

The driver is not unable to locate the element defaultCurrency by xpath.
Error -"org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with."
Code:
driver.findElement(By.xpath("//[#id='currency']/fieldset[2]/table/tbody/tr[1]/td[2]")).click();
WebElement defaultCurrency = driver.findElement(By.xpath(".//*[#id='defaultCurrency']"));
Select defaultCurrency_Select = new Select(defaultCurrency); defaultCurrency_Select.selectByVisibleText("USD");
Source Code:
<td>
<select id="defaultCurrency" class="validate[required]" onchange="javascript:clrErrors()" style="height:25px;width:160px;" name="defCurrency.defaultCurrency">
<option value="">Select Currency..</option>
<option value="INR">GBP</option>
<option value="EUR">EUR</option>
<option value="USD">USD</option>
</select>
</td>

WebDriver throw ElementNotVisibleException exception only when element is not visibile. Therefore, have a look at the element, (e.g. via Firebug) and note which element is activating him as to be visible. Then try to activate the first element, and then operate on desired one. Or maybe element is designed to be invisibile at that specific moment, so you should not be allowed to perform actions on it.

Use this
Select currency = new Select(driver.findElement(By.cssSelector("Select[id="defaultCurrency"]")));

Related

element not visible exception in selenium drop down

I am getting element not visible exception in carrier drop-down list. I tried implicit wait, explicit wait, and all id, class, XPath, CSS selector way to find the element. Please help me to get to get the right XPath. I have "element not visible" exception error message at select a carrier drop-down list. Thank you .
<select class="form-control error" id="smsCarrier" name="smsCarrier" required="" data-required-message="Carrier is required." aria-required="true" aria-invalid="false">
<option value="-1">Select a Carrier</option>
<option value="#message.alltel.com">Alltel</option>
<option value="#txt.att.net">AT&T</option>
<option value="#myboostmobile.com">Boost Mobile</option>
<option value="#sms.cricketwireless.net">Cricket Wireless</option>
<option value="#msg.fi.google.com">Project Fi</option>
<option value="#text.republicwireless.com">Republic Wireless</option>
<option value="#messaging.sprintpcs.com">Sprint</option>
<option value="#tmomail.net">T-Mobile</option>
<option value="#email.uscc.net">US Cellular</option>
<select class="form-control error" id="smsCarrier" name="smsCarrier" required="" data-required-message="Carrier is required." aria-required="true" aria-invalid="false">
<option value="-1">Select a Carrier</option>
<option value="#message.alltel.com">Alltel</option>
<option value="#txt.att.net">AT&T</option>
<option value="#myboostmobile.com">Boost Mobile</option>
<option value="#sms.cricketwireless.net">Cricket Wireless</option>
<option value="#msg.fi.google.com">Project Fi</option>
<option value="#text.republicwireless.com">Republic Wireless</option>
<option value="#messaging.sprintpcs.com">Sprint</option>
<option value="#tmomail.net">T-Mobile</option>
<option value="#email.uscc.net">US Cellular</option>
<option value="#vtext.com">Verizon</option>
<option value="#vmobl.com">Virgin Mobile</option>
</select>
I used Fluent wait.
public void ContinueWhenReady(By locator, int timeout)
{
for (int i = 0; i < 2; i++)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(locator));
break;
}
catch (Exception e)
{
}
}
}
//PhoneNumberDetails details = table.CreateInstance<PhoneNumberDetails>();
ContinueWhenReady(By.CssSelector("#smsCarrier"), 50);
var SelectaCarrierDropDown = driver.FindElement(By.CssSelector("#smsCarrier"));
SelectaCarrierDropDown.Click();
var selectaCarrierElement = new SelectElement(SelectaCarrierDropDown);
//selectaCarrierElement.SelectByText(details.SelectaCarrier);
selectaCarrierElement.SelectByValue("#myboostmobile.com");
In JAVA you can do something like :
WebDriverWait wait = new WebDriverWait(driver, 10);
Select dropdown = new Select(wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("smsCarrier")))));
dropdown.selectByVisibleText("Cricket Wireless");
As others have mentioned your question isn't really detailed enough to know your exact problem, but there are a couple common ones when working with dropdowns in Selenium that I can point out.
First, you need to make sure the select has actually loaded on the page before you try to find it. So you may need to use a WebDriverWait to do that. Example:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("smsCarrier")));
Then you can try to locate the dropdown and store it in a WebElement:
WebElement carrierDropdown = driver.findElement(By.id("smsCarrier"));
The trick is, to work with dropdowns you need to then wrap that WebElement in a Select object:
Select carrierSelect = new Select(carrierDropdown);
From there you can use methods on that Select object to manipulate it, such as selecting an option from the dropdown. Example:
carrierSelect.selectByVisibleText("Boost Mobile");
or
carrierSelect.selectByValue("#myboostmobile.com");
Hope that helps!

Getting error while selecting drop down value in selenium webdriver

From drop down, I am not able to select the value from dropdown.
HTML Code :
<select id="ctl00_ContentPlaceHolder1_drp85" class="form-control select2-hidden-accessible" data-varindex="9" onchange="SetVariableValue('9', this, 'DESCENDANT - DROPDOWN')" name="ctl00$ContentPlaceHolder1$drp85" tabindex="-1" aria-hidden="true">
<option value="-1">--Select--</option>
<option value="Please don't hesitate">Please don't hesitate </option>
<option value="Please reach out to us">Please reach out to us</option>
<option value="Remember we are here for you">Remember we are here for you</option>
<option value="If you need help">If you need help</option>
<option value="If you ever need additional help">If you ever need additional help</option>
</select>
Selenium WebDriver Code :
WebElement dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_drp85"));
selectbyindex=new Select(dropdown);
selectbyindex.selectByIndex(1);
Getting exception as :
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated".
Can anyone help where I am making mistake?
Just wait for the drop-down to be visible first and then select:
new WebDriverWait(driver, TimeSpan.FromSeconds(45)).Until(ExpectedConditions.ElementIsVisible((By.Id("ctl00_ContentPlaceHolder1_drp85"))));

Selenium webdriver select option

I'm trying to select an option from a select drop-down using Selenium/java/MS Edge driver, but I'm finding I can't select the option even though I can see I have located it.
My code so far;
final static String URL = "http://usedcars.bmw.co.uk";
final static String edgeDriverBinary = "MicrosoftWebDriver.exe";
System.setProperty("webdriver.edge.driver", edgeDriverBinary);
WebDriver driver = new EdgeDriver();
driver.get(URL);
// Click the field to activate the drop down
By byQsRangeBtn = By.xpath("//div[#id='quick-search']/form/fieldset[#class='form']/div/div[#class='js-ctm-dropdownlist']/div[#class='js-ctm-display']/span");
WebElement qsRangeElement = driver.findElement(byQsRangeBtn);
qsRangeElement.click();
Sending this click does activate the drop-down and I can see it expands
Select qsRange = new Select(driver.findElement(By.id("qsRange")));
log("OPTIONS: " + qsRange.getOptions());
In the above I can dump .getOptions() and I see all the data from the select options.
When I call this I just get an exception with no stack trace or useful logging information. I've tried .selectByIndex() and .selectByVisibleText() with the same result - an exception and no information.
qsRange.selectByValue("3648835");
The part of the page is below. The id="qsRange" is set as not visible in the page (grey'd out in firebug). class="js-ctm-display" is the only div that is visible.
If I manually click the drop down to display it's values then <div class="js-ctm-dropdownlist"> becomes <div class="js-ctm-dropdownlist js-open">, but firebug still sees the select as not displayed.
<form class="quicksearch mp-search-count" method="post" action="/search-cars.aspx">
<fieldset class="form">
<div class="search-control series js-ctm js-ctm-mode-full" data-theme-type="dropdownlist">
<div class="js-ctm-dropdownlist">
<select id="qsRange" class="mp-control-data" name="Range">
<option value="-1">Series</option>
<option value="890">1 Series</option>
<option value="3648873">2 Series</option>
<option value="66">3 Series</option>
<option value="3648835">4 Series</option>
<option value="67">5 Series</option>
<option value="68">6 Series</option>
<option value="69">7 Series</option>
<option value="3648830">X</option>
<option value="3648831">Z</option>
<option value="3648832">M</option>
<option value="3648938">PHEV</option>
<option value="3648833">Hybrid</option>
<option value="3648834">BMW i</option>
<option value="3648879">Alpina</option>
</select>
<div class="js-ctm-display">
<span>Series</span>
<span class="js-ctm-ico"></span>
</div>
If I manually select a value from the drop-down then I see class="js-ctm-display" becomes as below and the page then makes an ajax call to obtain values for subsequent drop-downs.
<div class="js-ctm-display">
<span>4 Series</span>
<span class="js-ctm-ico"></span>
</div>
Because I can tell that the page is making multiple ajax calls as I move through the options I believe I need to simulate user interaction (clicks) rather than being able to just set values. However my attempts at clicking on an option in the list aren't working out at all.
I've also attempted to get a WebElement for one of the option values with the intention of sending it a click, but that throws an exception saying the WebElement is expected to be a Select rather than an Option.
Does anyone have a view on how to approach this kind of page? I'm rather new to Selenium so expect my approach isn't the best.

wait for element to be enabled protratcor

Again, this question might be easy or already asked but i am not able to find any answer to this. All i get is to check if element is clickable but that is not working for me.
Is there any simple solution to wait for an element until it becomes enabled. I have set of dropdowns in my application and other dropdown get selectable only when value is selected in previous dropdown.
I tried below code but it is not working.
var element = element(by.css('[id*="uxSeries"] select'));
browser.wait(protractor.ExpectedConditions.elementToBeClickable(element), 10000);
Error message i am getting:
Failed: stale element reference: element is not attached to the page
document
HTML for dropdown before it get enabled:
<select name="GalleryControl$uxSeries" onchange="OnChangeScript" id="uxSeries" disabled="disabled" style="width:215px;">
<option selected="selected" value="">All Series</option>
<option value="Value1">Option1</option>
<option value="Value2">Option2</option>
<option value="Value3">Option3</option>
<option value="Value4">Option4</option>
<option value="Value5">Option5</option>
</select>
After i select value in my first dropdown, disabled="disabled" property goes away from dropdown.
Can i wait for browser until it is enabled or Disabled property is no more?
Failed: stale element reference: element is not attached to the page document
This exception occurred due to changing of element, as you are saying I have set of dropdowns in my application and other dropdown get selectable only when value is selected in previous dropdown means when it's getting enable, it changes their position or some attribute value may be disabled attribute become change that's why you're in trouble.
You should try first to wait until this element become stale from disabled state using protractor.ExpectedConditions.stalenessOf(), then find again this element when it is in enable state as below :-
var EC = protractor.ExpectedConditions;
#find disabled select element first
var disabledSelect = element(by.css("select#uxSeries[disabled='disabled']"));
#now wait until this becomes stale from disabled state
browser.wait(EC.stalenessOf(disabledSelect), 10000);
#now find enabled select element
var enabledSelect = element(by.css("select#uxSeries"));
#now do your further steps

How to select the Dropdown option based on class selection using webdriver

When the user expands the menu and chooses and option, how can I get the selected option with Selenium?
<select class="brand-select select-glossy user-success" name="">
<option value="All Makes">All Makes</option>
<option value="Ford">Ford</option>
<option value="Lincoln">Lincoln</option>
<option value="Mercury">Mercury</option>
</select>
Find the select element and make a Select object our of it (need to import org.openqa.selenium.support.ui.Select)
Use the API therein to get the first selected option:
WebElement selectedOption = new Select(driver.findElement(By.className("brand-select"))).getFirstSelectedOption();