I have a problem with some selectors to click in my automatic test in Selenium. My test doesn't see any of the selectors that I used. There is my div which I working with:
<select name="people" id="demo-htmlselect" onchange="selectConfigOption(this.value)" >
<option value="">Choose a selection...</option>
<option value="429" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/tobacco.png"
data-description=""> Tobacco 27 mg/ml </option>
<option value="432" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/menthol.png"
data-description=""> Menthol 27 mg/ml </option>
<option value="435" data-imagesrc="https://logicvapes-us-dev.jtiweb.co.uk/media/catalog/swatches/4/45x45/media/cherry.png"
data-description=""> Cherry 27 mg/ml </option>
</select>
And my ideas (which aren't working):
wd = new FirefoxDriver();
WebElement span = wd.executeScript("return document.getElementById('dd-select');");
wd.findElement(span).click();
//wd.findElement(By.xpath("//div[#class='dd-select']/span[#class='class='dd-pointer.dd-pointer-down'']")).click();
//wd.findElement(By.xpath("value=//*[#id='432']"));
//WebElement register = wd.findElement(By.name('people'));
//wd.findElement(By.partialLinkText("Choose a selection...")).click();
//wd.findElementById("select=//*[#id='429']").click();
Thanks for every advice!
As per the html you have shared the the element is a <select> tag so you have to use the Select class an additionally induce WebDriverWait to choose an option and you can use the following solution:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[#id='demo-htmlselect' and #name='people']")));
Select mySelect = new Select(elem);
//selecting the second item by value 429
mySelect.selectByValue("429");
This drop down is made using select and option tags, so you can use select class from selenium.
Select drop_down = new Select(driver.findElement(By.id("demo-htmlselect")));
drop_down.selectByVisibleText("Menthol");
Or
drop_down.selectByValue("432");
This is not the correct way to select an option from a select box.
See Select
You have to use something like this:
WebElement element = <WEB DRIVER INSTANCE>.findElement(By.xpath(SELECTOR FOR THE SELECT ELEMENT));
Select select = new Select(element);
select.selectByValue(<VALUE OF THE SELECTED ITEM>);
Related
I am having an issue when I try to select an option in a <select> in Selenium.
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
This simply gives me ElementClickIntercepted.
Trying to click on it also gives me ElementClickIntercepted.
Trying to click on it with JS gives me a NullPointerException.
I can very easily select it in Firefox with the element selector so nothing is on top of the select that prevents me from clicking it.
What is intercepting the click? usually when it's because an element is overlaying another, it will tell me in the test results, but here it doesn't.
<div class="pull-left">
<select name="nb" class="form-control">
<option value="10">10</option><option value="20">20</option><option value="50">50</option><option value="100000">All</option>
</select>
</div>
Select xPath:
//select[#name="nb"]
And it is the only select on the page.
Try this:
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//select[#name='nb']")));
Select select = new Select(element);
actions.moveToElement(element);
select.selectByValue("100000");
As the element is a <select> element ideally you need to use Select class. To invoke click() on the option with value as 1000 you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.form-control[name='nb']")))).selectByValue("100000");
xpath:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='form-control' and #name='nb']")))).selectByValue("100000");
The drop down is already selected by value 1 and its not locating through the id or name. How to find the xpath to locate as for the image?
HTML:
<select name="ctl05$ddlSelectBox" onchange="javascript:setTimeout('__doPostBack(\'ctl05$ddlSelectBox\',\'\')', 0)" id="ctl05_ddlSelectBox" style="height:21px;width:180px;">
<option selected="selected" value="-1">--Select Box--</option>
<option value="2742">Box_H026_01</option>
Image:
The desired element is a JavaScript enabled <select> node. So locate the element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select[name$='ddlSelectBox'][onchange*='ddlSelectBox']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[contains(#name, 'ddlSelectBox') and contains(#onchange, 'ddlSelectBox')]")));
I tried to use the objname.deselectByVisibileText() on multiple dropdowns (select/span) and I get the following error
Exception in thread "main" java.lang.UnsupportedOperationException: You may only deselect options of a multi-select.
How can I clear those respective fields? My method atm looks like this:
public void deselect(String s, String t)
{
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
Select select = new Select(element);
select.deselectByVisibleText(t);
}
Obviously, I need a solution without deselect, as none of them work (byValue, byIndex, etc.) due to the same error as above.
Typically the first option is the default. You can just select it.
select.selectByIndex(0);
If you have a select element that looks like this:
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Running the code:
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex];
would return the selected option.
Now you got to know the selected index. So, use javascript executor to unselect it
I am starting in the world of UI automation with WebDriver and Java. I am getting a problem when I try to select a element of a combo box. This is the code:
WebDriver driver = new FirefoxDriver();
driver.get("http://intersite.com/");
new Select(driver.findElement(By.xpath(".//*[#id='term']"))); //Exception happens in this line org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#id='term']"}
And this is the code in the web site (I use Firepath to know the Xpath):
<select name="term" onchange="getTipDoc('');" id="term" class="termination"><option value="">-- Select an Option --</option>
<option value="OPT1">Option 1</option>
<option value="OPT2">Option2</option>
</select>
I see in the tag select, the ID attribute is correct but the exception always happens. I tried with othe method for locate the element like "By.id" but doesn't work too. What can i do?
Regards!
Couple of possible reasons can happen in case like this
Element you are looking for is inside an iframe. Use driver.switchTo().frame(driver.findElement(somethting));
Element look up is faster than the load time. In this case use explicit wait. WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("term"))); See this
There are duplicate Ids. try using select[id='term'][class='termination'] as cssSelector
And, of course use By.id() since the id is available.
you need to wait to get the page load before trying to get its element this code will help you to do that
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='term']")));
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");