当我试图从下拉菜单中选择一个元素时,我总是遇到一个错误



我正试图使用selenium和Python从下拉菜单中选择一个元素。我要选择的元素是借记卡的到期月份和年份。每当我运行代码时,它都会抛出以下错误消息:

属性错误:"bool"对象没有属性"clear"

这是我使用的代码:

wait = WebDriverWait(driver,   delay).until(ec.text_to_be_present_in_element((By.ID,'ExpirationDateMonth'),"4"))
print("page is ready")
except TimeoutException:
print("Loading took too much time")                               
select = Select(driver.find_element_by_id('ExpirationDateMonth'))
select.select_by_index
wait.clear()
wait.click()
wait.sendkeys('4')
print(wait)
time.sleep(11)
driver.close();

''''html

*</div>
<div class="form-group">
<div class="col-sm-3 col-sm-offset-3">
<label for="ExpirationDate" class="control-label">Expiration Date</label>
</div>
<div id="ExpirationDate">
<div class="col-sm-1">
<label class="control-label" for="ExpirationDateMonth"> Month</label>
<select id="ExpirationDateMonth" class="form-control form-control-select" data-val="true" data-val-required="The SelectedExpirationDateMonth field is required." name="CardPayment.SelectedExpirationDateMonth"><optgroup label="Expiration Date Month"></optgroup><option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</div>
<div class="col-sm-1">
<label class="control-label" for="ExpirationDateYear"> Year </label>
<select id="ExpirationDateYear" class="form-control form-control-select" data-val="true" data-val-required="The SelectedExpirationDateYear field is required." name="CardPayment.SelectedExpirationDateYear"><optgroup label="Expiration Date Year"></optgroup><option>2020</option>
<option>2021</option>
<option>2022</option>
<option>2023</option>
<option>2024</option>
<option>2025</option>
<option>2026</option>
<option>2027</option>
<option>2028</option>
<option>2029</option>
<option>2030</option>
<option>2031</option>
<option>2032</option>
<option>2033</option>
<option>2034</option>
<option>2035</option>
<option>2036</option>
<option>2037</option>
<option>2038</option>
<option>2039</option>
<option>2040</option>
<option>2041</option>
<option>2042</option>
<option>2043</option>
<option>2044</option>
</select>
</div>*

我试过这个代码

select = Select(driver.find_element_by_xpath('//*[@id="ExpirationDateMonth"]'))
select.select_by_value("12")
And it gave me this error
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-7-d8a234e99906> in <module>
104 # select = Select(driver.findElement(By.id("//*[@id="ExpirationDateMonth"]")));
105 select = Select(driver.find_element_by_xpath('//*[@id="ExpirationDateMonth"]'))
--> 106 select.select_by_value("12")
107 
108 # select = Select(driver.findElement(By.xpath("//*[@id="ExpirationDateMonth"]")));
C:ProgramDataAnaconda3libsite-packagesseleniumwebdriversupportselect.py in select_by_value(self, value)
85             matched = True
86         if not matched:
---> 87             raise NoSuchElementException("Cannot locate option with value: %s" % value)
88 
89     def select_by_index(self, index):
NoSuchElementException: Message: Cannot locate option with value: 12

I also tried this:
select = Select(driver.findElement(By.xpath('//*[@id="ExpirationDateMonth"]')));
select.deselectAll();
select.selectByVisibleText("Value12");
And It gave me this error:
AttributeError                            Traceback (most recent call last)
<ipython-input-9-e85af3dd3041> in <module>
106 # select.select_by_value("12")
107 
--> 108 select = Select(driver.findElement(By.xpath('//*[@id="ExpirationDateMonth"]')));
109 select.deselectAll();
110 select.selectByVisibleText("Value12");
AttributeError: 'WebDriver' object has no attribute 'findElement'

I tried the code you sent me in my code:
```python
select = Select(driver.find_element_by_xpath('//*[@id="ExpirationDateMonth"]'))
select.select_by_visible_text('12')


[1]: https://i.stack.imgur.com/5pkgd.png
select.select_by_visible_text('12')
print select.first_selected_option.text if you want it's value 

要选择选项12并使用您选择的选项,请先按其文本值抓取该选项,然后打印所选选项。

相关内容

最新更新