Select value from dropdown with selenium webdriver - selenium

I am trying to select a value from a dropdown with selenium webdriver but it just opens the dropdown and immediately closes it.
Here is the code for webdriver:
driver.findElement(By.cssSelector("span.k-select ")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//li[#class='k-item'][.='Revenue per click']")).click();
And this is the html code from the browser for the dropdown:
<div class="controls">
<span class="k-widget k-dropdown k-header kendo-input span8 ddFix" unselectable="on">
<span unselectable="on" class="k-dropdown-wrap k-state-default">
<span unselectable="on" class="k-input">Select revenue type...
</span>
<span unselectable="on" class="k-select">
<span unselectable="on" class="k-icon k-i-arrow-s">select</span>
</span>
</span>
<input name="revenueType" id="revenueType" class="kendo-input span8 ddFix" data-role="dropdownlist" style="display: none;">
</span>
</div>
<div class="k-animation-container km-popup">
<div class="k-list-container k-popup k-group k-reset" id="revenueType-list" data-role="popup">
<ul unselectable="on" class="k-list k-reset" tabindex="-1" role="listbox" aria-hidden="true" id="revenueType_listbox" aria-live="off" style="overflow: auto;">
<li tabindex="-1" role="option" unselectable="on" class="k-item">Select revenue type...</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per action</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per click</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per sale</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per action + Revenue per sale</li>
<li tabindex="-1" role="option" unselectable="on" class="k-item">Revenue per mile</li>
</ul>
</div>
</div>

It's hard to debug without the context but:
Your locator is invalid, instead try using:
By.xpath("//li[text()='Revenue per click']")
Since only one item has that text, it should be sufficient. If you still want to include #class in selector, use
By.xpath("//li[#class='k-item' and text()='Revenue per click']")
Try using javascript change the unselectable to off using javascript executor.

Get the dropdown text value after perform click action
subdropdown_child = navigationDriver.findElements(By.cssSelector("select#ctl00_SPWebPartManager1_g_c873566a_540c_4681 option");
for (Element subchild : subdropdown_child)
{
dropdown_value = subchild.text();
new Select(navigationDriver.findElementByCssSelector("select#ctl00_SPWebPartManager1_g_c873566a_540c_4681_")).selectByVisibleText(dropdown_value);
}

Related

Can't click with selenium on service console option of salesforce

i'm trying to automate Salesforce Service Console, but i'm having some troubles because the multiples iframes.
One thing i really need is to click on the option "Close all primary tabs", but everything i have tried is not working.
The weird thing is that although the click is done nothing happens, if i try to do a click on chrome inspector console using javascript the tabs are closed, also if i click manually the tabs are closed, but if is made this trought selenium/katalon although the click is done nothing happens.
I have discovered that the popup html source is not under an iframe but the render of the popup overlays a iframe which comes from the external page tab.
If i trick the webpage modifying the css with the inspector moving the popup more up, were there is not iframe, then the click is closing the tabs.
Also if i delete the iframe from the html, the click is closing the tabs.
So i understand that this could be the problem, but although i have tried switch to iframe still not working.
i'am using katalon to automate but also happens with selenium .
Can you help me?
code of the popup list
<div id="ext-comp-1031"
class="x-menu x-menu-floating x-layer"
style="position: absolute; z-index: 15000; visibility: visible; left: 1086px; top: 124px;">
<a class="x-menu-focus"
href="#"
onclick="return false;"
tabindex="-1"
id="ext-gen173"/>
<ul class="x-menu-list"
id="ext-gen174"
style="height: 158px;">
<li id="x-menu-el-ext-comp-1106"
class="x-menu-list-item">
<a id="ext-comp-1106"
class="x-menu-item tab-scroller-menu-item "
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon hidden"
id="ext-gen526">
<span class="x-menu-item-text"
id="ext-gen527">
<div class="tabTitle">
<span class="tabText">External Page</span>
</div>
<img alt=""
src="/s.gif"
class="refresh-icon"
id="ext-gen538">
<div class="x-menu-item-icon burl-link third-position"
id="ext-gen539"/>
<img alt=""
src="/s.gif"
class="bookmark-icon bookmarkNoPin"
id="ext-gen540"></span>
</a>
</li>
<li id="x-menu-el-ext-comp-1107"
class="x-menu-list-item">
<a id="ext-comp-1107"
class="x-menu-item tab-scroller-menu-item x-tab-tabmenu-selected "
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon hidden"
id="ext-gen528">
<span class="x-menu-item-text"
id="ext-gen529">
<div class="tabTitle">
<span class="tabText">External Page</span>
</div>
<img alt=""
src="/s.gif"
class="refresh-icon"
id="ext-gen541">
<div class="x-menu-item-icon burl-link third-position"
id="ext-gen542"/>
<img alt=""
src="/s.gif"
class="bookmark-icon bookmarkNoPin"
id="ext-gen543"></span>
</a>
</li>
<li id="x-menu-el-ext-comp-1108"
class="x-menu-list-item">
<a id="ext-comp-1108"
class="x-menu-item x-tab-tabmenu-menuItem"
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon "
id="ext-gen530">
<span class="x-menu-item-text"
id="ext-gen531">Close all primary tabs</span>
</a>
</li>
<li id="x-menu-el-ext-comp-1109"
class="x-menu-list-item">
<a id="ext-comp-1109"
class="x-menu-item x-tab-tabmenu-menuItem"
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon "
id="ext-gen532">
<span class="x-menu-item-text"
id="ext-gen533">Refresh all primary tabs</span>
</a>
</li>
<li id="x-menu-el-ext-comp-1110"
class="x-menu-list-item">
<a id="ext-comp-1110"
class="x-menu-item x-tab-tabmenu-menuItem"
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon "
id="ext-gen534">
<span class="x-menu-item-text"
id="ext-gen535">Reopen last-closed tab</span>
</a>
</li>
<li id="x-menu-el-ext-comp-1111"
class="x-menu-list-item">
<a id="ext-comp-1111"
class="x-menu-item x-tab-tabmenu-menuItem"
hidefocus="true"
unselectable="on"
href="#">
<img alt=""
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
class="x-menu-item-icon "
id="ext-gen536">
<span class="x-menu-item-text"
id="ext-gen537">Show keyboard shortcuts (SHIFT+K)</span>
</a>
</li>
</ul>
</div>
xpath i'am selecting
//*/text()[normalize-space(.)='Close all primary tabs']/parent::*

Dropdown with classname not working with select function Selenium

I have a path:
<div class="jss601 jss602 jss581" aria-pressed="false" tabindex="0" role="button" aria-haspopup="true" xpath="1"><span></span></div>
but I could not write the XPath or CSS for clicking the element. Could you please help me?
This is a dropdown list called as "Tip" how can I manage the selection because it is not a select so I could not use the select function:
HTML:
<ul class="MuiList-root-139 MuiList-padding-140" role="listbox" xpath="1">
<li tabindex="-1" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296" role="option" data-value="">HiƧbiri
<span class="MuiTouchRipple-root-64"></span>
</li>
<li tabindex="-1" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296" role="option" data-value="RTU">RTU
<span class="MuiTouchRipple-root-64"></span>
</li>
<li tabindex="0" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296 MuiMenuItem-selected-297" role="option" data-value="Substation Control System">Substation Control System
<span class="MuiTouchRipple-root-64"></span>
</li>
<li tabindex="-1" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296" role="option" data-value="Control Center">Control Center
<span class="MuiTouchRipple-root-64"></span>
</li>
<li tabindex="-1" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296" role="option" data-value="IED">IED
<span class="MuiTouchRipple-root-64"></span>
</li>
</ul>
<li tabindex="-1" class="MuiButtonBase-root-55 MuiListItem-root-143 MuiListItem-default-146 MuiListItem-gutters-150 MuiListItem-button-151 MuiMenuItem-root-296" role="option" data-value="RTU" xpath="1">RTU
<span class="MuiTouchRipple-root-64"></span>
</li>
Here you can find a dropdown example and how find a element in this dropdown.
Selenium - Python - drop-down menu option value

Selecting an element from a dropdown using selenium

I have a DROPDOWN LIST and it doesn't have the select attribute:
<ul id="Registration_RegistrationStateCode_listbox" class="k-list k-reset" unselectable="on" style="overflow: auto; height: 200px;" tabindex="-1" role="listbox" aria-hidden="false" aria-live="off">
<li id="Registration_RegistrationStateCode_option_selected" class="k-item k-state-selected k-state-focused" unselectable="on" role="option" tabindex="-1" aria-selected="true">-- Select State --</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">Alabama</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">Alaska</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">Arizona</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">Arkansas</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">California</li>
<li class="k-item" unselectable="on" role="option" tabindex="-1">Colorado</li>
How to select a particular state?
I am unable to select any state as it does not have select tag.
If you use the Page Object Model, I would create a function in this specific Page Object such as the following:
public void SelectState(string state)
{
_driver.FindElement(By.Id("Registration_RegistrationStateCode_listbox")).Click();
_driver.FindElement(By.XPath("//li[text()='" + state + "']")).Click();
}
Otherwise you can just use the individual lines and pass in a state:
_driver.FindElement(By.Id("Registration_RegistrationStateCode_listbox")).Click();
_driver.FindElement(By.XPath("//li[text()='Minnesota']")).Click();

Selecting dropdown list box invisible items

How to select a invisible dropdown list drop from following code?
<div id="form:munit_panel" class="ui-selectonemenu-panel ui-widget ui-widget-content ui-corner-all ui-helper-hidden ui-shadow" style="width: 125px; display: block; top: 177px; left: 798.267px; z-index: 1006;">
<div class="ui-selectonemenu-items-wrapper" style="height:200px">
<ul id="form:munit_items" class="ui-selectonemenu-items ui-selectonemenu-list ui-widget-content ui-widget ui-corner-all ui-helper-reset" role="listbox" aria-activedescendant="form:munit_0">
<li id="form:munit_0" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all ui-state-highlight" role="option" tabindex="-1" data-label="No Unit">No Unit</li>
<li id="form:munit_1" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Bag">Bag</li>
<li id="form:munit_2" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Load">Load</li>
<li id="form:munit_3" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Litre">Litre</li>
<li id="form:munit_4" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Each">Each</li>
<li id="form:munit_5" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Unit">Unit</li>
<li id="form:munit_6" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Nos">Nos</li>
<li id="form:munit_7" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="R.feet">R.feet</li>
<li id="form:munit_8" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Sq.ft">Sq.ft</li>
<li id="form:munit_9" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Kgs">Kgs</li>
<li id="form:munit_10" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="feet">feet</li>
<li id="form:munit_11" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="ml">ml</li>
<li id="form:munit_12" class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" role="option" tabindex="-1" data-label="Mts">Mts</li>
</ul>
</div>
</div>
I tried this Selenium code to select the dropdown last option:
public void selectOneMenu(String idPrefix, String value) {
if (StringUtil.isNotBlank(value)) {
driver.findElement(By.id(idPrefix + "_label")).click();
driver.findElement(
By.xpath("//div[#id='" + idPrefix
+ "_panel']/div/ul/li[text()='" + value + "']"))
.click();
}
}
You should try using WebDriverWait to wait until dropdown options visible and enable to click after clicking on dropdown as below :-
public void selectOptionsByIdFromDropdown(String dropdownId, String optionsId) {
WebDriverWait wait = new WebDriverWait(driver, 10)
//First click on dropdown menu
WebElement dropdown = wait.until(ExpectedConditions.elementToBeClickable(By.id(dropdownId)));
dropdown.click();
//Now wait until nested option to be present by id
wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(dropdown, By.id(optionsId))).click();
}
Usage :-
I need to select the last option in the dropdown list box .when I click the drop down box 12th item is not visible
selectOptionsByIdFromDropdown("form:munit_panel", "form:munit_12");

How to find a node with xpath having namespace in xml

I am trying to find div node with Xpath for selenium. here div having categories/Accounts & Contacts as value of ext:tree-node-id need to be selected, but as ext:tree-node-id is a namespace my below xpath is not working.
//li[#class='x-tree-node']//div[contains(.,ext:tree-node-id='categories\/Accounts & Contacts')]
//li[#class='x-tree-node']//div[#ext:tree-node-id='categories\/Accounts & Contacts']
<li class="x-tree-node">
<div class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" ext:tree-node-id="categories\/Accounts & Contacts">
<span class="x-tree-node-indent"/>
<img class="x-tree-ec-icon x-tree-elbow-plus" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/>
<img class="x-tree-node-icon" unselectable="on" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/>
<a class="x-tree-node-anchor" tabindex="1" href="" hidefocus="on">
</div>
<ul class="x-tree-node-ct" style="display: none;">
</li>
<li class="x-tree-node">
<div class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" ext:tree-node-id="categories\/Opportunities">
<span class="x-tree-node-indent"/>
<img class="x-tree-ec-icon x-tree-elbow-plus" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/>
<img class="x-tree-node-icon" unselectable="on" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" alt=""/>
<a class="x-tree-node-anchor" tabindex="1" href="" hidefocus="on">
</div>
<ul class="x-tree-node-ct" style="display: none;">
</li>
A colon in the attribute will not allow the xpath works. You need to improve a little to it works as you wish, like:
//li[#class='x-tree-node']//div[#*[contains(local-name(), tree-node-id)] = 'categories\\/Accounts & Contacts']
The "local-name" will select the name after the colon named "tree-node-id".