I need help. I have been trying to automate selecting an item from a drop down menu. I have try many ways but I cannot get it to work. I using Selenium 2.39 firefox. The sample application is on https://www.salesforce.com/form/signup/freetrial-sales.jsp?d=70130000000EqoP&internal=true.
Any help will be appreciated. Thanks
The Xpath is: //*[#id='form-container']/ul/li[14]/div/div[2]/a/span[1]
Surrounded by :
<div id="form-container" class="clearfix wide-fields style-placeholder">
<div id="globalErrorMessage" style="display:none"> Please fill out the highlighted fields below </div>
<ul class="clearfix vertical form-ul">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-hidden">
<li class=" type-text">
<li class=" type-text">
<li class=" type-text">
<li class=" type-text">
<li class=" type-text">
<li class=" type-text">
<li class=" type-select">
<div class="control-container error">
<div class="label">
<div class="field">
<select id="CompanyEmployees" class="selectBox" name="CompanyEmployees" style="display: none;">
<a class="selectBox selectBox-dropdown" style="width: 296px; display: inline-block; -moz-user-select: none; "title="" tabindex="0">
<span class="selectBox-label" style="width: 262px;">Employees</span>
<span class="selectBox-arrow" />
</a>
<span class="form-field-error show-form-field-error">Select the number of employees</span>
</div>
<div class="info">
</div>
</li>
<li class=" type-hidden">
<li class=" type-hidden">
I have already tried using Select class but does not work.
Select select = new Select(driver.findElement(By.name("CompanyEmployees")));
select.selectByValue("250");
I get the following error:
....
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with
...
There are 2 points for you to be noted:
It's not a dropdown control as such. You might have to go through some basic HTML sources on how to achieve a DropDown scenario without using the actual DropDown control.
Escaping sequence (notice inverted commas inside the XPath expression)
Below is C# code. I'm sure you can figure out the code in the language of your choice.
Driver.FindElement(By.XPath(#"(//*[#id=""form-container""])/ul/li[14]/div/div[2]/a/span[1]")).Click(); //Click on the DropDown
Driver.FindElement(By.XPath(#"/html/body/ul[1]/li[4]/a")).Click(); //selects "21 - 100 Employees"
Please try the following code for "employees" selection:
public void selectEmployees(){
WebDriver driver = new FirefoxDriver();
WebDriverWait driverWait = new WebDriverWait(driver, 30);
driver.get("https://www.salesforce.com/form/signup/freetrial-sales.jsp?d=70130000000EqoP&internal=true");
// Wait for visibility of Employees combobox and click on it to open drop-down list
// Combo-box is selected by css locator which searches for ".selectBox" element that are preceded by "#CompanyEmployees" element
WebElement emplyeesCombobox = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#CompanyEmployees ~ .selectBox")));
emplyeesCombobox.click();
//Wait for visibility of required drop-down list item ("6 - 20 employees" in this example) and select it by clicking
// Drop-down list item is selected by XPath locator which searches for "a" element with "6 - 20 employees" text
WebElement itemToSelect = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='6 - 20 employees']")));
itemToSelect.click();
}
Hope it will help.
Related
I have a drop down with Small Medium and Large (code below).
I want to capture some text for each line - e.g. Small Tom; Medium Dick; Large Harry.
Supplementary question - can I get more than one name per size - e.g. Small Quantity 3 ;Tom, Dick, Harry
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick" /> <input type="hidden" name="hosted_button_id" value="B98VUHC4Y2HSU" />
<table>
<tr>
<td><input type="hidden" name="on0" value="Size" />Size
</td>
</tr>
<tr>
<td><select name="os0">
<option value="Small (S)">Small (S) </option>
<option value="Medium (M)">Medium (M) </option>
<option value="Large (L)">Large (L) </option>
</select>
</td>
</tr>
</table><input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_cart_LG.gif" border="0"
name="submit" alt="PayPal – The safer, easier way to pay online!" />
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif"
width="1" height="1" />
</form>
No you cannot do that. Check out some information referenced here in another thread. You could try using some sort of bootstrap version of it. It isn't possible with just HTML.
As for that extra question, would you want to? You would have to parse through the return string to separate the values.
Here is a Bootstrap version of it from that thread.
HTML:
<ul class="nav" role="navigation">
<li class="dropdown"> --Select Country--<b class="caret"></b>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop2">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">USA</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">UK</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Russia</a>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Others <input type="text" id="other"/></a>
</li>
</ul>
</li>
</ul>
JQuery:
$('.dropdown-menu input').click(function (e) {
e.stopPropagation();
});
$('.dropdown-menu li').click(function(){
$('.dropdown-toggle b').remove().appendTo($('.dropdown-toggle').text($(this).text()));
});
Bootstrap JS Example
I accept that what I asked for is not possible or at least not in my competence.
I have realised that what I really want is different The more I thought about it the easier it got.
But for any future enquirer
I need to add one line in the cart for each unique combination of two variables OS0 (Size) and OS1 (Name) and Paypal does that anyway. Only if the pair are duplicated does it bump up the quantity on the existing line.
I call this one closed
I want to select the text in the last li with xpath, i can use Css Selector too.
here the value is "3"
<div class="pg">
<input type="hidden" value="1" name="PaginationForm.CurrentPage">
<input id="PaginationForm_TotalPage" type="hidden" value="41" name="PaginationForm.TotalPage">
<span class="pgPrev">‹</span>
<ul>
<li class="">
<span class="current">1</span>
</li>
<li class="">
<a>2</a>
</li>
<li class="">
<a>3</a>
</li>
</ul>
<a class="jsNxtPage pgNext">›</a>
</div>
i try this in Selenium
driver.find_elements_by_xpath('(//*[#class="pg"]/ul/li/text())[last()]')
As the method name suggests, it can only return element, not text node. You can find the target <a> element first :
a = driver.find_element_by_xpath('//*[#class="pg"]/ul/li[last()]/a')
And then you can get the inner text from a.text
I set "cacheHtml=true" in web.config file and set sublayout's (ascx file) cache setting.
Whenever I reflash the page, I can see the increasing "From Cache" count in admin page. However, the sublayout(.ascx) doesn't get all its template field values on the page.
Suppose that there is CSS class name, like "nav-list", in its template field and HTML result should look like this.
<div class="nav-list">
something
</div>
But, if I check "cacheable", it shows like this. Otherwise, it shows correct style.
<div class=" ">
something
</div>
I checked:
Checked - Cacheable
Checked - Vary by Data
Checked - Vary by Parameters
Checked - Vary by Query String
What is the issue?
=================== Update ====================
This is the result when I DIDN'T check "Cacheable". (Correct Layout)
<div id="cloud-header">
<div class="navtacular-cover"></div>
<nav class="navtacular navtacular-theme-simple navtacular-variant-dark container block-center">
<h1 class="navtacular-label">
<i class="icon-reorder"></i>
</h1>
<ul class="navtacular-list">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
</ul>
</nav>
This is the result when I DID check "Cacheable". All class values which are from datasource are empty.
<div id="cloud-header">
<nav class="">
<h1 class="">
<i class=""></i>
</h1>
<ul class="">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
<li id="" class="navtacular-item">
</ul>
</nav>
This is the code for all class names which show empty.
<asp:View ID="viewNormalMode" runat="server">
<nav class='<% Response.Write(myDataSourceItem.Fields["Nav Bar Class"]); %>'>
<h1 class='<% Response.Write(myDataSourceItem.Fields["Label h1 Class"]); %>'>
<i class='<% Response.Write(myDataSourceItem.Fields["Label i Class"]); %>'></i>
<% Response.Write(myDataSourceItem.Fields["Nav Bar Label"]); %></h1>
<ul class='<% Response.Write(myDataSourceItem.Fields["ul Class"]); %>'>
<asp:Literal ID="linkObjects" runat="server"></asp:Literal>
</ul>
</nav>
</asp:View>
The class name is from another item which has different template fields, called NavBarLinkItem.
In the NavBarLinkItem templates, there are fields (url, li Class, a Class, li Id, etc...).
Again, the original nav sublayout uses datasource to get those values.
new to Watir and trying to select items in an unordered JS list. I'm using watir-classic and testing in IE with Ruby v1.9.3p392.
<div id="ctl00_content" style="text-align: left; background: #fff;">
<script type="text/javascript"> … </script>
<div id="ctl00_breadCrumb" class="navBar"> … </div>
<div id="ctl00_menuBarUpdatePanel">
<div id="ctl00_menuBar" class="menuBar">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<div class="menu">
<ul>
<div id="ctl00_MenuBarButtons" class="menuBarButtons">
<li id="ctl00_AddMenu" style="display:inline;"> … </li>
<li id="ctl00_ChangeCustodyMenu" style="display:inline;">
Change Custody
<ul>
<li id="ctl00_CheckInMenuItem" style="display:inline;">
<li id="ctl00_CheckOutMenuItem" style="display:inline;">
<li id="ctl00_ReceiveMenuItem" style="display:inline;">
<li id="ctl00_ReturnMenuItem" style="display:inline;">
<li id="ctl00_DropOffMenuItem" style="display:inline;">
<a onclick="window.location = '/WebSite/PropertyItems.aspx?action=dropOff'; return false;" href="">
Drop-off
I'm trying to select Drop-off (the last item in the list) and have attempted using the following:
browser.ul(:id,"ctl00_ChangeCustodyMenu").li(:text,"Drop-off").click
browser.div(:id, "ctl00_MenuBarButtons").li(:index, 1).link(:index, 4).click
... but then read that I can't select items in Watir unless I can see it. I started to test with fire_event("onclick") with the following tip: "I firstly need to fire_event("onmouseover") which selects the item then fire_event("onclick")."
So I try this:
browser.li(:id,"ctl00_ChangeCustodyMenu").fire_event("onclick")
This method will flash the 'Change Custody' menu, but not drop down the list with the 'Drop-down' item. Hence, I am not able to select the item on the list. The Change Custody list will drop once the user hovers over the menu in normal operation.
How do I select the JS drop-down list and select an item in an unordered list? Do I need to use webdriver's select_list, or am I just missing something? Watir-classic seems pretty straight forward and should handle this, but maybe the JS structure is tricky here.
Thanks for the help!
Shannon
I am a beginner in using webdriver and Here is the code with which i am working.
I want to click on a dropdown and again click on the list of values provided for that dropdown.
Please help me, i looked for other threads, but they are different.
<ul>
<li class="size select required">
<div id="dk_container_partNumber-55414" class="dk_container dk_theme_size dk_focus"
tabindex="" style="display: block;">
<a class="dk_toggle" style="width: 162px;">
<div class="dk_options">
<ul class="dk_options_inner">
<li class="dk_option_current">
<li class="">
<a data-dk-dropdown-value="605930243">S</a>
</li>
<li class="">
<a data-dk-dropdown-value="605930251">M</a>
</li>
<li class="">
<a data-dk-dropdown-value="605930260">L</a>
</li>
<li class="">
<a data-dk-dropdown-value="605930278">XL</a>
</li>
<li class="">
<a data-dk-dropdown-value="605930286">XXL</a>
</li>
</ul>
</div>
</div>
<select id="partNumber-55414" class="size-select" name="partNumber" style="display: none;">
<option data-property="CAT_SELECTSIZE" selected="selected" value="">Select Size</option>
<option value="605930243">S</option>
<option value="605930251">M</option>
<option value="605930260">L</option>
<option value="605930278">XL</option>
<option value="605930286">XXL</option>
</select>
I wish to select "S" and the same must be seen on the dropdown.
I am able to click on the dropdown and see the list of options using the below code :
driver.findElement(By.xpath("//(more xpath goes here)/ul/li[#class = 'size select required']/div/a/span")).click();
I tried the below code to select "S", but in vain :
Select SizeDropdown = new Select(driver.findElement(By.xpath("//(more xpath here)/ul/li[#class = 'size select required']/select")));
//driver.findElement(By.xpath("(//(more xpath here)/ul/li[#class = 'size select required']/select)/option[2]")).click();
//Below line gets and clicks the first option of Select Size 'S'
//SizeDropdown.getFirstSelectedOption().click();
//SizeDropdown.selectByVisibleText("S");
//SizeDropdown.selectByIndex(1);
//SizeDropdown.selectByValue("S");
I tried each command separately (after uncommenting),but could not see the "S" as the label of the dropdown as if it is selected by me.
Have you tried simplifying your xpath?
I use something like this to select an option from a drop down without first clicking to expand it:
Select select = new Select(driver.findElement(By.xpath("//select[#id='partNumber-55414']"));
select.selectByVisibleText("S");