Element is not currently visible so may not be interacted with - selenium

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

Related

Selenium (Java) Select tag selection error

<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

Exception in getting xpath to a web element: org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"

There is a dropdown with 9 options in it, all of anchor (a) type. Selecting an option from the list, changes its class & other attributes dynamically.
This is how the html code looks like:
<select data_old_value="Email" id="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843" name="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843" onchange="RuleWizardIns.RuleActionChanged($(this))" style="width: 103px !important; height: auto !important; display: none;" aria-disabled="false"><option value="Email" selected="selected">Send Email</option><option value="TaskCreate">Create Task</option><option value="AddActivity">Add Activity</option><option value="AddToList">Add To List</option><option value="LeadUpdate">Update Lead Fields</option><option value="TagLead">Tag Lead</option><option value="StarLead">Star Lead</option><option value="UnStarLead">Unstar Lead</option><option value="Webhook">Call Webhook</option></select>
<option value="Email" selected="selected">Send Email</option>
<option value="TaskCreate">Create Task</option>
<option value="AddActivity">Add Activity</option>
<option value="AddToList">Add To List</option>
<option value="LeadUpdate">Update Lead Fields</option>
<option value="TagLead">Tag Lead</option>
<option value="StarLead">Star Lead</option>
<option value="UnStarLead">Unstar Lead</option>
\<option value="Webhook">Call Webhook</option>
</select>
<span>
<a class="ui-selectmenu ui-widget ui-state-default ui-corner-all ui-selectmenu-dropdown" id="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843-button" role="button" href="#nogo" tabindex="0" aria-haspopup="true" aria-owns="ActionType-db5ff710-2a01-11e8-9c5f-22000aa79843-menu" style="width: 103px;" aria-disabled="false"><span class="ui-selectmenu-status">Send Email</span><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span></a>
<span class="ui-selectmenu-status">Send Email</span>
<span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span>
</a>
</span>
I tried to get the control on Update Lead Fields option by using below code, but unfortunately its not working.
Select ActionType = new Select(driver.findElement(By.xpath("//select[#data_old_value='Email']/following-sibling::span/a")));
ActionType.selectByVisibleText("Update Lead Fields");
I'm getting below error:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a"
And there is no ui for select attribute as you can see from below image:
https://d24cdstip7q8pz.cloudfront.net/t/t20170511180558/content/common/images/Untitled.png
A Select object can only be created by getting the WebElement with tag is "select" that's what the errormessage is about. I created an C# example which I tested giving expected result.
Driver.Url = ("https://developer.mozilla.org/en-
US/docs/Web/HTML/Element/select");
var valuesSelectElement = Driver.FindElement(By.Name("select"));
var selectElement = new SelectElement(valuesSelectElement);
// To select an option by value
selectElement.SelectByValue("value1");
// To select an option by text
selectElement.SelectByText("Value 2");
var options = selectElement.Options;
//To click on one of the options
foreach (var option in options)
{
if (option.Text.Equals("Value 3"))
{
option.Click();
}
}

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.

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

org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "td"

hii all i am very new to automation
my problem is i am getting an error
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "td"
when i am trying to select an option from dropdown list
this is my sourch code
public void CompanyFilters_Fn() {
// TODO Auto-generated method stub
_driver.findElement(By.xpath(".//*[#id='company._idCell']")).click();
Select CompanyOptions = new Select(_driver.findElement(By.xpath(".//*[#id='company._idCell']")));
CompanyOptions.deselectAll();
CompanyOptions.selectByIndex(5150);
CompanyOptions.selectByIndex(5003);
}
this is my html code
<td id="company._idCell" class="filterValue filterActive filterChanged firepath-matching-node" onclick="showInputElement('pre.myProjectsForm.company._id', this, null, 'auto'); return false;" title="Select to filter by Company">Company: [No Company]</td>
these are the options
<div id="divSelect" class="inputElement" style="left: -1000px; top: 212px; width: auto; height: 20px;">
<select id="inputSelect" size="5" style="width: auto; visibility: visible;" multiple="" disabled="">
<option value="-1">Any</option>
<option value="0">[No Company]</option>
<option value="5150">123</option>
<option value="5203">AutoCarLoanCompany</option>
<option value="5205">Company_rahul</option>
<option value="5202">CreditCardCompany</option>
<option value="5003">IBM</option>
<option value="5204">June</option>
<option value="5300">other companyyyyy</option>
<option value="5001">qa</option>
<option value="5101">Rahul</option>
</select>
</div>
Plzzz help me to get out of this...
The error message is very descriptive, you've given it a td when it expects a select (rightly so).
So here:
Select CompanyOptions = new Select(_driver.findElement(By.xpath(".//*[#id='company._idCell']")));
You need to change this to give it the select:
Select CompanyOptions = new Select(_driver.findElement(By.id("inputSelect")));