java
  • selenium
  • xpath
  • 2016-05-03 69 views 0 likes 
    0

    我將嘗試使用JUnit和Selenium測試此網站: https://www.oanda.com/currency/converter/兩個不同的元素具有相同的xpath?

    我試圖從「貨幣無我有」,以及「貨幣我想要的」選擇單位。然後我發現xpaths是一樣的。只有「貨幣我有」代碼可以成功運行。 「我想要的貨幣」總是失敗。

    的XPath是driver.findElement(By.xpath("//span[text() = 'GBP']")).click();

    可能有人在幫助呢?謝謝。

    代碼1:

    public class Currency_I_Have { 
        WebDriver driver = new FirefoxDriver(); 
    
        @Before 
        public void setUp() throws Exception { 
         driver.manage().window().maximize(); 
         driver.get("https://www.oanda.com/currency/converter/"); 
        } 
    
        @Test 
        public void test() { 
         driver.findElement(By.id("quote_currency_input")).click(); 
    
         driver.findElement(By.xpath("//span[text() = 'GBP']")).click(); 
         WebElement Amount = driver.findElement(By.id("quote_amount_input")); 
         Amount.clear(); 
         Amount.sendKeys("100"); 
        } 
    } 
    

    代碼2:

    public class Currency_I_Want { 
        WebDriver driver = new FirefoxDriver(); 
    
        @Before 
        public void setUp() throws Exception { 
         driver.manage().window().maximize(); 
         driver.get("https://www.oanda.com/currency/converter/"); 
        } 
    
        @Test 
        public void test() { 
         driver.findElement(By.id("base_currency_input")).click(); 
    
         driver.findElement(By.xpath("//span[text() = 'GBP']")).click(); 
         WebElement Amount = driver.findElement(By.id("base_amount_input")); 
         Amount.clear(); 
         Amount.sendKeys("200"); 
        } 
    } 
    

    回答

    2

    我指望該網頁匹配了XPath 4個元素。 (雖然進一步檢查看起來你可以在每一對中使用,因爲它們是愚蠢的。)你需要做的是找到你想要的特定跨度的唯一父元素。例如,兩個獨特匹配的元素也可以更獨特通過引用:

    //div[@id='quote_currency_selector']//span[text()='GBP'] 
    

    (我認爲這是一個你想要的) 另一種可能更獨特通過引用:

    //div[@id='base_currency_selector']//span[text()='GBP'] 
    

    我從下拉菜單中的「GBP」條目中「高於」XML樹的「祖先」DIV獲得了「引用貨幣選擇器」和「基本貨幣選擇器」位。

    +0

    工作正常。非常感謝。 – robertredrain

    相關問題