2017-07-26 185 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(); 

使用它,而不是寻找元素,更容易变成到一个方法,所以你可以调用它时。希望它有帮助