Can't select option in Select with Selenium - java

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

Related

Selenium to click this button

I am trying to create a test to select this button
<button type="submit" class="btn btn--stretch btn--primary btn--green t-auth__login--btn">
Create my account</button>
the code that I used is this, but it doesn't work
driver.findElement(By.className("btn btn--stretch btn--primary btn--green t-auth__login--btn")).click();
Can somebody tell me the correct way to code this test?
If it is any help, the webpage in question is https://www.dunnesstores.com/customer/login
To click() on the element you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn")).click();
xpath:
driver.findElement(By.xpath("//button[#class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]")).click();
Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn--stretch.btn--primary.btn--green.t-auth__login--btn"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--stretch btn--primary btn--green t-auth__login--btn' and contains(., 'Create my account')]"))).click();

How to click on web element to display datepicker?

I can not locate element for this box to click on to get the date picker to popup
Any help would be appreciated
when I inspect element this is what I see...Xpath does not work either
<input ng-disabled="viewOnly" class="formInput effectiveDates ng-pristine ng-isolate-scope hasDatepicker ng-empty ng-invalid ng-invalid-required ng-not-modified ng-touched" autocomplete="off" name="effectiveStartDate" ng-required="true" placeholder="Select/Enter date(mm/dd/yyyy)" type="search" ng-model="authObj.authStartDate" value="" ng-keydown="preventUserEnterDateInfo($event)" datepicker="" minimumdate="02/27/2020" ng-change="effectiveDateChange(authObj.authStartDate)" ng-class="(authListForm.$submitted && authListForm.effectiveStartDate.$invalid)?'reqd':''" id="dp1582663746532" required="required">
The desired element is an Angular element so to locate/click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.formInput.effectiveDates.ng-pristine.ng-isolate-scope.hasDatepicker.ng-empty.ng-invalid.ng-invalid-required.ng-not-modified.ng-touched[name='effectiveStartDate'][placeholder*='Enter date']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='formInput effectiveDates ng-pristine ng-isolate-scope hasDatepicker ng-empty ng-invalid ng-invalid-required ng-not-modified ng-touched' and #name='effectiveStartDate'][contains(#placeholder, 'Enter date')]"))).click();
You can try click using JavascriptExecutor.
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
or you can try click using coordinates:
Actions builder = new Actions(driver);
builder.moveToElement(element, 10, 25).click().build().perform();
I noticed that the 'type'-tag is set to Search. If you want a browser to recognize it as a Date-Input, change it to type="date". The browser itself will invoke its default datepicker.

How to locate the element as shown in the image

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

Unable to locate dynamic button on dialog box

I try to locate a dynamic button element after click search but it cannot locate it. The Create Account CID sometimes is not clickable and sometime can click.
<div class="pzbtn-rgt" data-click="...">
<div class="pzbtn-mid" data-click="....">
<img src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" alt="" class="pzbtn-i">
Create Individual CID
<img alt="" src="https://10.204.137.86:5111/prweb/PRWebLDAP3/SstGGrXNazw%5B*/webwb/zblankimage.gif" class="pzbtn-i">
The create individual CID is the one need clicks on.
I using absolute xpath but it still fails. I had tried with many ways. Please help. Thanks.
WebDriverWait waitCIDBtn = new WebDriverWait(driver, 10);
waitCIDBtn.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div")));
WebElement createCID = driver.findElement(By.xpath("/html/body/div[3]/div[4]/div/div/form/div[1]/div/div/div/div/div[1]/table/tbody/tr/td/div[2]/table/tbody/tr/td/div/div/span/div/div[4]/div/div/div/div[4]/div/div/span/button/div/div/div/div"));
createCID.click();
As you are trying to invoke click() on the element so, instead of using visibilityOfElementLocated() you need use elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.pzbtn-mid>img.pzbtn-i[src*='zblankimage']"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='pzbtn-mid']/img[#class='pzbtn-i' and contains(#src, 'zblankimage')]"))).click();

Problem with selectors in Selenium Web Driver

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

Categories