2016-12-28 58 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)。 –