2017-04-12 228 views
1

我有一個Selenium Webdriver JUnit Java測試,我無法在Drupal 8表(也有樣式)中成功使用定位器。在Firefox中,我可以使用Firepath和Xpath檢查器來確定並檢查xpath確定。但是,當我運行JUnit 4測試時,無法找到它。有趣的是,當我第一次運行這個測試時,它運行正常,然而之後的很多嘗試都不會運行。Selenium Webdriver - 無法找到元素

我已經在這個網站上成功地使用了xpath等許多其他的測試,但由於某種原因,我無法得到這個工作。

其實無法定位元素消息是爲下一個命令;

driver.findElement(By.id("edit-application-status-1")).click(); 

但是我知道這是在以前的命令(如下)失敗,因爲它從來沒有點擊去這個頁面。

失敗代碼;

driver.findElement(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td[2]/a")).click(); 

也試過;

driver.findElement(By.cssSelector("a[href='/application-19']")).click(); 

我試圖改變到cssSelector與顯式等待如下

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href='/application-19']")));

滾動到視圖中,如下;

WebElement element1 = driver.findElement(By.cssSelector("a[href='/application-19']")); 
jse.executeScript("arguments[0].scrollIntoView(true);", element1); 
+0

分享網站的URL或HTML代碼 –

+0

並請分享webdriver的初始化代碼,看看你所使用的執行 –

回答

0

處理Web表格可能會很棘手,所以最好遍歷WebTable的所有元素;然後檢查,如果你的元素已經被發現,然後觸發你的行動:

List<WebElement> tableElements = driver.findElements(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td")); 


     for(WebElement item : tableElements){ 

     System.out.println("item text : " + item.getText()); 

      if(item.getText().equals("Text of your <a> tag")){ 

       WebElement newItem = item.findElement(By.tagName("a")); 

       newItem.click(); 

      } 
     } 
+0

感謝。我試過這個,但它也沒有工作?列表 tableElements = driver.findElements(By.xpath(「.//*[@ id ='block-ua-theme-content']/div/div/div [2 ] /表/ tbody的/ TR [1]「)); \t爲(WebElement項:tableElements){ \t如果(item.getText()等於( 「Abhimanyujeet」)。){ \t WebElement的newitem = driver.findElement(By.xpath( 「/ a」)的) ; \t newItem.click(); \t} –

+0

什麼是錯誤? – kushal

+0

無法找到元素。但是這是下一個命令。它似乎在不點擊該項目的情況下跳過此代碼。 –

0
//Click on first item in list tr[1] 
     System.out.println("Before"); 
     List<WebElement> tableElements = driver.findElements(By.xpath(".//[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]")); 

     for(WebElement item : tableElements){ 

      if(item.getText().equals("Abhimanyujeet")){ 

       WebElement newItem = driver.findElement(By.xpath("/a")); 
       System.out.println("tableElements" + tableElements); 
       System.out.println("item" + item); 

       newItem.click(); 

      } 
     } 
     System.out.println("After"); 

     driver.findElement(By.id("edit-application-status-1")).click(); 

控制檯;

之前

tableElements[EyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9- e491-4afd-9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content'] /div/div/div[2]/table/tbody/tr[1]]] 

itemEyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9-e491-4afd- 9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]] 

跟蹤;

org.openqa.selenium.NoSuchElementException: Unable to locate element: #edit\-application\-status\-1 
+0

因此錯誤是''edit-application-status-1''嘗試使用'driver.findElement(By.cssSelector(「#edit-application-status-1」))。click();' – kushal

+0

報告的錯誤是「編輯應用程序狀態-1」;然而這不是真正的問題。該元素在下一頁。問題在於這個元素; 。driver.findElement(By.cssSelector( 「一個[HREF = '/應用-18']」))點擊();永遠不會被點擊,所以它似乎跳過了這一點,並嘗試點擊「編輯 - 應用程序狀態-1」這當然將失敗,因爲我們還沒有在該頁面上。 –

+0

我認爲這是一個普遍問題。看來Selenium認爲它已經點擊了一個項目,因此移動到下一行失敗的代碼行,因爲之前的項目沒有被點擊,這意味着下一個元素當然不存在。 –