-1

I'm using selenium with Tor and I'd like to select an item from a dropdown list by the visible text or by part of text. The class Select doesn't work cause of a bug in firefox

So this is my code:

Select dropdown = new Select(driver.findElement(By.id("serverLogin"))); //Selects my dropdown
dropdown.selectByVisibleText(server);   //Selects the server

How can I do it using javascript executor?

Naman
  • 27,789
  • 26
  • 218
  • 353
Davide Melis
  • 39
  • 1
  • 8

2 Answers2

0

Lets try with below code and see if it works-

WebElement dropDownListBox = driver.findElement(By.id("serverLogin"));
((JavascriptExecutor)driver).executeScript("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", dropDownListBox, "blablabla");
Kapil
  • 397
  • 4
  • 6
  • Still nothing... it opens the dropdown's options list but it doesn't select anyone then it closes the dropdown leaving always the first option selected – Davide Melis Sep 03 '17 at 11:22
  • what is the firefox version you are using? – Kapil Sep 03 '17 at 11:25
  • I'm using the latest version of Tor using the code I found here https://stackoverflow.com/a/23320554/8142148 It works all perfectly except for dropdowns – Davide Melis Sep 03 '17 at 11:35
  • can you paste DOM and some more of your exact code? – Kapil Sep 03 '17 at 11:41
  • This is the output I get: 1504442314198 Marionette INFO startBrowser 613d1f95-3368-435e-87be-020aa90850fb 1504442314219 Marionette INFO sendAsync 613d1f95-3368-435e-87be-020aa90850fb set 03, 2017 2:38:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMAZIONI: Detected dialect: W3C Password Attuale: 1234aa 1504442315212 Marionette INFO sendAsync 613d1f95-3368-435e-87be-020aa90850fb 1504442345032 Marionette INFO sendAsync 613d1f95-3368-435e-87be-020aa90850fb 1504442345064 Marionette INFO sendAsync 613d1f95-3368-435e-87be-020aa90850fb Note that it works on chrome – Davide Melis Sep 03 '17 at 12:41
0

I solved it creating a dropdown WebElement and sending to it the first letter to select my option, then sending Enter key. Here is the code:

WebElement dropdown = driver.findElement(By.id("serverLogin")); 
        dropdown.sendKeys(server);  
        dropdown.sendKeys(Keys.ENTER);

thanks anyway.

Davide Melis
  • 39
  • 1
  • 8