2014-11-24 63 views

回答

1

@蘇里亞的答案是正確的。非常久遠的,你也可以嘗試下面的代碼,結果是:

String price = driver.findElement(By.xpath("//span[@class='total-line__name' and contains(text(),'Shipping')]/following-sibling::strong")).getText(); 

注: -在上面的代碼,你可以用替換的文字「運費」「小計」「納稅''總計'太,以檢索他們各自的文本。


這裏,是更新的代碼,你詢問: -

public class Testing 
{ 
public static void main(String args[]) throws InterruptedException{ 

     WebDriver driver = new FirefoxDriver(); //Opening firefox instance 

     driver.manage().window().maximize(); //maximizing window 

     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //Giving implicit timeout of 20 seconds 

     driver.get("http://testing-671.myshopify.com/"); //Navigating to url 

     driver.findElement(By.xpath("//img[@alt='testing']")).click(); //Clicking on Testing product 

     driver.findElement(By.xpath("//input[@id='add-to-cart']")).click(); //Clicking on Add to cart button 

     driver.findElement(By.xpath("//input[@id='checkout']")).click(); //Clicking on Checkout button 

     //Clearing and sending value in email 
     driver.findElement(By.xpath("//input[@id='checkout_email']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_email']")).sendKeys("[email protected]"); 

     //Clearing and sending value in first name 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_first_name']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_first_name']")).sendKeys("Test"); 

     //Clearing and sending value in last name 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_last_name']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_last_name']")).sendKeys("Test"); 

     //Clearing and sending value in address 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_address1']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_address1']")).sendKeys("Test Address"); 

     //Clearing and sending value in city 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_city']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_city']")).sendKeys("Test City"); 

     //Selecting India as Country 
     Select sel = new Select(driver.findElement(By.xpath("//select[@id='checkout_shipping_address_country']"))); 
     sel.selectByVisibleText("India"); 

     //Selecting Goa as Province 
     sel = new Select(driver.findElement(By.xpath("//select[@id='checkout_shipping_address_province']"))); 
     sel.selectByVisibleText("Goa"); 

     //Clearing and sending value in Zip 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_zip']")).clear(); 
     driver.findElement(By.xpath("//input[@id='checkout_shipping_address_zip']")).sendKeys("789088"); 

     //Clicking on Continue button 
     driver.findElement(By.xpath("//input[@value='Continue']")).click(); 

     //Waiting till the text changes from "—" to some price value. 
     new WebDriverWait(driver,10).until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//span[@class='total-line__name' and contains(text(),'Shipping')]/following-sibling::strong"), "—"));  

     //Getting the price of Shipping 
     String price = driver.findElement(By.xpath("//span[@class='total-line__name' and contains(text(),'Shipping')]/following-sibling::strong")).getText(); 

     //Printing price of shipping 
     System.out.println("The price of Shipping is: "+price); 
} 
+0

嘿子,我試過你的代碼,但得到相同的「java.lang.NullPointerException」錯誤。 可以喲給我你運行的測試用例 – Ram 2014-11-25 08:48:16

+0

@ user3159596:我已經添加了上面的代碼。請檢查。 – Subh 2014-11-25 10:25:44

+0

嘿Subh,感謝您的代碼幫助。但我無法理解這段代碼。 所以我發佈了一個新的問題,我從selenium IDE獲得了完整的代碼,這裏是鏈接。給出格式,請幫助我解決這個問題。請編輯相同的代碼,讓我運行它們並獲得期望的結果。(能夠打印運費,即Rs 8) http://stackoverflow.com/questions/27124598/ – Ram 2014-11-25 10:52:52

1

試試這個代碼:

//Get the rate - updated with currect xpath 
    String rate = driver.findElement(By.xpath("//strong[@data-checkout-total-shipping-target]")).getText(); //shipping-option__price 
    //Print it in Eclipse console 
    System.out.println("Rate is " + rate); 
+0

感謝這個答案。但是我無法打印運費 – Ram 2014-11-24 11:07:45

+0

您可以發佈您的html代碼嗎?或者這是公共網站? – Surya 2014-11-24 11:12:31

+0

請在以下網址查看此測試網站:testing-671.myshopify.com 1.將測試產品添加到購物車2.轉到結帳頁面3.輸入所有詳細信息4.輸入地址後點擊「繼續」您重定向到下一頁,此頁面上出現運費。現在我想打印這個運輸價格。 – Ram 2014-11-24 11:54:50

相關問題