Selenium (Java) Select tag selection error - selenium

<div class="pull-right">
<button type="button" class="btn-excel-select-prd btn btn-sm btn-base">엑셀다운로드</button>
<select id="selectLength" cdgrp="PAGE_LEN" class="form-control input-sm" style="">
<option value="10" title="10개씩 보기">10개씩 보기</option>
<option value="20" title="20개씩 보기">20개씩 보기</option>
<option value="50" title="50개씩 보기" selected="">50개씩 보기</option>
<option value="100" title="100개씩 보기">100개씩 보기</option>
</select>
</div>
enter image description here
I want to choose the 4th option.
However, if I write the code as below, an error "element not interactable: Element is not currently visible and may not be manipulated" appears. How can I solve it?
WebElement selectElement = driver.findElement(By.id("selectLength"));
Select select = new Select(selectElement);
select.selectByIndex(3);
enter image description here

Related

Looking at the "other" html properties on an element in Selenium

I'm using Selenium to automate some very basic data entry tasks. Sadly I don't have any influence over the site on which the form is hosted.
I have a number of fields on the form that seem to only differ in fields that I don't know how to make Selenium look at. I find all of the fields on the page and can enter text into them as below:
Dim i As Integer = 0
Dim elements = browser.FindElementsByClassName("form-control")
For Each elemnt In elements
Try
elemnt.SendKeys(i)
Catch
End Try
i += 1
Next
Unfortunately this isn't going to be a long-term solution though, as they might change the order of boxes or similar without notifying me. Is there any way to view the whole of the <input ...> tag?
A selection of the inputs are below:
<input class="form-control ng-valid ng-dirty ng-valid-parse ng-touched" data-ng-model="sQuoteName" kdfapp="Quoting" kdfpage="createDeal" kdfid="dealName" style="">
<input kdfapp="quote" kdfpage="quote" kdfid="amEmail" type="text" class="form-control ng-valid ng-dirty ng-valid-parse ng-touched" id="ciscoEmail" data-ng-model="data.suggestAm" placeholder="Email" style="">
<select name="sQuoteType" data-ng-model="sQuoteType" data-ng-change="changeQuoteType();" kdfapp="Quoting" kdfpage="createDeal" kdfid="selectedBuyingBehalf_xxx" class="ng-pristine ng-untouched ng-valid">
<option value="" class="ng-binding">Select</option>
<option value="2" class="ng-binding">Prime Contractor/Systems Integrator</option>
<option data-ng-show="stiEligible" value="1" class="ng-binding ng-hide">Solution Technology Integrator</option>
<option data-ng-show="partner.partnerBuyMthod == 'DIRECT'" value="3" class="ng-binding" style="">IT Service Provider (Outsourcing Deal)</option>
</select>
The most useful descriptor seems to be kdfid=
Please use GetAttribute method to get the Kdfid.
Sample code:
element.GetAttribute("kdfid")

Unable to locate element in Selenium using correct xpath

My AUT have different tabs to test and validate various WebElements, having almost identical properties, present on these tabs.
I need to validate a dropdown searchbox and validate the options listed in the dropdown.
My script can not locate this dropdown searh box, while the xpath work perfectly fine in firebug and can identify the search box.
The error displayed is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn-group']/select[#id='selectBS']"}
My script is as below:
if(Driver.findElement(By.xpath("//div[#class='btn-group']/select[#id='selectBS']")).isDisplayed());
{
System.out.println(" drop down box is displayed on the View Bill screen");
}
The HTML is as below:
<div class="col-md-5 col-sm-6 col-xs-12">
<div class="row panel panel-default panel-heading">
<div class="col-sm-4 col-xs-12">
<div class="dropdown">
<div class="btn-group">
<select id="selectBS" class="ng-pristine ng-valid ng-touched" ng-change="getBasicSearchSelectedOption()" ng-model="basicSearchSelectedOption" ng-options="option.text for option in basicSearchoptions track by option.index">
<option value="0" selected="selected" label="Search For..">Search For..</option>
<option value="1" label="Invoice Group Number">Invoice Group Number</option>
<option value="2" label="Account Number">Account Number</option>
<option value="3" label="Account Name">Account Name</option>
<option value="4" label="Type">Type</option>
</select>
</div>
</div>
</div>
Please let me know what could be wrong, and why Selenium is not able to locate the element using xpath, while its working fine in xpath.
Also, please let me know if my xpath is not correct.
Check if is present, after that, do some action so the options are visible and check that an option is visible. If these work then it may be an issue checking isDisplayed for elements with type select.

Can't click the check box in the sign up page

I am writing selenium script for Autotrader.com signup page. I am not able to click on 'I Agree' Check box. This is what I have written:
WebElement cboxlink = driver.findElement(By.id("j_id_29-j_id_2h-"
+ "myatcRegisterForm-j_id_3n-acceptTerms"));
cboxlink.click();
The HTML is:
<div id="j_id_3n" class="atcui atcui-form-row atcui-clearfix atcui-small terms" style="">
<input id="j_id_29-j_id_2h-myatcRegisterForm-j_id_3n-acceptTerms" class="checkbox" type="checkbox" value="true" name="j_id_29-j_id_2h-myatcRegisterForm-j_id_3n-acceptTerms">
<label for="j_id_29-j_id_2h-myatcRegisterForm-j_id_3n-acceptTerms">
I accept terms of the
<a target="_blank" href="/privacy.jsp">Privacy Statement</a>
and <a target="_blank" href="/legal/visitor-agreement.xhtml">Visitor Agreement</a>.
</label>

