python / selenium中的下拉菜单

时间:2016-09-19 16:38:25

标签: python selenium selenium-webdriver

尝试使用python和selenium自动填充表单。下拉菜单html是:

<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")

错误是&#34;元素当前不可见,可能无法与&#34;进行交互。

3 个答案:

答案 0 :(得分:0)

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

尝试这样的事情:

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

答案 1 :(得分:0)

这样可行

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

答案 2 :(得分:0)

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

我建议你使用var error = function(message) { // A nice wrapper function stderr.accept(message); // 'accept', since we passed a `Consumer<...>` }; error('error'); // Will now print to Java's stderr 等到下拉前可见下拉列表如下: -

WebDriverWait