how to select custom dropdown list element from selenium - drop-down-menu

how to select custom dropdown list element from selenium.
I want to select a country from the dropdown list, using selenium python webdrive.
<select id="id_country" class="hidden-field" name="country" data-id="1394114974464-fOg4n">
<div class="custom dropdown" data-id="1394114974464-fOg4n">
<a class="current" href="#">Belize</a>
<a class="selector" href="#"></a>
<ul>
<li class="">Select Your Country</li>
<li class="">Afghanistan</li>
<li>Albania</li>
<li class="">Algeria</li>
<li class="">American Samoa</li>
<li class="">Andorra</li>
<li>Angola</li>
<li class="">Anguilla</li>
<li class="">Antigua and Barbuda</li>
<li class="">Argentina</li>
<li class="">Armenia</li>

The currently recommended method of selecting an item from a dropdown menu is to use the Select class. Here's a quick example:
from selenium.webdriver.support.ui import Select
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("file:///C:\testing\\test.html")
element = browser.find_element_by_id("id_country")
select = Select(element)
select.select_by_visible_text("Armenia")
However, the HTML you posted doesn't seem to work; I just get an empty dropdown box. You'll need to fix that in order to be able to use the above code. For example:
<html><body>
<select id="id_country" name="country">
<option>Select Your Country</option>
<option>Afghanistan</option>
<option>Armenia</option>
</select>
</body></html>
This works fine for me, producing a dropdown box with three options - Select Your Country, Afghanistan and Armenia. Pointing the above Python script at this file correctly selects Armenia.
Edit: Here's a quick-and-dirty Python script that successfully selects Armenia from the list of countries:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://ds.arm.com/rfq/")
country = "Armenia"
dropdown_path = "id('content')/form/fieldset/div[5]/div[2]/div/a[2]"
country_path = "id('content')/form/fieldset/div[5]/div[2]/div/ul/li[contains(text(), '%s')]" % country
browser.find_element_by_xpath(dropdown_path).click()
browser.find_element_by_xpath(country_path).click()
It makes use of XPaths to locate the dropdown arrow and then the <li> tag containing "Armenia". I don't claim that it's especially intuitive, or neat to look at, but it works. They've really over-complicated things with that website, so I'm not sure if the simpler Select method can be made to work here. In general, if you can locate an element using an id (find_element_by_id), class (find_element_by_class) or name (find_element_by_name), you should do so. None of those work here, unfortunately :)
I can recommend the XPath Checker extension for Firefox to help you find similar XPaths for the other form elements. Good luck, I really don't envy you having to work with that site!!

Related

How to remove class from all list of an id with prototypejs

I want to remove all classes named active of all <li> under <ul id="navNovelty"> using prototypejs.
I did $('ul#navNovelty li').removeClassName('active'); but it did not work. Here is my full code
<ul id="navNovelty" class="nav om">
<li id="multimedia" class="active">Multimedia</li>
<li id="love">Love & Family</li>
<li>MyBusiness</li>
</ul>
How to remove class from all list of an id with prototypejs ?
Unlike jQuery, Prototype has a different accessor for single elements and multiple elements. If you want to target a single item, and find it by its id, you use one dollar sign:
$('navNovelty').removeClassName('active');
If you want to access multiple items, you use two:
$$('ul#navNovelty li').map('removeClassName', 'active');

Selenium webdriver: How to find nested tags?

