nicollette16
nicollette16

Reputation: 57

Python with Selenium drop down list

I cant get automate creating account with group_option selected using selenium with python. I tried several solutions but still it doesn't work. the website is form .php please see codes i used. Im on Linux not Windows.

test-1

driver = webdriver.PhantomJS()

select = Select(driver.find_element_by_name('group_option[]'))
select.select_by_value("Test")
driver.find_element_by_name("submit").click()

website.php

<select onchange="javascript:setStringText(this.id,'group')" id="usergroup" name="group_option[]" class="form" tabindex="105">
    <option value="">Select Groups</option>
    <option value=""></option>  
    <option value="Test"> Test </option>
    <option value="Test1"> Test1 </option>
</select>

Upvotes: 3

Views: 3460

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193098

To select the option with text as Test you can use the following solution:

select = Select(driver.find_element_by_xpath("//select[@class='form' and @id='usergroup'][contains(@name,'group_option')]"))
select.select_by_value("Test")

Update

As you are still unable to select from the dropdown-list as an alternative you can induce WebDriverwait and use either of the following solutions:

  • Option A:

    select = Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//select[@class='form' and @id='usergroup'][contains(@name,'group_option')]"))))
    select.select_by_value("Test")
    
  • Option B:

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='form' and @id='usergroup'][contains(@name,'group_option')]"))))
    select.select_by_value("Test")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Upvotes: 2

Kaushik
Kaushik

Reputation: 150

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('url')

select = Select(driver.find_element_by_xpath("//select[@id='usergroup']"))

# select by visible text
select.select_by_visible_text('Test')
 OR
# select by value 
select.select_by_value('Test')

Upvotes: 0

min2bro
min2bro

Reputation: 4628

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get('url')

select = Select(driver.find_element_by_id('usergroup'))

select by value

select.select_by_value('Test')

Upvotes: 0

Related Questions