Java 来自Chrome的XPath在Microsoft Edge上不起作用
我编写了一个脚本来测试一个网站,我从google chrome获取了所有元素的Xpath,我想在Microsoft Edge上测试它,但每当我运行它时,我都会得到一个Java 来自Chrome的XPath在Microsoft Edge上不起作用,java,selenium,microsoft-edge,Java,Selenium,Microsoft Edge,我编写了一个脚本来测试一个网站,我从google chrome获取了所有元素的Xpath,我想在Microsoft Edge上测试它,但每当我运行它时,我都会得到一个org.openqa.selenium.remote.ProtocolHandshake createSession错误。所以我猜它无法找到边缘上的元素 public class testing { ExtentHtmlReporter htmlreporter = new ExtentHtmlReporter("D:\\S
org.openqa.selenium.remote.ProtocolHandshake createSession
错误。所以我猜它无法找到边缘上的元素
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");
}
}
以下是你问题的答案: 要单击按钮
检查可用性
,而不是您使用的绝对路径:
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();
如果这能回答您的问题,请告诉我。我认为您需要做的是使用显式等待,如果尚未解决问题,则应修复该问题
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();
使用它而不是查找元素,这样更容易转换为方法,所以您可以随时调用它。希望有帮助完全错误是什么?它甚至无法连接到Edge。我认为这与查找元素无关。@TamasHegedus它与Edge连接并打开URL,但当点击元素
driver.findElement(By.xpath(“/html/body/div[1]/div[3]/div[2]/div/div/div/form/div[4]/按钮”)时,测试失败。单击()代码>@Tuks抱歉,我忘了发布整个错误。我现在已经发布了完整的错误。因此错误实际上与org.openqa.selenium.remote.ProtocolHandshake createSession
无关。我建议首先在Edge中打开页面,并手动检查该元素的存在。
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();