Python with Selenium下拉列表
我无法使用selenium和python自动创建带有组_选项的帐户。我尝试了几种解决方案,但仍然不起作用。该网站是form.php,请参阅我使用的代码。我在Linux上而不是Windows上 测试-1Python 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_
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
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