Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python with Selenium下拉列表_Python_Selenium_Select_Selenium Webdriver_Drop Down Menu - Fatal编程技术网

Python with Selenium下拉列表

Python with Selenium下拉列表,python,selenium,select,selenium-webdriver,drop-down-menu,Python,Selenium,Select,Selenium Webdriver,Drop Down Menu,我无法使用selenium和python自动创建带有组_选项的帐户。我尝试了几种解决方案,但仍然不起作用。该网站是form.php,请参阅我使用的代码。我在Linux上而不是Windows上 测试-1 driver = webdriver.PhantomJS() select = Select(driver.find_element_by_name('group_option[]')) select.select_by_value("Test") driver.find_element_by_

我无法使用selenium和python自动创建带有组_选项的帐户。我尝试了几种解决方案,但仍然不起作用。该网站是form.php,请参阅我使用的代码。我在Linux上而不是Windows上

测试-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>

选择组
试验
测试1
按值选择
要选择文本为“测试”的选项,可以使用以下解决方案:

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

更新 由于您仍然无法从下拉列表中选择作为替代方案,您可以使用WebDriverwait并使用以下任一解决方案:

  • 备选方案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")
    
  • 备选案文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")
    
  • 注意:您必须添加以下导入:

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

im使用driver=webdriver.PhantomJS()。。我尝试了您的解决方案,但仍然无法使用组选项获得结果。thanks@pguardiario我正在使用Ubuntu服务器headless。你能推荐什么?我总是使用chrome,因为我喜欢调试控制台。我测试了代码,但它不起作用。我不知道是什么wrong@nicollette16检查我的更新答案并让我知道状态。测试了这个,我想我看到了来自其他解决方案的相同代码,但它不起作用
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")
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")
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC