2016-09-20 58 views
-1

怎麼可能找到一個angularJs元素的xpath?比如我發現,在我的頁面所有鏈接具有相同的XPath由於angularJs重複的項目 - >找到重複的angularJs的xpath項目

.//*[@id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a 

,但我有元素10,它們是文字differente,所以我嘗試用`」

,所以我試圖用含有,但它永遠無法找到它

.//[@id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(@aria-label='Creazione Prodotto')]"` 

我用硒,junit4,火狐的webdriver

這是我的代碼

public class PB01_TTT { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    WebElement element; 




    @Before() 
     public void setUp() throws Exception { 

    FirefoxProfile fxProfile = new FirefoxProfile(); 
    fxProfile.setPreference("browser.download.folderList", 2); 
    fxProfile.setPreference("browser.download.manager.showWhenStarting", false); 

    fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false); 
    fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
      "application/pdf, application/x-pdf, application/octet-stream"); 
    fxProfile.setPreference("pdfjs.disabled", true); 
    driver = new FirefoxDriver(fxProfile); 
    baseUrl = "https://w8aon2bpm.replynet.prv:9443"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 




    @Test 
     public void testPBO1TTT() throws Exception { 

      driver.get(baseUrl + "/ProcessPortal/login.jsp"); 
      // driver.get(baseUrl + "/ProcessPortal/dashboards/SYSRP/RESPONSIVE_WORK"); 

      driver.findElement(By.id("username")).clear(); 
      driver.findElement(By.id("username")).sendKeys("user"); 
      driver.findElement(By.id("password")).clear(); 
      driver.findElement(By.id("password")).sendKeys("password"); 





      String columnToDisplay=driver.findElements(By.xpath(".//*[@id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(@aria-label='Creazione Prodotto')]")); 


     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 



        element = (WebElement) driver.findElements(By.xpath(columnToDisplayXpath)); 

        Assert.assertNotNull(element); 

它總是給我一個notFoundElement,有什麼建議嗎?

謝謝

回答

0

如果你有10個鏈接,然後有一個高的機會,他們在即使路徑是一樣的,在這種情況下,你需要根據事情來構建一個路徑的方式都不同,是不同的。
例如:使用HREF,文本或其他任何部分。如果您需要在獲得選擇請加鏈接的HTML部分的任何幫助,不同

//a[contains(@href, 'part_of_href')] 
//a[contains(text(), 'part_of_text')] 
//a[@title='title'] 
//a[contains(@aria-label='Creazione Prodotto')] 

,您可以根據需要更改網址。

提示:避免使用絕對的XPath和屬性,不建議任何東西,他們可以改變這樣的:.//*[@id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a[contains(@aria-label='Creazione Prodotto')]" 這會給你很多的工作在未來。