A webpage contains
<div class="divclass">
<ul>
<li>
"hello world 1"
<img src="abc1.jpg">
</li>
<li>
"hello world 2"
<img src="abc2.jpg">
</li>
</ul>
</div>
I am able to get data under div using
element = driver.find_element(class: "divclass")
element.text.split("\n")
But I want all links respective to the achieved data
I tried using
driver.find_elements(:css, "div.divclass a").map(&:text)
but failed.
How can I get related links to the data?
If you want to get the href attribute try the below code(I am not familiar with ruby so I am posting the code in Java).
List<WebElement> elements = driver.findElements(By.xpath("//*[#class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getAttribute("href"));
}
The xpath points to all the a tags under the div tag with class name =divclass.
If you want to get the text of all the links, you can use the blow code:
List<WebElement> elements = driver.findElements(By.xpath("//*[#class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getText());
}
Hope it helps.
In ruby
element = driver.find_elements(:xpath, "//*[#class='divclass']//a")
list = element.collect{|e| hash ={e.text => e.attribute("href")}}
will return corresponding links with data in array of hashes

Click on a link in a table based on the other text in the row in selenium IDE 2

I am new to selenium and have very little coding experience. That being said, I am trying to set up a suite of regression tests for my software. I run into a problem when making a test to add and save a new contact and information, then immediately delete the contact (just to make sure any new additions to the software don't break that functionality). My contacts are saved into a table with the name in the first column, details in the second, edit link in the third, delete link in the fourth, and assign contact link in the fifth. Everything works fine right after I set up the test, but the IDE assigns what delete link to click with an xpath so if i add another contact before my test contact, the wrong one gets deleted. How do I target the delete link in my test contact row so it will search for the text "test contact" and click the delete link in the same row everytime even if i have changed the order of the contacts in the table or added many new contacts? Below is a sample of one row in the table. I need to select delete based on the name in that row.
<table class="tbl">
<thead>
<tbody>
<tr>
<td>
<a class="entityLink" target="_attached" href="../download/Average+Inspection.xlam?documentID=55840"> Average Inspection.xlam </a>
</td>
<td></td>
<td>43.76 Kb</td>
<td>SP: Randi kay Anderson</td>
<td>5/16/14 1:13 PM</td>
<td> No </td>
<td>
<ul class="actlinks llink">
<li>
<a class="al-assign" target="_top" href="/app/admin/massAssignFiles.do?m=massAssignSLFileAttachment&returnPage=BACK_ADMIN_SEARCH_UPLOADEDFILE&documentID=55840"> Assign </a>
</li>
<li>
<a class="al-delete" onclick="return myConfirm();" href="/app/admin/manageFiles.do?m=deleteFile&documentID=55840">Delete</a>
</li>
<li>
<a class="al-edit" href="/app/admin/manageFiles.do?m=editFile&fileid=55840">Edit</a>
</li>
<li>
<a class="al-edit" target="_top" href="sharingPermissions.do?m=editFileSharingPermissions&returnPage=BACK_ADMIN_SEARCH_UPLOADEDFILE&fileID=55840">Edit Sharing Permissions</a>
</li>
</ul>
</td>
</tr>
What I feel, there should be some uniqueness among the entries of table.So you should use that unique property to locate and perform action on your required element.
By looking into above table, I assume that ?documentID=55840 is that unique property for each table and you can use this to locate the required element. The only thing you have to add in your code is , after adding a new entry your code should be able to obtain the documentId of newly added entry.
You can do it in following steps:
Add a new entry
Obtain the document Id of newly added entry
Using the document Id locate the delete element
// Given text should be dynamic to make it generic
String hrefOfElememnt = driver.findElement(By.xpath("//a[contains(text(),'Average Inspection.xlam')]")).getAttribute("href");
String[] hrefArray= hrefOfElememnt .split("?");
String[] documentIdArray = hrefArray[1].split("=");
String documentId = documentIdArray [1];
// To find delete button
WebElement deleteLink = driver.findElement(By.xpath("//a[#class='al-delete' and contains(#href,'"+ documentId +"')]"));
deleteLink.click();
use this
command- click
target- xpath=//*[text()="text you want to click"]
I used it, this will solve your problem

Select href with id and class using xpath

let say I have DOM like this:
<div id="tabsmenu">
<ul>
<li class="one">foo</li>
<li class="two">baz </li>
</ul>
</div>
and I would like to get the text from <a href> elements:
# desired output: ['#foo', '#baz']
How to do it using xpath and using combination id and element with a specific class within id ?
Already tried:
some_doc.xpath('//a[#id="tabsmenu"]/[#class="ui-tabs-anchor"]/#href')
# select all href tags of any a element that is in id tabsmenu and class attribute ui- tabs-anchor
EDIT - corrected tabmenu into tabsmenu
You're most likely looking for something like this:
//div[#id='tabsmenu']//a[#class='ui-tabs-anchor']/#href
That will get all href attributes that are part of an a tag with the class ui-tabs-anchor and inside a div element with the id tabsmenu.
Also you might want to take a look at this question:
Find out if class name contains certain text
This is because the class will match the exact value (ui-tabs-anchor) and maybe some additional class might be added there such as class="ui-tabs-anchor disabled" and then there will not be a match in there.

Using Xpath to get text from span- webdriver

I'm using selenium webdriver to get some text on my webpage using xpath.
This is the code
<a class="ng-binding" data-toggle="tab" href="#tabCreatedByMe">
Created By Me
<span class="badge ng-binding">3</span>
</a>
I need to get the number '3'. this number is changing everytime
I made this code but it does not return anything
public String getAmountSubtab1() throws InterruptedException{
WebElement s = driver.findElement(By.xpath("//*[#class='badge ng-binding']"));
return s.getText(); }
Suggestions?
Are you sure you have only one span with the class badge ng-binding It might be that you might have another span before this with the same class name. Advised not to use class name when identifying an element. Use this xpath. Should work.
//a[contains(text(), 'Created By Me')]/span

Resources