2016-09-19 112 views
0

尝试使用python和selen自动填充表单。下拉菜单的HTML是:python/selenium中的下拉菜单

<select id="typeOfTeacher" class="chosen-select-no-single ng-untouched ng-dirty ng-valid-parse ng-valid ng-valid-required" required="" ng-class="{ 'has-error' : positionDetailForm.typeOfTeacher.$invalid && !positionDetailForm.typeOfTeacher.$pristine }" ng-change="vm.setRequired()" tabindex="-1" ng-model="vm.data.typeOfTeacher" name="typeOfTeacher" data-placeholder="Select" style="display: none;"> 
<option value="" disabled="" selected="">Select</option> 
<option class="ng-binding ng-scope" value="1" ng-repeat="teacherType in vm.teacherTypes">No position at the moment</option> 
<option class="ng-binding ng-scope" value="2" ng-repeat="teacherType in vm.teacherTypes">Supply</option> 
<option class="ng-binding ng-scope" value="3" ng-repeat="teacherType in vm.teacherTypes">Permanent</option> 
</select> 

Python代码是:

elem = Select(browser.find_element_by_id('typeOfTeacher')) 
elem.select_by_value("1") 

错误是 “元素当前不可见,不能与之交互”。

+0

难道你'WevDriverWait'试图等到相互作用之前元素可见? –

回答

0

我还没有使用python Select方法,但我猜测错误消息意味着菜单没有被打开,因此菜单中的元素仍然隐藏,无法与之交互。

尝试这样:

element = driver.find_element_by_id('typeOfTeacher').click() 
driver.find_element_by_css_selector("[value=\"1\"]").click() 
0

这会工作

element = driver.find_element_by_id('typeOfTeacher').click() 
element.find_element_by_xpath(".//option[@value='1']").click() 
0

它看起来像计时问题。你应该尝试使用Waits

我会建议你使用WebDriverWait等到下拉可视交互,如下之前: -

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

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "typeOfTeacher"))) 

select = Select(element) 
select.select_by_value("1")