【问题标题】:Selenium Python trying to click on Radio button error ElementNotVisibleExceptionSelenium Python 尝试单击单选按钮错误 ElementNotVisibleException
【发布时间】:2016-01-19 10:13:54
【问题描述】:

我有一个带有一些单选按钮的网页。我正在尝试单击一个单选按钮,但它没有单击并抛出

完整的错误是:

    Traceback (most recent call last):
  File "C:\Webdriver\ClearCore\TestCases\DataPreviewsPage_TestCase.py", line 48, in test_add_Lademo_CRM_DataPreviews
    data_previews_page.click_from_file_radio_button_from_options_tab()
  File "C:\Webdriver\ClearCore\Pages\data_previews.py", line 112, in click_from_file_radio_button_from_options_tab
    fromfile_radiobutton.click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 69, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 448, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 196, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: Cannot click on element

我点击按钮的方法是:

def click_from_file_radio_button_from_options_tab(self):
        fromfile_radiobutton = self.driver.find_element(*MainPageLocators.data_previews_fields_from_File_radioButton_from_options_tab_xpath)
        fromfile_radiobutton.click()
        return self

MainPageLocators 中按钮的定位器的 XPATH 是:

data_previews_fields_from_File_radioButton_from_options_tab_xpath = (By.XPATH, '//span[@class="gwt-RadioButton block"]/label[contains(text(), "From file")]/../input')

HTML 是:

<table class="gwt-DisclosurePanel gwt-DisclosurePanel-open" cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <tr>
                <td align="left" style="vertical-align: top;">
                    <div style="padding: 0px; overflow: hidden;" aria-hidden="false">
                        <div class="content" aria-hidden="false">
                            <span class="gwt-RadioButton block">
                                <input id="gwt-uid-163" type="radio" name="fields" value="on" tabindex="0" checked=""/>
                                <label for="gwt-uid-163">From file</label>
                            </span>
                            <span class="gwt-RadioButton block">
                            <span class="gwt-RadioButton GPI5XK1CET GPI5XK1CFT">
                            <input class="gwt-IntegerBox" type="text" disabled="" size="3"/>
                            <span class="gwt-RadioButton block">
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>

它不会在 HTML 中显示 hidden=True。它不应该说 Element 不可见。我不知道为什么我不能点击这个按钮。 它在网页上可见。

如何单击此单选按钮? 一些帮助表示赞赏。

谢谢, 里亚兹

【问题讨论】:

    标签: python-2.7 selenium xpath selenium-webdriver


    【解决方案1】:

    我不相信您的 XPath 是正确的。我会尝试这样的事情。我无法从有限的 HTML 中判断这是否足够独特。

    fromfile_radiobutton = self.driver.find_element_by_css_selector("input[type='radio'][name='fields']")
    fromfile_radiobutton.click()
    

    【讨论】:

    • 感谢您的建议。它不是唯一的,因为还有其他单选按钮。我已经要求开发人员为表格输入一个 ID。然后我可以从表 id 开始构建 xpath。
    • 只是为了好玩...试试这个self.driver.find_element_by_css_selector("table.gwt-DisclosurePanel.gwt-DisclosurePanel-open input[type='radio'][name='fields']")。我添加了对 TABLE 的引用,它可能是独一无二的,可以找到收音机。
    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2016-04-29
    相关资源
    最近更新 更多