2017-07-26 184 views
-4

我已經編寫了一個腳本來測試一個網站,並且我已經從所有元素的google chrome中取得了Xpath,並且我想在Microsoft Edge上測試它,但是每當我運行它時,我得到一個org.openqa.selenium.remote.ProtocolHandshake createSession錯誤。所以我的猜測是它無法在Edge上找到元素。來自Chrome的XPath無法在Microsoft Edge上工作

public class testing 
{ 
    ExtentHtmlReporter htmlreporter = new ExtentHtmlReporter("D:\\Selenium\\test_report.html"); 
    ExtentReports extent = new ExtentReports(); 
    ExtentTest test; 
    JavascriptExecutor jse; 
    WebDriver driver; 

    @BeforeTest 
    public void setup() 
    { 
     System.setProperty("webdriver.edge.driver", "D:\\Selenium\\MicrosoftWebDriver.exe"); 

     driver = new EdgeDriver(); 
     driver.manage().window().maximize(); 
     extent.attachReporter(htmlreporter); 

     jse = (JavascriptExecutor)driver; 
     extent.setSystemInfo("Browseer", "Microsoft Edge"); 
     extent.setSystemInfo("OS", "Windows 10"); 

     driver.get("https://www.hoteltreeoflife.com/reservation"); 
    } 
    @Test 
    public void test1() 
    { 
     test = extent.createTest("Test Case 1 : Selecting the dates", "Clicking check availabiluty with default dates"); 

     test.info("Finding Check Availability"); 
     driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/button")).click(); //<-- This is the element 
     test.info("Clicks on Check Availability"); 

     String title = driver.getTitle(); 
     Assert.assertTrue(title.contains("Choose Your Room")); 
     test.info("Successfully selected dates"); 
    } 
} 

我也找不到在Edge上獲取Xpath的方法。

編輯:

這是完全錯誤:

[TestNG] Running: 
    Command line suite 

[VerboseTestNG] RUNNING: Suite: "Command line test" containing "1" Tests (config: null) 
[VerboseTestNG] INVOKING CONFIGURATION: "Command line test" - @BeforeTest newpackage.testing.setup() 
[16:46:55.985] - Listening on http://localhost:4848/ 
Jul 26, 2017 4:46:57 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFO: Detected dialect: OSS 
[VerboseTestNG] PASSED CONFIGURATION: "Command line test" - @BeforeTest newpackage.testing.setup() finished in 4794 ms 
[VerboseTestNG] INVOKING: "Command line test" - newpackage.testing.test1() 
[VerboseTestNG] FAILED: "Command line test" - newpackage.testing.test1() finished in 303 ms 
[VerboseTestNG] java.lang.AssertionError: expected [true] but found [false] 
[VerboseTestNG]  at newpackage.testing.test1(testing.java:53) 
[VerboseTestNG] INVOKING CONFIGURATION: "Command line test" - @AfterMethod newpackage.testing.check(org.testng.ITestResult)(value(s): [TestResult name=test1 status=FAILURE method=testing.test1()[pri:0, instance:[email protected]] output={null}]) 
[VerboseTestNG] PASSED CONFIGURATION: "Command line test" - @AfterMethod newpackage.testing.check(org.testng.ITestResult)(value(s): [TestResult name=test1 status=FAILURE method=testing.test1()[pri:0, instance:[email protected]] output={null}]) finished in 462 ms 
[VerboseTestNG] INVOKING CONFIGURATION: "Command line test" - @AfterTest newpackage.testing.close() 
[16:47:03.511] - Stopping server. 
[VerboseTestNG] PASSED CONFIGURATION: "Command line test" - @AfterTest newpackage.testing.close() finished in 3788 ms 
[VerboseTestNG] 
[VerboseTestNG] =============================================== 
[VerboseTestNG]  Command line test 
[VerboseTestNG]  Tests run: 1, Failures: 1, Skips: 0 
[VerboseTestNG] =============================================== 

=============================================== 
Command line suite 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 

Java Result: 1 
+2

什麼是完整的錯誤?它甚至不能連接到Edge。 –

+0

我不認爲它與查找元素有關。 – NarendraR

+0

@TamasHegedus它連接到Edge並打開URL,但是當點擊元素'driver.findElement(By.xpath(「/ html/body/div [1]/div [3]/div [2]/div/div/form/div [4]/button「))。click();' –

回答

1

這裏是回答你的問題:

點擊該按鈕Check availability,而不是絕對路徑您使用的是:

driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/button")).click(); 

考慮使用以下唯一的邏輯xpath

driver.findElement(By.xpath("//button[@class='btn btn-primary btn-block']")).click(); 

讓我知道如果這個回答你的問題。

0

我認爲你需要做的就是使用明確的等待,如果問題還沒有解決,就應該解決問題。

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""/html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/button""))); 
element.click(); 

使用它,而不是尋找元素,更容易變成到一個方法,所以你可以調用它時。希望它有幫助