python-2.7 - Selenium Python属性错误: 'WebElement' object has no attribute 'select_by_visible_text'

标签 python-2.7 selenium selenium-webdriver

我正在从 Selenium Python 的下拉字段中选择一个值。我收到错误:

AttributeError: 'WebElement' object has no attribute 'select_by_visible_text'

我选择下拉列表的方法是:

def select_dataset_from_dataset_dropdown(self):
    dataset_drop_down_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'search_variables_lb_datasets')))
    dataset_drop_down_element.select_by_visible_text(str("Query (Query)"))

HTML 是:

    <select id="search_variables_lb_datasets" class="gwt-ListBox marginbelow" style="display: inline;">
<option value="-">-</option>
<option value="CRM">CRM</option>
<option value="ESCR">ESCR</option>
<option value="ORCHARD">ORCHARD</option>
<option value="Edit_test">Edit_test</option>
<option value="Query (Query)">Query (Query)</option>
</select>

我想选择值“Query(查询)”

我可以选择这个值吗?

错误跟踪是:

Traceback (most recent call last):
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\TestCases\StructuredSearch_TestCase.py", line 54, in test_00001_create_search_query_dataset_for_search_and_check_search_variables_dm_query_variables_are_displayed
    search_variables_dm_query_page.select_datamap_and_dataset_from_the_dropdown()
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\Pages\Structured_Search\search_dm_query_page.py", line 90, in select_datamap_and_dataset_from_the_dropdown
    self.select_dataset_from_dataset_dropdown()
  File "C:\Webdriver\ClearCore Regression Test\ClearCore - Regression Test\Pages\Structured_Search\search_dm_query_page.py", line 79, in select_dataset_from_dataset_dropdown
    dataset_drop_down_element.select_by_visible_text(str("Query (Query)"))
AttributeError: 'WebElement' object has no attribute 'select_by_visible_text'

谢谢,里亚兹

最佳答案

select_by_visible_text() 方法可在 Select 上使用类,实例化它:

from selenium.webdriver.support.select import Select

dataset_drop_down_element = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.ID, 'search_variables_lb_datasets')))
dataset_drop_down_element = Select(dataset_drop_down_element)

关于python-2.7 - Selenium Python属性错误: 'WebElement' object has no attribute 'select_by_visible_text' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360355/

相关文章:

selenium - 使用音频时,Docker中的Firefox中的getUserMedia无法正常工作

java - 无法使用 Selenium + Java 定位类跨度元素

python-3.x - selenium.common.exceptions.ElementNotInteractableException : Message: element not interactable when clicking on an element using Selenium Python

java - 通过 Selenium 和 Java 使用 ChromeDriver 和 Chrome 在窗口之间切换时,从渲染器接收消息超时 : 300. 000 错误

python-2.7 - 使用 Python boto3 设置或修改 AWS Lambda 环境变量

python - WxPython - 清除事件队列和未决任务

python - 有谁知道将现有 csv 文件转换为 UTF-8 编码的简单函数?

ruby-on-rails - Selenium Webdriver - 刷新陈旧元素快捷方式?

python - 多参数解包

javascript - 如何使mocha中的 "it"等到 "it"内的 promise 得到解决?