簡體   English   中英

獲取最新信息<select>Python Selenium 下拉菜單中的值

[英]getting current <select> value from drop-down menu with Python Selenium

我正在檢查 Selenium Python 網頁上下拉字段中的選定值。 我想打印出所選值是什么。 我從打印出來的下拉列表中獲取所有值。 例如,下拉列表包含以下值:“浮點數”、“日期/時間”、“文本字符串”、“整數” 例如,所選值為“文本字符串”當我打印正在打印的值時“浮點日期/時間文本 stringInteger"

我的代碼片段是:

def get_selected_value_from_user_defined_type_dropdown(self, type):
    # Params : the selected value for the user defined type dropdown e.g. Text string
    user_defined_type_dropdown_element = self.get_element(By.XPATH, '//table[@id="data_configuration_edit_data_object_tab_details_tb_fields"]/tbody/tr[1]//td[3]//select')
    print ("------------------------------")
    print user_defined_type_dropdown_element.text
    return user_defined_type_dropdown_element.text

Get Element 在我的基類中,實現是:

# returns the element if found
def get_element(self, how, what):
    # params how: By locator type
    # params what: locator value
    try:
        element = self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e:
        print what
        print "Element not found "
        print e
        screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time.  This way the screenshot name will be unique and be able to save
        self.save_screenshot(screenshot_name)
        raise
    return element

HTML是:

<table id="data_configuration_edit_data_object_tab_details_tb_fields" class="GJPPK2LBJE border" cellspacing="0" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true">
<thead aria-hidden="false">
<colgroup>
<tbody style="">
    <tr class="GJPPK2LBCD GJPPK2LBJD" __gwt_subrow="0" __gwt_row="0">
    <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBED GJPPK2LBKD">
    <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD">
    <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBKD">
        <div __gwt_cell="cell-gwt-uid-214" style="outline-style:none;">
            <select tabindex="-1">
                <option value="Floating point">Floating point</option>
                <option value="Date/time">Date/time</option>
                <option selected="selected" value="Text string">Text string</option>
                <option value="Integer">Integer</option>
        </select>
        </div>
    </td>
        <td class="GJPPK2LBBD GJPPK2LBDD GJPPK2LBOD GJPPK2LBKD">
    </tr>
    <tr class="GJPPK2LBCE" __gwt_subrow="0" __gwt_row="1">
</tbody>
<tbody style="display: none;">
<tfoot style="display: none;" aria-hidden="true"/>

我如何打印出所選值是什么?

我嘗試使用 first_selected_option 但出現以下錯誤:

    Traceback (most recent call last):
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\TestCases\DataObjectsPage_TestCase.py", line 294, in testk_edit_Data_Objects_ACVSEQ_Is_The_Saved_Details_Present
    self.assertTrue(data_objects_edit_page.is_value_saved_from_user_defined_type_dropdown("Text string"), "Data Objects ACVSEQ type drop down does not show the expected saved value. Please see log for details")
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\Pages\data_objects_edit.py", line 140, in is_value_saved_from_user_defined_type_dropdown
    return self.get_selected_value_from_user_defined_type_dropdown(str(value)) == value
  File "C:\Webdriver\ClearCore 501 Regression Test\ClearCore 501 - Regression Test\Pages\data_objects_edit.py", line 133, in get_selected_value_from_user_defined_type_dropdown
    selected_option_element = user_defined_type_dropdown_element.first_selected_option
AttributeError: 'WebElement' object has no attribute 'first_selected_option'

我的代碼片段是:

def get_selected_value_from_user_defined_type_dropdown(self, type):
    #Params : the selected value for the user defined type dropdown e.g. Text string
    user_defined_type_dropdown_element = self.get_element(By.XPATH, '//table[@id="data_configuration_edit_data_object_tab_details_tb_fields"]/tbody/tr[1]//td[3]//select')
    selected_option_element = user_defined_type_dropdown_element.first_selected_option
    print ("------------------------------")
    print selected_option_element.text
    return selected_option_element.text

我從這篇文章中得到了 Python 使用 first_selected_option 的片段: https ://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver

為什么 first_selected_option 不起作用? 請問語法是什么?

謝謝,里亞茲

user_defined_type_dropdown_element<select>標簽,這就是為什么您在打印時獲得所有選項的原因。 如果您想要選擇的選項,請使用Select class 和first_selected_option

# initialize Select object
select = Select(user_defined_type_dropdown_element)

# to print the text
print select.first_selected_option.text

# to print the value
print select.first_selected_option.get_attribute("value")

檢查在選擇下,有 4 個選項,所以如果你想獲得每個選項的值,你必須選擇每個選項。

在你的 Xpath 中使用它作為第一個選項值

//table[@id="data_configuration_edit_data_object_tab_details_tb_fields"]/tbody/tr[1]//td[3]//select//option[1]

對於 Python-Selenium Dropdown 代碼,以下代碼可用於最初從下拉列表中獲取所選值,並通過使用 Selenium 下拉操作(如 Select_by_value 或 Select_by_index I)相應地更改值。在“dropdown_value”中下拉,稍后我們可以設置一些條件來驗證它

----------

get_value = driver.find_element_by_id("id")
dropdown_value = get_value.get_attribute("value")

----------

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM