无法定位元素Selenium Webdriver中出现错误

无法定位元素Selenium Webdriver中出现错误,selenium,Selenium,我正在使用最新的webdriver版本。下面是我写的webdriver代码 Thread.sleep(5000); WebElement providerprofile = driver.findElement(By.xpath("//div[@class='provider-namebox']//a[@href='#provider-detail?anthony-firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&am

我正在使用最新的
webdriver
版本。下面是我写的
webdriver
代码

Thread.sleep(5000);
       WebElement providerprofile = driver.findElement(By.xpath("//div[@class='provider-namebox']//a[@href='#provider-detail?anthony-firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River Family Physicians@South Carolina@@@@@@@@@28.6667@77.2167&standardsearch']"));
 providerprofile.click();
下面是HTML代码片段

<div class="col-sm-12 inner-provider clearfix">
<a href="#provider-detail?anthony--firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River Family Physicians@South Carolina@@@@@@@@@28.6667@77.2167&standardsearch">
<span class="col-xs-5 col-sm-12 image-box">
<img class="img-circle" alt="" src="images/provider-image.png"/>
</span>
</a>
<div class="col-xs-7 col-sm-12 mobile-showBox">
<div class="provider-namebox">
<a href="#provider-detail?anthony-firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River Family Physicians@South Carolina@@@@@@@@@28.6667@77.2167&standardsearch">
<span class="nowrap">
<h2>Anthony </h2>
</span>
<span class="nowrap">
<h2>Firilas</h2>
, MD
</span>
</a>
<h3> N/A </h3>
</div>

不适用
现在,当我运行上面的
webdriver
code时,它给我的错误是
“无法定位元素”

我用于上面HTML的Xpath:
//div[@class='provider-namebox']//a[@href='#提供程序详细信息?anthony firilas和U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River FamilyPhysicians@South卡罗莱纳州28日。6667@77.2167&standardsearch']


即使使用相同的xpath在firebug和firepath中进行标识,也只需使用下面的代码来标识元素

WebElement providerprofile = driver.findElement(By.xpath("//div[@class='provider-namebox']/a"));
由于错误为“无法定位元素”,因此该元素可能未在指定的时间段内加载。此外,如果href是唯一的且不是“动态的”,则只能搜索href。请尝试以下代码:

WebDriverWait wait = new WebDriverWait(_driver, 60);        
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='#provider-detail?anthony-firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River Family Physicians@South Carolina@@@@@@@@@28.6667@77.2167&standardsearch']")));

使用“wait”命令,测试将等待60秒,然后出现错误退出。一旦找到元素,它将立即继续。如果您仍然收到相同的错误,则href可能不是唯一的。也许您可以按类名尝试…

问题是,在我使用webdriver执行搜索选项之后,webdriver无法定位正在发生的元素,因为我的xpath由于即将出现的动态值而不是唯一的,并且它们的xpath与我的xpath相同,所以我使用前向同级方法识别要继续识别元素的特定元素。

仍然会出现以下错误“org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“xpath”,“选择器”:“//div[@class='provider-namebox']/a”}命令持续时间或超时:187毫秒”@在查找元素之前,首先确保页面已完全加载(您可以使用JavaScript查找页面加载状态)。接下来,尝试仅使用classname识别
div
元素。如果无法识别
div
标记,请验证任何帧中是否存在
div
元素。如果您能够成功地找到
div
元素,请使用我提供的语法来查找link元素。您是否验证了您的元素是否存在于任何帧中?您是否能够使用classname成功地仅识别
div
元素?仍然收到以下错误信息“org.openqa.selenium.TimeoutException:等待by.xpath://a[@href=”#提供程序详细信息?anthony firilas&U2FsdGVkX181p0qdh48AoV8B1OV7voTYsNsYgSWYsg0=&Ashley River Family找到的元素的可见性60秒后超时。”Physicians@South卡罗莱纳州28日。6667@77.2167&standardsearch']生成信息:版本:“2.45.0”,修订版:“32a636c”,时间:“2015-03-05 22:01:35”“您是否100%确定href总是相同的?通过href查找元素不是一个好的做法。由于在60秒内找不到该元素,因此它肯定没有加载。你想点击什么?要确保页面已完全加载,请尝试以下操作:
ExpectedCondition pageLoadCondition=new ExpectedCondition(){public Boolean apply(WebDriver)_driver){return((JavascriptExecutor)_driver).executeScript(“return document.readyState”).equals(“complete”);}
还可以在页面上的其他元素上进行测试,以检查页面是否正确加载…Etiene是的,你是对的,href从来都不一样,我刚才观察到了,但现在该怎么办我已经尝试了上面的代码,但仍然给出了相同的错误,该场景就像我必须在下拉列表中给出一些输入值一样向下,然后单击匹配按钮,然后在搜索框底部,只有少数医生的个人资料会出现在同一页面中,并且在推荐的个人资料中,我需要单击其中一个……希望将此作为评论-请向未来的读者解释问题所在。
WebElement providerprofile = driver.findElement(By.xpath("//div[@class='provider-namebox']/a"));