Chrome中的XPath无法在Microsoft Edge上运行

时间:2017-07-26 11:13:37

标签: java selenium microsoft-edge

我已经编写了一个脚本来测试一个网站,我已经从谷歌浏览器中获取了所有元素的Xpath,我想在Microsoft Edge上测试它,但每当我运行它时,我得到一个org.openqa.selenium.remote.ProtocolHandshake createSession错误。所以我的猜测是它无法在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");
    }
}   

我也找不到在边缘上获取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:newpackage.testing@59e84876] 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:newpackage.testing@59e84876] 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 个答案:

答案 0 :(得分: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();

如果这回答你的问题,请告诉我。

答案 1 :(得分: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();

使用它而不是查找元素,更容易变成方法,以便您可以随时调用它。希望它有所帮助