How to locate Telerik controls in Selenium? - java

URL :
http://demos.telerik.com/aspnet-ajax/dropdownlist/examples/overview/defaultcs.aspx
Question :
How to select dropdown value?
My Code:
Select dropdown = new Select(driver.findElement(By.xpath("//a[#class='rddlSlide']//span")));
dropdown.selectByVisibleText("Chai");

How to select dropdown value?
Actually target element is not proper <select> element, Hence you can't handle is using Select class.
Try as below :-
driver.get("http://demos.telerik.com/aspnet-ajax/dropdownlist/examples/overview/defaultcs.aspx");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 60);
//This line would find the dropdown element and open the options
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_ContentPlaceholder1_RadDropDownProducts"))).click();
//This line would select the desire option using their text
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text() = 'Chai']"))).click();

Related

Select value by text from a dynamic non select dropdown using Selenium Java

I want to select value by text from a dynamic non select dropdown. I did some research and I found this code:
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("/html/body/div[1]/div[2]/div/div/div"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
Basically it put the dropdowns options into a List element, and run through in a for loop, and if the options contains text, click it and break the loop. However it is not working with this type of dropdown:
Snapshot:
Can you suggest what can I do?
Snapshot of Specific value:
Note: The page is only available via private vpn, so I cannot share it.
To select the value by text Teszt_5 from a dynamic non Select dropdown you can use the following locator strategies:
String text = "Teszt_5";
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("//div[#class='mat-autocomplete-panel mat-autocomplete-visible ng-star-inserted' and starts-with(#aria-labelledby, 'mat-form-field-label')]//mat-option//span[#class='mat-option-text']"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
References
You can find a couple of relevant detailed discussions in:
Automating jQuery based bootstrap dropdown using Selenium and Java
How to select a DropDown from a list but HTML don't have select Tag using Selenium Java

Dropdown not working with selenium for this site

Hi I tried using the below code to use the dropdown from the foodpanda site and choose the city name but it's not working for me.
public static void main(String args[]) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.foodpanda.in/restaurants/city/pune?gclid=CIbFi5iEvdMCFdeFaAodujsK5w");
Thread.sleep(5000);
WebElement drp =driver.findElement(By.id("cityId"));
Select drp2 = new Select(drp);
drp2.selectByVisibleText("Bangalore");
It gives out the error-
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
That <select> element has the CSS display: none;. Its just a place holder.
I think the site uses some fancy <span>s to get the awesome look of the drop down. Can you please check that?
So I was trying to figure out how the dropdown works & here's what I found. The select is just a placeholder, it has no role on the webpage. The dropdown is populated using a javascript call when the input box (see below) is clicked.
Once you click inside the input box, some DOM elements are added which contain <p> tag with city names, which when clicked, selects the target city you want to select.
Based on my observation, a span with class name tt-suggestions is visible when the input element is clicked. This span has a div within it which has all the p elements within it. You should aim to click the corresponding p element which has the text of your desired city name.
Here's a working code:
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(
By.xpath("//input[#class='form-control twitter-typeahead tt-input']")
).click(); // this will click inside the input element
WebElement drp = (WebElement) wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//span[#class='tt-suggestions']"))); // this is the span element which gets populated
System.out.println(drp.getAttribute("innerHTML")); // just in case you want to see the above span's HTML code
drp.findElement(By.xpath("//div/p[text()[contains(.,'Bangalore')]]")).click(); // This will select Bangalore from the Dropdown

Selenium Java - How to Select a Dropdown Element that Has No ID

I am using Selenium - Java WebDriver (ChromeDriver) as a new user.
While trying to select an item from a dropdown menu using Java, I couldn't do it because of the error message. Please note that I've tried variations of Select and WebElement options but not having the expected result: clicking the link from the drop down menu that should take me to the target page.
Here is the error message shown in eclipse:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Payment"}
Here is the relevant code segment:
Select dropdown2 = new Select(webDriver.findElement(By.linkText("Payment")));
dropdown2.selectByVisibleText("Payment");
I have also tried the following with no success:
WebElement element = webDriver.findElement(By.cssSelector("a[class='glyphicon glyphicon-credit-card']"));
element.click();
Also, the following code that didn't work:
WebElement element = webDriver.findElement(By.partialLinkText("Payment"));
Select mySelect= new Select(element);
mySelect.selectByVisibleText("Payment");
The segment of html is shown below:
Hoping to get feedback.
Thank you.
As I'm seeing in provided screenshot HTML, this is not a <select> element, so you can't use Select() class here to work with dropdown.
You should try using simple finder with WebDriverWait as below :-
WebDriverWait wait = new WebDriverWait(driver,10);
//First click on dropdown down to open options
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.dropdown > a.dropdown-toggle"))).click();
//Now select opened option
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.dropdown-menu > li > a[href*='Billing']"))).click();
Or
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Payment"))).click();
Or
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Payment"))).click();

How to select a dropdown value inside a frame in webdriver using java

I am trying to select a dropdown value which is inside a frame. This is the code
This is how we write for a link with in frame but i am unable to select a value from dropdown
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("right"));
WebElement el1 = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Text")));
el1.click();
First, wait for the correct frame (according to HTML code, the name of the frame is main_b)
Next, you don't have a link there (<a> tag), so By.partialLinkText cannot be used. Use By.name("field") instead
Finally, instead of clicking on it, get a Select object: Select mySelect = new Select(el1); and choose one of its options using selectByVisibleText, selectByValue or selectByIndex method
So all together looks like this:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("main_b"));
Select mySelect = new Select(
wait.until(
ExpectedConditions.elementToBeClickable(
By.name("field")
)));
// Select second option by visible text
mySelect.selectByVisibleText("Bacon Anna");
// Same option selected by value
mySelect.selectByValue("16344");
// Same option selected by index
new Select(el1).selectByIndex(1);
Try below code:
WebElement fr = driver.findElement(By.name("main_b"));
driver.switchTo().frame(fr);
WebElement dropdown = driver.findElement(By.name("field"));
Select sel = new Select(dropdown);
sel.selectByValue("<Value to be selected>");
You can also use wait commands if your page takes some time to load. Hope it helps.

