简体   繁体   English

带有Selenium的Python下拉列表

[英]Python with Selenium drop down list

I cant get automate creating account with group_option selected using selenium with python. 我无法使用python和硒来自动选择group_option创建帐户。 I tried several solutions but still it doesn't work. 我尝试了几种解决方案,但仍然无法正常工作。 the website is form .php please see codes i used. 该网站是.php格式,请参阅我使用的代码。 Im on Linux not Windows. 我在Linux上不是Windows。

test-1 试验-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 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>
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')
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')

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: 由于您仍然无法从下拉列表中进行选择,因此可以诱导WebDriverwait并使用以下任一解决方案:

  • Option A: 选项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: 选项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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM