在Django webtest中查找同名的多个单选按钮

时间:2022-09-03 16:51:15

I'm writing some web tests with the Django webtest where I'm trying to set one radio button in a pair of radio buttons to be checked. Naturally, they both have the same name attribute value.

我正在用Django webtest编写一些web测试,我试图在一对单选按钮中设置一个单选按钮来进行检查。当然,它们都有相同的name属性值。

Using pdb, and assuming I have a form variable whose type is webtest.forms.Form, here's what I see if I type form.html, i.e. what Webtest has been handed by beautifulsoup (only part of it, but the relevant part).

使用pdb,假设我有一个表单变量,它的类型是webtest.forms。表单,这是我输入表单时看到的。html,也就是说什么Webtest是由beautifulsoup提供的(只提供一部分,但相关部分)。

<input name="is_external" type="radio" value="False"/>
<input name="is_external" type="radio" value="True"/>

Normally to select a specific input element which shares a name with other input elements I'd do something like form.get('is_external', index=1) which would in this example select the input whose value is True.

通常,要选择与其他输入元素共享名称的特定输入元素,我需要做一些类似表单的事情。get('is_external', index=1)在本例中,它将选择值为True的输入。

However, when I do this I'm merely given the error message:

然而,当我这样做时,我得到的只是一个错误信息:

*** IndexError: list index out of range.

***索引错误:列表索引超出范围。

form.get('is_external') on its own will only give me the first one. I've checked the docs and they are sadly lacking when it comes to radio buttons.

get('is_external')本身只会给出第一个。我已经检查了文档,在单选按钮的时候他们很遗憾。

Am I missing something? I've used this method with type="checkbox" elements before and I've had no problems.

我遗漏了什么东西?我以前在type="checkbox"元素中使用过这个方法,我没有遇到任何问题。

1 个解决方案

#1


1  

A colleague gave me some information that led to figuring out an answer.

一个同事给了我一些信息,让我找到了答案。

It turns out that according to Webtest, Radio buttons are seen as a single item in the Form API. So in order to select another one, you need to use:

根据Webtest,单选按钮被视为表单API中的一个项目。因此,为了选择另一个,您需要使用:

form['is_external'].select('True')

That way, the second input in my example is given checked="checked". So it's not possible to select a specific radio button in the Webtest Form class.

这样,我的示例中的第二个输入就会被检查="checked"。所以不可能在Webtest表单类中选择一个特定的单选按钮。

#1


1  

A colleague gave me some information that led to figuring out an answer.

一个同事给了我一些信息,让我找到了答案。

It turns out that according to Webtest, Radio buttons are seen as a single item in the Form API. So in order to select another one, you need to use:

根据Webtest,单选按钮被视为表单API中的一个项目。因此,为了选择另一个,您需要使用:

form['is_external'].select('True')

That way, the second input in my example is given checked="checked". So it's not possible to select a specific radio button in the Webtest Form class.

这样,我的示例中的第二个输入就会被检查="checked"。所以不可能在Webtest表单类中选择一个特定的单选按钮。