2014-10-07 85 views
-2

我試圖通過我的值來獲取隨機選擇複選框與動態ID和代碼粘貼下面與相應的錯誤消息。出現如何在Selenium Webdriver中爲XPath使用單引號?

By.xpath("//div[contains(@id,'team_"+randNumber(i)+")]")) != null){ 

錯誤爲:

The given selector //div[contains(@id,'team_'2)] is either invalid or does not result in a WebElement. 

因此,上述術語應具有爲//div[contains(@id,'team_2')]

回答

0

顯示效果應該是這樣的:

By.xpath("//div[contains(@id,'team_"+randNumber(i)+"')]") 
1

這有什麼與硒有關,這是純粹是 Java String操作問題!

int rand = randNumber(i); 
String id = "team_" + rand; 
String xpathh = "//div[contains(@id, '" + id + "']"; // note the nested quotes! 
By.xpath(xpathh)); 

如果要擀成一條線這一點,複製粘貼都在一起,得出:

By.xpath("//div[contains(@id, '" + "team_" + randNumber(i) + "']")); 

一對夫婦的報價可以優化掉了:

By.xpath("//div[contains(@id, 'team_" + randNumber(i) + "']")); 
相關問題