Selenium - 无法选择带id的输入框

时间:2015-11-11 10:04:11

标签: html css selenium xpath

我的HTML

<td>
  <iframe id="Name_IF" style="left: auto; top: auto; width: 100%; height: 22px; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; margin-top: auto; margin-right: auto; margin-bottom: auto; margin-left: auto; vertical-align: bottom; border-top-color: rgb(112,112,112); border-right-color: rgb(112,112,112); border-bottom-color: rgb(112,112,112); border-left-color: rgb(112,112,112); border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; float: none; display: inline; position: static; background-color: white;"
  src="/a.rapidspellweb?t=r&n=blank.html" tabindex="0" frameborder="0">
    <html spellcheck="false">

    <head>

      <body class="RS_SingleLineTB" style="color: rgb(33,33,33); line-height: normal; letter-spacing: normal; overflow: hidden; clear: none; font-family: MS Shell Dlg; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: 400; vertical-align: auto; border-top-width: medium; border-right-width: medium; border-bottom-width: medium; border-left-width: medium; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; float: none; list-style-type: disc; list-style-position: outside; list-style-image: none; page-break-before: auto; page-break-after: auto; cursor: auto; unicode-bidi: normal; direction: ltr; ruby-align: auto; ruby-position: above; ruby-overhang: auto; layout-grid-char: none; layout-grid-line: none; layout-grid-mode: both; layout-grid-type: loose; overflow-x: hidden; overflow-y: hidden; zoom: normal; layout-flow: horizontal; writing-mode: lr-tb; caption-side: top; box-sizing: content-box; orphans: 2; widows: 2; page-break-inside: auto; empty-cells: show; background-image: none; background-attachment: scroll; background-repeat: repeat; background-position-x: 0%; background-position-y: 0%; background-color: rgb(255, 255, 255);"
      contentEditable="true" onload="loaded=true;">
        <nobr/>
<input name="Description" id="Name" style="width: 100%; display: none;" onchange=" HE(event,this,'applylogic');" ChangeMessage="applylogic" jQuery17108595534879847912="5" rsw_extension="[object Object]" spellchecktimeoutid="21" />

我需要在输入字段中输入文本,但无法找到带ID的输入字段。在IE中使用开发者工具时,当我选择时,

<input name="Description" id="Name" style="width: 100%; display: none;" onchange=" HE(event,this,'applylogic');" ChangeMessage="applylogic" jQuery17108595534879847912="5" rsw_extension="[object Object]" spellchecktimeoutid="21" />

没有任何内容突出显示。输入字段在选择时突出显示,

<body class="RS_SingleLineTB" style="color: rgb(33,33,33); line-height: normal; letter-spacing: normal; overflow: hidden; clear: none; font-family: MS Shell Dlg; font-size: 10pt; font-style: normal; font-variant: normal; font-weight: 400; vertical-align: auto; border-top-width: medium; border-right-width: medium; border-bottom-width: medium; border-left-width: medium; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; float: none; list-style-type: disc; list-style-position: outside; list-style-image: none; page-break-before: auto; page-break-after: auto; cursor: auto; unicode-bidi: normal; direction: ltr; ruby-align: auto; ruby-position: above; ruby-overhang: auto; layout-grid-char: none; layout-grid-line: none; layout-grid-mode: both; layout-grid-type: loose; overflow-x: hidden; overflow-y: hidden; zoom: normal; layout-flow: horizontal; writing-mode: lr-tb; caption-side: top; box-sizing: content-box; orphans: 2; widows: 2; page-break-inside: auto; empty-cells: show; background-image: none; background-attachment: scroll; background-repeat: repeat; background-position-x: 0%; background-position-y: 0%; background-color: rgb(255, 255, 255);"
contentEditable="true" onload="loaded=true;">
HTML structure(in Developer tools on selecting the input field this code gets selected) 使用ID找不到元素。如何使用xpath找到输入字段?

如果我的问题有任何错误,请原谅我。

1 个答案:

答案 0 :(得分:1)

我正在使用Selenium的Java绑定

您需要先切换到iframe

WebElement iframe = driver.findElement(By.id("Name_IF"));
driver.switchTo().frame(iframe);

WebElement inputElement = driver.findElement(By.id("Name"));