【问题标题】:Insert value into input field using Selenium and Java使用 Selenium 和 Java 将值插入输入字段
【发布时间】:2020-10-14 13:38:49
【问题描述】:

我正在使用代码行:

driver.findElement(By.xpath("//*[@id=\"sernum\"]")).sendKeys("XXXXXXXXXX");

我需要做的就是在字段中插入一个值。但这行不通……

来自“dom”的片段:

【问题讨论】:

  • Edit 您的问题和发布错误堆栈以及您的相关 HTML?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

要在<input> 字段中发送字符序列,因为该元素是动态元素,您需要将WebDriverWait 用于elementToBeClickable(),您可以使用以下任一Locator Strategies

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#sernum[name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='sernum' and @name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多