Select HTML 内的文本 WebElement

Select WebElement by text within HTML

我已经尝试 select 这个 WebElement 一段时间了。它是我测试的 Web 应用程序首页上的一个注册按钮。我想知道,有没有一种方法可以用来在 HTML 中抓取文本来定义 WebElement?

<div>
  <div>
    <div style="">
      <div style="transform: translate(0px, 0px) scale(0.5, 0.5) rotate(0deg);">
        <div role="img">
          <img src="images/lsloginform/newskin/try_it_normal.png?1444658058414">
        </div>
      </div>
      <div style="transform: translate(0px, 75px) scale(1, 1) rotate(0deg);">
        <canvas height="7" width="75" style="top: 0px; left: 0px; width: 75px; height: 7px;"></canvas>
      </div>
      <div style="transform: translate(0px, 82px) scale(1, 1) rotate(0deg);">
        <div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN&nbsp;UP</div>
      </div>
    </div>
    <div style="display: none;"></div>
    <div style="display: none;">
      <div style="transform: translate(0px, 0px) scale(0.5, 0.5) rotate(0deg);">
        <div role="img">
          <img src="images/lsloginform/newskin/try_it_MO.png?1444665385167">
        </div>
      </div>
      <div style="transform: translate(0px, 75px) scale(1, 1) rotate(0deg);">
        <canvas height="7" width="75" style="top: 0px; left: 0px; width: 75px; height: 7px;"></canvas>
      </div>
      <div style="transform: translate(0px, 82px) scale(1, 1) rotate(0deg);">
        <div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN&nbsp;UP</div>
      </div>
      <div></div>
    </div>
    <div style="display: none;"></div>
    <div style="display: none;"></div>
  </div>
</div>

具体例子,我想把上面div的xpath利用HTML末尾的"SIGN UP"文本。我该怎么做?

我测试的应用程序非常挑剔,能够 select 通过该属性将使我的生活变得更加轻松。这是一个问题的原因是因为我需要 3 种不同的方法来获取 xpath:一种用于正常的按钮,一种用于悬停,另一种是因为单击按钮时按钮会立即发生变化。我的大部分自动化脚本看起来像:

    public class changeLanguageLogin {
        public void run(WebDriver driver) {
            WebElement changeLanguage = getWebElement(driver, "xpath", "img", "src", "select_application_language_normal");
            Actions actions = new Actions(driver);
            actions.moveToElement(changeLanguage).build().perform();
            WebElement changeLanguageMO = getWebElement(driver, "xpath", "img", "src", "select_application_language_hover");
            changeLanguageMO.click();
            WebElement changeLanguageClicked = getWebElement(driver, "xpath", "img", "src", "select_application_language_pushed");
            changeLanguageClicked.click();
        }


      private static By buildXPathExpression(String htmlElement, String attributeName, String attributeValue)
      {
          return By.xpath("//"+htmlElement+"[contains(@"+attributeName+",'"+attributeValue+"')]");
      }

      private static By buildCSSExpression(String htmlElement, String attributeName, String attributeValue)
      {
          return By.cssSelector(""+htmlElement+"["+attributeName+"='"+attributeValue+"']");
      }

      private static WebElement getWebElement(WebDriver driver, String operation, String htmlElement, String attributeName, String attributeValue)
      {
          By byObj = null;
          WebElement webElementObj = null;
          if(operation!= null)
          {
              if(operation.equals("xpath"))
              {
                  byObj = buildXPathExpression(htmlElement,attributeName,attributeValue);
              }
              else if(operation.equals("cssSelector"))
              {
                  byObj = buildCSSExpression(htmlElement,attributeName,attributeValue);
              }
          }
          if(byObj != null)
          {
              webElementObj = driver.findElement(byObj);
          }

          return webElementObj;
      }
}

一天很多很多次,一种简单地定义 xpath 的方法 "will not work" 我不得不四处寻找另一种方法。目标是不使用任何 "hard coded" xpath,因为当我开始工作时这是有问题的。

所以我正在寻找的答案:通过 HTML、"SIGN UP".

中的文本定义一个 xpath

This response will not help if you are bound to only xpath or css lookups.

选项 1

如果您愿意为您的辅助方法添加另一个案例,我认为 By.linkText 可以满足您的要求?

  /**
   * @param linkText The exact text to match against
   * @return a By which locates A elements by the exact text it displays
   */
  public static By linkText(final String linkText) {
    if (linkText == null)
      throw new IllegalArgumentException(
          "Cannot find elements when link text is null.");

    return new ByLinkText(linkText);
  }

或者可能By.partialLinkText

  /**
   * @param linkText The text to match against
   * @return a By which locates A elements that contain the given link text
   */
  public static By partialLinkText(final String linkText) {
    if (linkText == null)
      throw new IllegalArgumentException(
          "Cannot find elements when link text is null.");

    return new ByPartialLinkText(linkText);
  }

如果您针对已国际化的系统进行测试,这可能会成为一个问题。根据环境,user/browser 的区域设置会更改您要查找的文本。

如果文本发生变化,这也可能是一个维护问题。

选项 2

如果您可以控制 HTML,您可以考虑将 id 添加到页面上的元素。有了它,您就可以使用 By.id 查找。从我的一些初步研究来看,元素 id 被视为更可靠和一致的路径之一。

/**
   * @param id The value of the "id" attribute to search for
   * @return a By which locates elements by the value of the "id" attribute.
   */
  public static By id(final String id) {
    if (id == null)
      throw new IllegalArgumentException(
          "Cannot find elements with a null id attribute.");

    return new ById(id);
  }

选项 3

我的最终建议是尝试实现您自己的 By 查找。我不会建议查找的实现,但如果这种行为是您所需要的,那么您应该知道您可能会创建一个子类来执行您想要的操作。

祝你好运。

对于旧式 inlie 样式 css,您生成 css 选择器取决于为 div 编写的样式,例如 "div[style*='paste_style_attribute']"。但是这种方法是不可靠的,因为相同的样式重复。

以下div:

<div style="font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);">SIGN&nbsp;UP</div>

您可以生成以下 css 选择器:

"div[style*='font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);']"

Java代码:

driver.findElement(By.cssSelector("div[style*='font-family: franklin-gothic-ext-comp-urw; font-weight: 700; font-style: normal; font-size: 15px; opacity: 1; color: rgb(63, 63, 63); transform: translate(7px, 0px) scale(1, 1) rotate(0deg);']"))