1

我正在使用 webdriver 进行自动化,每个问题都有一些单选按钮,单选按钮旁边有一些文本,例如是/否。以下是我通过检查元素得到的代码

<label class="fs-508-label" for="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio_com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620">   

<input name="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio" id="editTemplate-editForm-content-flowTemplateFull-genericPage-_id162-page_0-profilerQuestionnaireBlock-_id167-page__1-questionnaire-_id169-0-questionRadio_com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620" value="com.taleo.functionalcomponent.prescreening.entity.question.PossibleAnswer__78620" type="radio" class="possibleanswers" onclick="onChoicePicked(this)"> 

No

</label>

你能帮我弄清楚吗?我目前能够使用单选按钮

element = browser.find_element_by_name(name got form above)

但是 element.text 并没有给我“不”而是空的,这让我感到困惑。我可能在这里遗漏了一些东西。

4

1 回答 1

1

您的元素是空的,这是正确的行为。问题是,通过执行 browser.find("idblahblah") 您只需获得单选按钮元素本身,而不是文本,因为这是其他元素。文本本身是可以通过执行 driver.findElement(By.className("fs-508-label")) 找到的元素,但这不是最佳解决方案,因为类值往往会发生变化。我建议将 html 改成这样:

<input id="true_radio_button" type="radio" name="bar"
value="bar_radio_true" checked="checked"/> 
<label id="whatever" for="true_radio_button">True</label>

这样您现在可以使用 ID 轻松找到单选按钮,然后找到相应的标签。可以通过检查“for”属性是否等于单选按钮 ID 来检查它是否正确 - label.getAttribute("for") 等于 radio_button_ID

编辑:使用这个: driver.findElement(By.className("fs-508-label")) 应该可以正常工作

于 2012-08-30T10:15:56.493 回答