I want to select the first web element or any random value from a drop down list. Can anyone please guide me how we can select it with Selenium WebDriver with Java.
Here are the docs for the Dropdown List component.
There are two ways we can select a country name manually.
Click on Select Drop down -> Type text United Kingdom and press Enter. I tried the code below but it did not work.
WebDriver driver=new ChromeDriver(); System.setProperty("webdriver.chrome.driver", "D:\\Eclipse19\\SampleProject\\chromedriver.exe"); driver.get("https://design.appway.com/screen/Design:Components_DropdownList"); Thread.sleep(2000); WebElement SelectCountryDropDownWebElement=driver.findElement(By.xpath("//div[@id='aw-id-gen-10']")); SelectCountryDropDownWebElement.click(); Thread.sleep(2000); WebElement SelectCountryinputtexbox=driver.findElement(By.xpath("//input[@placeholder='Select country']")); SelectCountryinputtexbox.sendKeys("United Kingdom"); Thread.sleep(1000);
I also tried with Robot class. I also used the sendkeys method with Keyboard event but it did not work for me .
Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ENTER);
Another manual way is to click on the drop down text box, the list of countries will get displayed then mouse hover to required country and click it .
I also tried using Action method and mouse hover, also used getoptions method of the select class but I was not able to select the value through automation script. Please note here we do not have select class.
It would be very nice if someone could please suggest how we can select a value from the drop down on this website.
CodePudding user response:
See if this works. Click on the dropdown, identify the input box inside it and send the value
driver.findElement(By.xpath("//div[@id='Section-1']//./div[@class='awTextInput xTextInputx']")).click();
driver.findElement(By.xpath("//div[@id='aw-id-gen-7']/input[@class='awTextInput_input']")).sendKeys("United kingdom", Keys.ENTER);
Add wait time to see the input is getting entered
CodePudding user response:
I am a python resource, so writing in python, but you may adapt these locators to JavaScript. This worked for me.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='awTextInput xTextInputx']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='awList']//li[@id='GB']"))).click()
You may replace the id GB
with any other option, or if you use //*[@class='awList']//li
you can loop through all the 54 options that are available.