need help to find Xpath of following html--selenium--

I am using firebug for finding xpath, this is the error that is displayed in the error console.
no such element: Unable to locate
element:{"method":"xpath","selector":".//*[#id='select2-contact_id-result-v0w5-258']"}
driver.findElement(By.xpath(".//*[#id='select2-contact_id-result-v0w5-258']")).click();
html is as follow
id="select2-contact_id-result-v0w5-258" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Single contact
There could be following reason for it :-
May be your id is dynamically generated, so you need to try with different locator to create By object as below
By by = By.className("select2-results__option select2-results__option--highlighted");
or
By by = By.cssSelector(".select2-results__option select2-results__option--highlighted");
or
By by = By.xpath("//*[contains(., 'Single contact')]");
or if id is not dynamically generated
By by = By.id("select2-contact_id-result-v0w5-258");
May be your element is inside a frame or iframe, If it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("frame id or name or index");
May be element is not being fully loaded on the page due to slow internet, so you need to implement WebDriverWait to wait until element is visible and clickable as below :-
WebDriverWait wait = new WebDriverWait (driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(by)); //use anyone of the above by object
Now after successfully getting element you need to perform click as below :-
el.click();
Note :- if your element is actually a dropdown with select tag, then you need to create Select() object to work with dropdown as below :-
Select sel = new Select(el);
//now perform step to select an option by visible text from dropdown
sel.selectByVisibleText("your visible option text");
or
sel.selectByIndex("your option index");
or
sel.selectByValue("your option value");
Hope it helps you..:)

Categories

Resources