【问题标题】:Locating label element using class使用类定位标签元素
【发布时间】:2017-02-05 19:50:30
【问题描述】:

我正在尝试定位标签元素并用一些值填充它,但我无法得到它。我正在使用JavatestNGSelenium 编写以下代码。

我使用的代码如下

driver.findElement(By.className("ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-maxlength ng-touched")).sendKeys("12345");

driver.findElement(By.className("item-input-wrapper scan-input-label")).sendKeys("12345");

这是我在检查元素时收到的元素的详细信息。

【问题讨论】:

  • 您为什么要尝试更改标签?
  • @ScaryWombat :) 他不是。出于他的慷慨,他只是想给那个标签寄一些钥匙,以便在冬天看到它。像你这样的袋熊肯定会同情。干杯,伙计:)
  • @AdrianColomitchi - 这对我没有帮助。我试过了。
  • 谁能帮帮我:(

标签: java selenium selenium-webdriver css-selectors testng


【解决方案1】:

实际上 selenium 不支持使用By.className() 复合类名来定位元素。您应该尝试使用By.cssSelector() 来定位<input> 元素,如下所示:-

driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label']")).sendKeys("12345");

或更具体:-

driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label'][ng-model *= 'ctrl.currentValue']")).sendKeys("12345");

已编辑:- 如果您想使用 <label> 元素的类名来定位,请尝试如下:-

driver.findElement(By.cssSelector("label.item-input-wrapper.scan-input-label"));

或者您也可以使用 By.className() 与任何人一起找到它,如果单个类名是:-

driver.findElement(By.className("item-input-wrapper"));

【讨论】:

    【解决方案2】:

    实际上 selenium 不支持使用 By.className() 定位元素 您可以尝试使用类名的 css 语法,css=tagname.classname

    driver.findElement(By.cssSelector(input.item-input-wrapper)).sendKeys("12345");
    

    【讨论】:

    • selenium 支持 By.className() 使用其类定位元素。但这里的问题是它只接受单个类名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2022-01-13
    • 2020-04-26
    相关资源
    最近更新 更多