How to search and select text in dropdown using selenium webdriver - java

I am new to Selenium and i am stuck in one implementation
My requirements are
Go to Settings page
Look for the dropdown
Fetch all values from dropdown
Look for a certain text in dropdown
If the text is not selected,Select the particular text in dropdown
My code is as follows
void setru()
{
driver.switchTo().frame("contentFrame");
Select rudropdown=new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
List<WebElement> drop=rudropdown.getOptions();
int e=drop.size();
for(int i=0;i<e;i++)
{
String temp=drop.get(i).getText();
String actual_RU="000139, NEXTRAN CORPORATION - JACKSONVILLE";
boolean flag=false;
if(temp.contains("000139"))
{
flag=true;
rudropdown.selectByValue("actual_RU");
}
}
But this is giving me error message.
org.openqa.selenium.NoSuchElementException: Cannot locate option with value: actual_RU

It may works if you replace the below line in your code
rudropdown.selectByValue("actual_RU");
with
rudropdown.selectByValue(temp);

There seems to be a typo mistake. Use below code
rudropdown.selectByValue(actual_RU);
Instead of
rudropdown.selectByValue("actual_RU");

Related

Unable to click or select the value which is displayed in dropdown

I am trying to implement some code using Selenium Webdriver using Java.
Basically, I have a website with a text box. Once user enter the first letter, based on that a value will be displayed(using AJAX). I need to select the particular value, which i mentioned in send keys .
WebElement fromCity = driver.findElement(By.id("pickUpLocation"));
fromCity.sendKeys("A Ma Temple / 媽閣");
Thread.sleep(2000);
WebElement ajaxContainer1 = driver.findElement(By.className("txt-box ng-touched ng-dirty ng-valid"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if (value1.getText().equals("A Ma Temple ")) {
((WebElement)ajaxValues1).click();
break;
}
}
After you send keys.your Ajax value should be retrieved in a box related to you keyword search.You need to get the complete box.and fetch each one as you have done in for loop .get the text and compare it with your expected text and click where this condition is true.
What is that line for before thread.sleep()
I think u can try selecting through index. It should be like this
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);
0 represents the first element in the dropdown. Based on index number of that element, feed the corresponding number in selectByIndex(0)
Let me know if this helps. Thanks

Selenium Java : Handling check boxes

Screenshot
I have to test a question-answer module where user will enter answers through check box. There are total 5 j-query tabs with some questions. and it will move to other tab if answers are given and clicked "Save and next" button. I am going following way but it is not selecting every check box. because some check boxes are not displayed. Can someone suggest better way to select each and every checkbox?
List<WebElement> chkbx = driver.findElements(By.xpath("//input[#type='checkbox']"));
int j=1;
while(j<5){
for(int i = 0;i<chkbx.size();i++){
if(chkbx.get(i).isDisplayed()){
chkbx.get(i).click();
}
}
driver.findElement(By.xpath("x-path of save button")).click();
j++;
}
Try below code to get all your checkboxes and perform the click operation on the displayed checkboxes :
List<WebElement> chkbx =driver.findElements(By.xpath("//input[#type='checkbox']"));
for(WebElement e:chkbx)
{
if(e.isDisplayed())
{
e.click();
Thread.sleep(1500);
}
}

innerHTML not getting values Selenium Java

Purpose: My code should get the all the links that are available under a drop down menu. It should print all those links under that specific menu.
Site used for testing: http://test1.absofttrainings.com and the specific menu option is Test Pages.
Question/Problem: The code is not printing 2 values that I am expecting, there are 2 links under Test Pages.
Code:
WebDriver driver= new FirefoxDriver();
driver.get("http://test1.absofttrainings.com/#");
//Step1: Create a List of WebElements to put all the links
// the //a will give me all the links associated
List<WebElement> drop_downs= driver.findElements(By.xpath("//a[contains(text(), 'Test Pages')]//a"));
for(int i=0;i<drop_downs.size(); i++){
WebElement e= drop_downs.get(i);
String text=e.getAttribute("innerHTML");
System.out.println("Links are " + text);
}
Thanks in advance for your time.
Code snippet
//find all the links of sibling(s) of the 'Test Pages'
List<WebElement> drop_downs= driver.findElements(By.xpath("//*[contains(text(), 'Test Pages')]/following-sibling::*//a"));
for(WebElement links : drop_downs){
System.out.println(links.getAttribute("innerHTML"));
}

Selenium is unable to locate any element in html page

I'm trying to make a small java app. that can log in to my university portal, I used Selenium in the following code:
//some import statements
public class Portal{
public Portal(){
File file = new File("C:/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
HtmlUnitDriver driver = new HtmlUnitDriver();
String target = "http://portal.kfupm.edu.sa/cp/home/loginf";
driver.get(target);
ArrayList <WebElement> inputs = (ArrayList<WebElement>) driver.findElements(By.tagName("input"));
System.out.println(inputs.size());
for(WebElement input : inputs){
System.out.println(input.getAttribute("value") + " " + input.getAttribute("name"));
// to insure the link is displaying something
ChromeDriver driver1 = new ChromeDriver();
driver1.get(driver.getCurrentUrl());
}
public static void main(String [] args){
new Portal();
}
}
the problem is when I use this target (my university portal) I get inputs.size() = 0; although, I'm sure there are elements with (input) tagName. Also I get the same resulst whatever was the method of (By) class I used.
However, when I change the target to any link (for example: "http://www.google.com" or "http://www.facebook.com" , I get elements in the inputs ArrayList (all elements of tagName (input) that are in the target html page). Can any body please tell me what is the problem and how can I solve it? thanks in advance..
The reason that you find zero elements with tag input is that all the elements lie inside a frame tag. Selenium is able to see to current window and all the elements that are defined inside frame tag (i.e. are inside a frameset) are invisile to it.
To see the element you will need to switch frame first, So that selenium has control inside the target frame and not in the outer window. Try this
driver.swtichTo().frame(0); // this will move selenium control inside first frame

Testing drop down list values in Selenium - Console does not print mis-match

I'm testing drop down list values in Selenium. I am working in Java.
A drop down list has 40 values. I want my code to get all the values in the DDL and compare it to the ones they should be.
My code as it stands will fail if the values in the DDL do not match the hardcoded values I expect. It prints a message which says values do not match. However it doesnt tell me which value has not matched.
What is the best way to achieve this?
I am using selenium.getSelectOptions to get the DDL values. Then using if statement to check if values are same, if not then fail. But it doesnt tell me which value is not the same.
Well without the code is hard to help you without the code. I will try to outline a little help how do I approach this:
public void chooseProduct(String product){
List<WebElement> Options = new ArrayList<WebElement>();
productChoooser = driver.findElement(By.id("id_of_the_selectbox"));
productChoooser.click();
Select select = new Select(productChoooser);
Options = select.getOptions();
for (WebElement option:Options){
if(option.getText().equals(product)){
option.click();
}
}
The above code select one item from the dropdown. You can easily modify it bymodyfying the if statement in this, say:
boolean foundIt = false;
for (WebElement option:Options){
if(option.getText().equals(product)){
System.out.println("The product " + product + "was found in the drop down list");
foundIt = true;
}
}
if (!foundit){
System.out.println("The product " + product + "was NOT found in the drop down list");
}
}
please recheck it after me, I am writing the code directly to the answer window

Categories