Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个不同页面上的两个元素使用相同的XPath。如何在Java中使用SeleniumWebDriver访问它们?_Java_Selenium_Xpath_Webdriver - Fatal编程技术网

两个不同页面上的两个元素使用相同的XPath。如何在Java中使用SeleniumWebDriver访问它们?

两个不同页面上的两个元素使用相同的XPath。如何在Java中使用SeleniumWebDriver访问它们?,java,selenium,xpath,webdriver,Java,Selenium,Xpath,Webdriver,我正在尝试验证两个不同页面上的页眉标题 访问站点并验证页面标题 在文本字段中输入输入,然后单击提交按钮 验证页眉标题 步骤1和步骤3中的页面具有相同的头标题xpath,当我尝试访问和验证时,会收到一条错误消息: 预期(预期标题)但找到(意外标题) 代码: HTML代码的屏幕截图: 可能是时间问题,驾驶员仍在读取旧标题。您可以使用等待文本成为您正在等待的内容 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement header

我正在尝试验证两个不同页面上的页眉标题

  • 访问站点并验证页面标题
  • 在文本字段中输入输入,然后单击提交按钮
  • 验证页眉标题
  • 步骤1和步骤3中的页面具有相同的头标题
    xpath
    ,当我尝试访问和验证时,会收到一条错误消息:

    预期(预期标题)但找到(意外标题)

    代码:

    HTML代码的屏幕截图:


    可能是时间问题,驾驶员仍在读取旧标题。您可以使用等待文本成为您正在等待的内容

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement header = wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("/html/body/div[4]/div/div[1]/div/h1/span"), "expected title"));
    
    或者等待第一个标头过时

    WebElement firstHeader = driver.findElement(...);
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.stalenessOf(firstHeader));
    WebElement secondHeader = driver.findElement(...);
    
    编辑

    如果是同一个元素,则可以使用
    texttobepresentelement

    WebElement header = driver.findElement(...);
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.textToBePresentInElement(header, "expected title"));
    

    你能提供HTMLcode吗?你所说的页眉标题是什么意思?您是否指向窗口(浏览器选项卡)标题?对不起,我如何在此处添加屏幕截图。我看不到任何附件选项。“我是新的stackoverflow。@santhoshkumar我的意思是,页面顶部的标签中包含页面标题。当您对两个页面标题进行同步时,控制台中打印的内容是什么?两者是否相同?\n我尝试了第二种选择。它给出了超时错误。预期条件失败:等待元素过时并超时。@Parveen我添加了另一个选项。'WebElement header=driver.findElement(By.xpath(“/*[@id='goCompare']]/div[4]/div/div[1]/div/h1/span”);WebDriverWait wait=新的WebDriverWait(驱动程序,10);wait.until(ExpectedConditions.texttobepresentElement(标题,“开始搜索”);字符串header1=header.getText();Assert.assertEquals(标题1,“开始搜索”);System.out.println(“在步骤1页面上成功”);'谢谢我用上面的代码解决了这个问题。让我知道这是否是一个好的做法
    WebElement header = driver.findElement(...);
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.textToBePresentInElement(header, "expected title"));