2016-12-28 85 views
0

使用Firefox和标记在我的web应用程序的链接中的其他事情,我得到的,这个代码,我想我可以用它来caprture对象:硒和捕捉对象

cb_or_somename_someothername cb_area_0219 

此字符串是“类名”在Firebug。 要我输入脚本:

WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219")); 

但在执行时,脚本没有找到的元素。

其他onfo在Firebug的面板是:

class="cb_or_somename_someothername cb_area_0219" 


onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false" 


href="#" 


id="fwMainContentForm:j_idt156:2:selectRole" 

是我的脚本参考元素以错误的方式?

+0

您是否得到了'ElementNotFoundException'或关于复合类名称的异常? – Andersson

+0

引起:org.openqa.selenium.InvalidSelectorException:给定的选择器cb_or_somename_someothername cb_area_0219无效或不导致WebElement。发生以下错误: InvalidSelectorError:不允许使用复合类名称 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/invalid_selector_exception.html 构建信息:版本:'2.53.1',revision: 'a36b8b1cd5757287168e54b817830adce9b0158d',时间:'2016-06-30 19:26:09' –

回答

0

您不能使用复合类名称(带空格的名称)进行搜索。尝试通过CSS选择,而不是使用搜索:

WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219")); 

XPath

WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername")); 

WebElement rolle = driver.findElement(By.xpath("//*[@class='cb_or_somename_someothername cb_area_0219']")); 

而且你还可以通过以下两种类名一个使用搜索

WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part 

更新

如果您得到Element is not clickable...异常,您似乎在您尝试点击该元素时被其他元素覆盖了。因此,请尝试等待,直到此“覆盖”不再可见:

new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[@class='environtment-banner']")); 
WebElement rolle = driver.findElement(By.className("cb_area_0219")); 
rolle.click(); 
+0

评论不适合长时间讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/131898/discussion-on-answer-by-andersson-selenium-and-capture-object)。 –