I am trying to automate spicejet.com website, but the thing is I am not able to access the dropdown values using select as the dropdown values like destination, source etc are not in Select Option="value"
format. When I try to work on using click and select the places it's not working as the click lands me in different page. and it's not only that, I have seen same pattern in many websites. using input field, buttons and lists instead of direct drop down lists.
-
1Please show the exact HTML you are working with and also the code for the attempts you have made. Then we can advise better– Michael DurrantApr 7, 2016 at 11:11
-
What client for webdriver do you use? python/java/js?– bogdan.mustiataApr 7, 2016 at 16:32
3 Answers
Solution:
I have face this issue. Its really weird when all automation scripts and code written as per standards but ultimately it get failed. Below solution works for me:
Step 1.
WebElement listbox_webelement = drive.findElement(By.xpath("write_your_xpath_of_element"));
Step2.
listbox_webelement.click() [ Expected to list get visible ]
Step3.
Select select = new Select(listbox_webelement);
Step4.
select.sendKeys("Text_From_Your_Listbox");
OR
Step4.
listbox_webelement.sendKeys("Text_From_Your_Listbox");
You are using Select class to manipulate an ul element (unordered list) and it will fail, as Select class was designed to cater select (i.e dropdowns) elements only.
Now to solve your problem statement, say I have to select Kochi(COK) from the list, follow the below steps :
Do a sendkeys.
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).sendKeys("COK");
Now perform a click using linktext.
driver.findElement(By.linkText("Kochi (COK)")).click();
Hope this helps.
As a matter of fact for drop down list, in Selenium we have Select class. To use this select class you have to use import statement-
import org.openqa.selenium.support.ui.Select
This Select class has several methods that are supported to access the elements.
- ByVisibleText
- ByIndex
- By.Id
- ByValue
Sample code:
Select dropDown1 = new Select(element1);
dropDown1.selectByIndex(2);