Element is not currently visible so may not be interacted with

I need to select the last item in the list. My code below shows the message that the element is not currently visible. How can fix this?
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#s2id_autogen1 > a.select2-choice > span")));
WebElement element = driver.findElement(By.xpath("//select[#name='siteKey']"));
element.click();
Select select = new Select(element);
select.selectByIndex(select.getOptions().size()-1);
HTML:
<div id="overview_form">
<ol>
<li>
<span>1.</span>
<label class="input_label" for="sites">Sites*</label>
<div class="select2-container select2-dropdown-open select2-container-active" id="s2id_autogen1" style="width: 500px;">
<a tabindex="-1" class="select2-choice" onclick="return false;" href="javascript:void(0)">
<span>gn</span><abbr style="display:none;" class="select2-search-choice-close"></abbr><div><b></b></div></a>
<input type="text" class="select2-focusser select2-offscreen" disabled="disabled"></div>
<select style="width:500px; *width:400px;" size="1" name="siteKey" class="select2-offscreen" tabindex="-1">
<option value="30706">gn</option>
<option value="30813">www.walmart.com_20150212151258</option>
<option value="30815">www.walmart.com_20150212151452</option>
<option value="30817">www.walmart.com_20150212152338</option>
<option value="30819">www.walmart.com_20150212152521</option>
<option value="30820">www.walmart.com_20150212152849</option>
<option value="30822">www.walmart.com_20150212152939</option>
<option value="30824">www.walmart.com_20150212153438</option>
<option value="30761">www.yup.com</option>
</select>
</li>
</ol>
Screenshot of the List. This list has a search field, where user can enter prefixes to narrow-down the search.
Try this. Please make sure to expand the list as needed. The selector is written such a way so that it will always fine the last option of the select tag named siteKey
//this selector find the last child o
By cssSelector = By.cssSelector("[name='siteKey']>option:last-child");
//explicit wait to make sure the element present
WebElement element = new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(cssSelector));
element.click();
System.out.println(element.getText());
Print
www.yup.com

How to ALWAYS select the last item in the list

I need to select the last item in a DYNAMIC list. The following is my script. Thanks!
WebElement selectElement = driver.findElement(By.name("siteKey"));
Select select = new Select(selectElement);
//select.selectByVisibleText("last item");
//select.selectByIndex(0);
//select.selectByValue("value");
Please see the page HTML below. Let me know if I can provide you with any other info. Thanks!
<div id="overview_form">
<ol>
<li>
<span>1.</span>
<label class="input_label" for="sites">Sites*</label>
<div class="select2-container select2-container-active select2-dropdown- open" id="s2id_autogen1" style="width: 500px;">
<a tabindex="-1" class="select2-choice" onclick="return false;" href="javascript:void(0)">
<span>www.roger.com_20150210075155</span>
<abbr style="display:none;" class="select2-search-choice-close"></abbr>
<div><b></b></div></a>
<input type="text" class="select2-focusser select2-offscreen" disabled="disabled">
</div>
<select style=" size="1" name="siteKey" class="select2-offscreen" tabindex="-1">
<option value="30518">www.roger.com_20150209191817</option>
<option value="30520">www.roger.com_20150209192123</option>
<option value="30522">www.roger.com_20150209192351</option>
<option value="30524">www.roger.com_20150209192910</option>
<option value="30528">www.roger.com_20150209193425</option>
<option value="30529">www.roger.com_20150209193801</option>
<option value="30531">www.roger.com_20150209194009</option>
<option value="30546">www.roger.com_20150210074133</option>
<option value="30548">www.roger.com_20150210074359</option>
<option value="30550">www.roger.com_20150210075155</option></select>
</li>
</ol>
</div>
How about something like:
WebElement selectElement = driver.findElement(By.name("siteKey"));
Select select = new Select(selectElement);
select.selectByIndex(select.getOptions().size()-1);
You can use getOptions(), it will return a list then get the size of the list
Select select = new Select(driver.findElement(By.id("siteKey")));
List<WebElement> l = select.getOptions();
int numElements = l.size();
select.selectByIndex(munElements);
Hope this helps
I tried below way, and it's working.. Maybe this will help u .
WebDriver driver = new FirefoxDriver();
driver.get("http://www.echoecho.com/htmlforms11.htm");
Thread.sleep(100l);
driver.findElement(By.xpath("//select[#name='dropdownmenu']")).click();
Thread.sleep(100l);
WebElement element = driver.findElement(By.xpath("//select[#name='dropdownmenu']"));
List<WebElement> elements = element.findElements(By.tagName("option"));
System.out.println(elements.get(elements.size() - 1).getText());