需要使用Selenium WebDriver识别唯一字段

时间:2014-05-13 07:49:14

标签: selenium selenium-webdriver

我需要找到唯一字段来标识搜索框并在其中输入文字。

   <style type="text/css">
</head>
<body style="min-height: 81px;">
<div class="shared-page" data-cid="view221" data-view="views/shared/Page" data-render-time="0.02">
<a class="navSkip" tabindex="1" href="#navSkip">Screen reader users, click here to skip the navigation bar</a>
<header role="banner">
<a id="navSkip"></a>
<div class="main-section-body" role="main">
<div class="timeline" data-cid="view4947" data-view="views/timeline/Master" data-render-time="0.107">
<div class="section-padded section-header">
<div class="timeline-title" data-cid="view5085" data-view="views/timeline/Title" data-render-time="0.001">
<div class="search-bar-wrapper shared-searchbar" data-cid="view4948" data-view="views/shared/searchbar/Master" data-render-time="0.104">
<form class="search-form" action="" method="get">
<table class="search-bar search-bar-primary">
<tbody>
<tr>
<td class="search-input" width="100%">
<div id="search" class="shared-searchbar-input" data-cid="view4949" data-view="views/shared/searchbar/Input" data-render-time="0.002">
<div class="search-field-background"> </div>
<div class="search-field-wrapper">
<label class="placeholder-text" for="397271.5897375417" style="display: block;">enter search here...</label>
<textarea id="397271.5897375417" class="search-field" autocapitalize="off" autocorrect="off" spellcheck="false" name="q" rows="1"></textarea>

textarea 是与搜索框相关的字段。我尝试使用类,名称,但它说无法定位元素,数字ID也是动态的。请让我知道在这种情况下使用什么。

4 个答案:

答案 0 :(得分:0)

使用xpath,

driver.findElement(By.xpath("//textarea[@class='search-field']"));

或者

driver.findElement(By.name("q"));

答案 1 :(得分:0)

应该可以按类名

找到这个元素

在红宝石中: driver.find_element(:class, "search-field")

或在java中: driver.findElement(By.className("search-field"));

答案 2 :(得分:0)

以下xpath应该有效:

//div[@id='search']//textarea[@class='search-field']

答案 3 :(得分:0)

如果<textarea>只有一个,您可以使用

进行搜索
driver.findElement(By.tagName("textarea"));