【问题标题】:Unable to find an Xpath of an element using webdriver and xpath无法使用 webdriver 和 xpath 找到元素的 Xpath
【发布时间】:2013-05-22 20:14:00
【问题描述】:

我需要点击 username 旁边的文本框。无法使用 webdriver 找到 Xpath...我可以寻求帮助吗?


这里是 html sn-p:

<div id="form-1013-body" class="x-panel-body x-panel-body-default x-panel-body-default x-docked-noborder-top x-docked-noborder-right x-docked-noborder-bottom x-docked-noborder-left" style="padding: 0px; height: 108px; left: 0px; top: 0px; width: 376px;">
<span id="form-1013-outerCt" style="display: table; height: 100%;">
<div id="form-1013-innerCt" class="" style="display:table-cell;height:100%;vertical-align:top;padding:5px 5px 5px 5px">
<table id="textfield-1014" class="x-field x-table-plain x-form-item x-field-default x-anchor-form-item x-form-invalid" cellpadding="0" style="table-layout: auto;">
<tbody>
<tr id="textfield-1014-inputRow">
<td id="textfield-1014-labelCell" class="x-field-label-cell" width="205" valign="top" halign="right" style="">
<label id="textfield-1014-labelEl" class="x-form-item-label x-unselectable x-form-item-label-right" unselectable="on" style="width:200px;margin-right:5px;" for="textfield-1014-inputEl">Username:</label>
</td>
<td id="textfield-1014-bodyEl" class="x-form-item-body " role="presentation" colspan="1">
<input id="textfield-1014-inputEl" class="x-form-field x-form-required-field x-form-text x-form-invalid-field" type="text" autocomplete="off" name="username" aria-invalid="true">
</td>
<td id="textfield-1014-sideErrorCell" width="17" valign="middle" style="">
<div id="textfield-1014-errorEl" class="x-form-error-msg x-form-invalid-icon" style="" data-errorqtip="<ul class="x-list-plain"><li>This field is required</li></ul>">
<ul class="x-list-plain">
<li>This field is required</li>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
<table id="textfield-1015" class="x-field x-table-plain x-form-item x-field-default x-anchor-form-item" cellpadding="0" style="table-layout: auto;">
</div>
</span>

【问题讨论】:

  • 通过输入 id、用户名、类尝试。但没有复合(意思是 - 从未添加两个 xpath)..
  • WebDriverWait 等待 = new WebDriverWait(driver, 30); List 元素 = driver.findElements(By.tagName("input")); System.out.println("元素个数:"+elements.size()); for(WebElement ele : 元素){ ele.sendKeys("admin");
  • 找不到这样的元素异常
  • 当我尝试通过这个查看页面源时:
  • //System.out.println("webpage" + driver.getPageSource());--我没有看到整个页面源被吐出

标签: xpath selenium-webdriver xpathnavigator


【解决方案1】:

尝试替换您的代码

List<WebElement> elements = driver.findElements(By.tagName("input")); 

用这个:

List<WebElement> elements = driver.findElements(By.xpath("//input")); 

查看这个很酷的备忘单以获取更多选择

http://www.simple-talk.com/content/file.ashx?file=4937

【讨论】:

    猜你喜欢
    • 2011-08-14
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2013-01-21
    相关资源
    最近更新 更多