Skip to main content
 首页 » 编程设计

带有 Selenium 的 Python 下拉列表

2024年12月31日898°冷暖

我无法使用带有 python 的 selenium 选择的 group_option 自动创建帐户。我尝试了几种解决方案,但仍然无效。该网站是 .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() 

网站.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> 

请您参考如下方法:

要选择带有文本的选项作为测试,您可以使用以下解决方案:

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