Rupesh Katekar
Rupesh Katekar

Reputation: 23

Having issue with selecting drop down in selenium webriver

I have a sample HTML source of the dropdown. I have tried with all possibilities but I having

"Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable" error in selenium web driver.

Plz, give me a solution to select the dropdown values in the web driver. What should I use?[HTML source here][1]

WebElement clickclientdrpdown=driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div[4]/div/form/div[1]/span/span[1]/span/span[1]")); 
clickclientdrpdown.click(); 
WebElement selectclientdrpdown = driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div[4]/div/form/div[1]/span/span[1]/span/span[1]")); 
selectclientdrpdown.sendKeys("1 Private solution"); 

Upvotes: 2

Views: 294

Answers (1)

Ryan Wilson
Ryan Wilson

Reputation: 10765

Your xpath is prone to breaking easily if the format of the HTML ever changes, just use findElement(By.Name), the name attribute is less likely to change as it is part of the Form and name is the parameter name passed to the server:

//Selenium method  specific, prone to failure if element is disabled or not visible
WebElement selectclientdrpdown = driver.findElement(By.name("companyId"));
selectclientdrpdown.sendKeys("1 Private solution");



//Using the JavascriptExecutor
JavascriptExecutor js = (JavaScriptExecutor)driver;
js.ExecuteScript("document.querySelector("select[name='companyId'].value = '1 Private solution';"); 

Upvotes: 2

Related Questions