2015-11-13 188 views
-1

我想獲取一些元素,但它拋出錯誤「」。我的java代碼是 -硒沒有獲取元素

driver = new InternetExplorerDriver(); 
WebDriverWait wait = new WebDriverWait(driver, 60); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("New User"))); 
driver.findElement(By.name("New User")).click(); 

而HTML代碼就像下面的層次結構。

/html/frameset/frame/html/body/table/tr/td/New User 

我使用的是Internet Explorer 8,因此無法找到xpath,因此我沒有通過xpath方法綁定。

+0

'但它拋出錯誤'..what錯誤? –

+0

請添加更多信息...如相關的HTML,更多您嘗試過的內容等。 – JeffC

回答

0

有一個iframe,你必須切換到:

driver.switchTo().frame(0); // 0 - means, switch to the first frame 

WebDriverWait wait = new WebDriverWait(driver, 60); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("New User"))).click(); 
+0

只是一個改進。 [visibilityOfElementLocated](http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOfElementLocated-org.openqa.selenium.By-)返回WebElement它正在等待,因此顯式的findElement是多餘的:'WebElement newUserLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name(「New User」))); newUserLink.click()' –

+0

@ alb-i986是的,當然,雖然這不是答案的重點:)謝謝。 – alecxe

+0

感謝它的工作原理,但我如何知道幀(0)或幀(1)。什麼是0和1 – Shik