Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要使用Selenium WebDriver识别唯一字段_Selenium_Selenium Webdriver - Fatal编程技术网

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

需要使用Selenium WebDriver识别唯一字段,selenium,selenium-webdriver,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"

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

   <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是与搜索框相关的字段。我尝试使用class,name,但它说找不到元素,数字id也是动态的。请让我知道在这种情况下使用什么。

使用xpath

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


应该可以通过类名找到这个元素

在ruby中: driver.find_元素:类,搜索字段

或在java中:
driver.findElementBy.classNamesearch-field

以下xpath应该可以工作:

//div[@id='search']//textarea[@class='search-field']
如果只有一个,则可以使用搜索

driver.findElement(By.tagName("textarea"));

是黑体字吗?请更正您的问题。输入您如何使用类进行识别的代码。该页面上是否存在多个类似的文本区域?我更新了整个代码@NadunIf。如果下面的答案对您没有帮助,您可以发布一个指向显示此问题的活动页面的链接?同样的问题。它说org.openqa.selenium.NoSuchElementException:无法定位元素:{method:xpath,selector://textarea[@class='search-field']}xpath//div[@id='search']//textarea[@class='search-field']]使用了哪些内容?正如错误消息所说,似乎已经使用了By.xpath//textarea[@class='search-field'].org.openqa.selenium.NoSuchElementException:无法定位元素:{method:xpath,selector://div[@id='search']//textarea[@class='search-field']}@mfsiCan您可以打印以下内容:System.out.printlndriver.findElementsBy.xpath//div[@id='search']//textarea[@class='search-field‌​'].在这里调整输出的大小并粘贴?我尝试了这个…它在consoleorg.openqa.selenium.NoSuchElementException中不起任何作用:找不到元素:{method:xpath,selector://textarea[@class='search-field']}添加显式等待并重试。很可能元素未随页面加载一起加载。我添加了此sleep Thread.sleep1000;。仍然相同。@Nadunorg.openqa.selenium.NoSuchElementException:无法找到元素:{方法:标记名称,选择器:textarea}这是在任何框架下吗?我不这么认为。用整个正文更新了代码。你能提供URL吗?如果可能的话,我可能无法找到元素搜索字段
driver.findElement(By.tagName("textarea"));