2016-09-19 69 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")