【问题标题】:Selenium wont Regonize XPath that shows on devtoolsSelenium 无法识别在 devtools 上显示的 XPath
【发布时间】:2020-11-09 05:05:00
【问题描述】:

我正在尝试验证我在个人资料上发布的评论是否显示在我的墙上和个人资料供稿中。评论总是被发布;但是,硒无法识别已发布的评论。 xpaths 显示在 devtools 中,但我在 selenium 中收到以下错误:

org.openqa.selenium.TimeoutException:预期条件失败:等待代理元素的可见性。

这是 homeLocators 中的定位器:

@FindBy(how=How.XPATH ,using="(/html/body/main/div/section/div[2]/div/div[2]/div[2]/div/div/div[last()-1])/div[2]/div[2]‹")                      
public WebElement firstPostLastComment;

这是 profileLocators 中的定位器:

@FindBy(how=How.XPATH ,using="(/html/body/main/div/section/div[3]/div/div[2]/div[2]/div/div/div)[last()-1]/div[2]/div[2]")
public WebElement firstPostLastComment;

以下是我的操作文件中的函数:

public void postCommentProfile() {
    js.executeScript("window.scrollBy(0,450)", "");
    String random = UUID.randomUUID().toString();
    String randomShortened = random.substring(random.length() - 10);
    commentProfile = randomShortened;
    profileLocators.firstPostCommentInput.clear();
    profileLocators.firstPostCommentInput.sendKeys(randomShortened);
    profileLocators.firstPostCommentInput.sendKeys(Keys.ENTER);
}

public void newCommentShowsOnWallAndProfile() throws InterruptedException {
    navLocators.navProfile.click();
    js.executeScript("window.scrollBy(0,450)", "");
      new WebDriverWait(SeleniumDriver.getDriver(), 60)
      .until(ExpectedConditions.visibilityOf(profileLocators.firstPostLastComment));
    boolean flag = true;
    String expectedText = commentProfile;
    System.out.println("this is expected text in newCommentShows" + expectedText);
    String profileActualText = profileLocators.firstPostLastComment.getText();
    System.out.println("this is profile actual text in newCommentShows" + profileActualText);
     if(!profileActualText.equals(expectedText)) {
          flag = false;
          System.out.println("flag false fired");
      }
    
    navLocators.navHome.click();
    js.executeScript("window.scrollBy(0,450)", "");
      new WebDriverWait(SeleniumDriver.getDriver(), 60)
      .until(ExpectedConditions.visibilityOf(homeLocators.firstPostLastComment));
    String wallActualText = homeLocators.firstPostLastComment.getText();
    System.out.println("this is wall actual text in newCommentShows" + wallActualText);
    if(!wallActualText.equals(expectedText)) {
          flag = false;
      }
    Assert.assertTrue(flag);
}

如果我取消等待,我会收到以下错误:

Org.openqq.selenium.InvalidSelectorException:无效选择器:无法使用 xpath 表达式定位元素。

这是应用程序的链接 https://spbk.herokuapp.com/#/login

【问题讨论】:

  • 那么TimeoutExceptionInvalidSelectorException引发了哪个异常?

标签: java selenium xpath


【解决方案1】:

你可能会想出一个更好的 xpath,尽管如果你能做到的话,通过文本搜索的建议可能会更可靠。

为此,如果您的测试首先生成随机字符串然后调用

@Test public void should_post_comment()
{
  String randomCommentText = generateRandomComment();
  postCommentProfile(randomCommentText);
  newCommentShowsOnWallAndProfile(randomCommentText);
}

public String generateRandomComment()
{
  String random = UUID.randomUUID().toString();
  String randomShortened = random.substring(random.length() - 10);
  return randomShortened;
}

public void postCommentProfile(String randomShortened) {
    js.executeScript("window.scrollBy(0,450)", "");
    profileLocators.firstPostCommentInput.clear();
    profileLocators.firstPostCommentInput.sendKeys(randomShortened);
    profileLocators.firstPostCommentInput.sendKeys(Keys.ENTER);
}

但是,如果您确实无法获取文本或更改测试步骤以获取文本,您仍然可以获得具有显式定位器的元素。鉴于上面列出的示例应用程序,要获得最新帖子(顶部)的最新评论(最后),我会使用这样的 XPATH:

//div[@class='body-content-col'][1]//div[@class='post-comment-article group'][last()]

这会给你这样的东西:

@FindBy(how=How.XPATH ,using="(//div[@class='body-content-col'][1]//div[@class='post-comment-article group'][last()]")                      
public WebElement firstPostLastComment;

【讨论】:

  • 请注意,这个 XPATH 部分 //div[@class='body-content-col'][1] 说“给我第一个 div 类“body-content-col”——在换句话说,每个帖子。[1] 是第一个索引。
  • XPATH 的第二部分 //div[@class='post-comment-article group'][last()] 说“给我最后一个元素,它是一个带有类 post-评论文章”
【解决方案2】:

我建议您使用相对 xpath 而不是绝对 xpath。你可以这样做:

假设您的评论是:

“我的测试评论”

您可以通过以下方式定位:

browser.findElement(By.xpath(".//span[text()='My Test Comment']"));

这只是一个示例。您可以寻找更好的 XPath 定位器。

【讨论】:

  • cmets 是随机生成的字符串。那不行。
  • @AlecLivinghouse ,所以将你的 随机生成的字符串 放入变量中。有什么问题?
  • @JaSON 在函数 newCommentShowsOnWallAndProfile 中,我试图验证评论是否实际显示在它应该出现的位置。现在,在发布评论后选择评论的 xpath 不起作用。因此,我无法验证。
  • @AlecLivinghouse - 正如我建议的那样,您需要使用相对路径。现在我展示的只是一个例子,不一定是那样。为了确保评论确实显示在页面上您想要的部分中,我建议您在应该显示评论的地方添加一些前缀元素。如果您可以发布包含此评论元素的所需部分的完整 DOM 部分,我们将能够更好地为您提供帮助
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 2020-06-20
  • 1970-01-01
  • 2020-08-16
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多