【问题标题】:Is it possible to enter a text in a textbox without specific Element ID是否可以在没有特定元素 ID 的文本框中输入文本
【发布时间】:2013-12-04 06:49:29
【问题描述】:

我正在做一个在文本框中输入文本的测试,但它没有特定的 ID,所以每次我运行测试它都会改变。我在 java 中使用 selenium webdriver 请帮助

【问题讨论】:

    标签: java selenium selenium-webdriver browser-automation


    【解决方案1】:

    以下可以工作。你可以用谷歌搜索它们,看看它们是如何工作的。

    driver.findElement(By.id("id"));
    driver.findElement(By.cssSelector("cssSelector"));
    driver.findElement(By.name("name"));
    driver.findElement(By.linkText("linkText"));
    driver.findElement(By.partialLinkText("partialLinkText"));
    driver.findElement(By.className("className"));
    driver.findElement(By.xpath("xpath"));
    

    我相信其中一些会很有用。如果您想了解更多信息,请告诉我。 使用它们的难易程度按我提到的顺序排列。

    【讨论】:

    • 如果您知道如何使用 css,CssSelector 是迄今为止在屏幕上查找内容的最佳方法(除了 id)。它应该在该列表中更高。
    【解决方案2】:

    没有特定 ID 是什么意思?如果 id 至少部分保持不变,您可以使用 CSS 定位器

    driver.findElement(By.cssLocator("input[id*=somePartWhichNotChange]"));
    //* star means contains
    

    如果没有,那么您可以使用 cssSelector 来访问您的元素,例如 "body table input" 或使用 xpath 作为最后的手段。

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多