【问题标题】:How to set value in hidden textbox using selenium java如何使用 selenium java 在隐藏的文本框中设置值
【发布时间】:2018-08-18 21:30:45
【问题描述】:

如何使用 selenium java 在隐藏的文本框中设置值。它会引发以下错误,“不得隐藏、禁用或只读元素...”。我尝试了许多方法,例如使用 sendkeys,使用 js excute,但没有任何效果。请参考我的脚本中的以下代码,

方法:1

driver.findElement(By.xpath("editbox")).sendKeys(input);

方法:2

driver.findElement(By.id("editbox")).setAttribute("value", "your value");

方法三:

WebElement Element = driver.findElement(By.id("editbox"))
js.executeScript("arguments[0].value = arguments[1];", Element, input);

【问题讨论】:

  • 你尝试让它可见吗?
  • 嗨@Andersson,我是硒新手。你能告诉我如何让它可见
  • 您可以等待visibility of elementscroll down 使其可见
  • @Andersson,我试过了。它表明该对象是可见的。单击使用 js 正在该对象中工作,但只有“设置”存在问题
  • 使用相关的 HTML 和您尝试 Automate 的确切 Manual Steps 更新问题。跨度>

标签: javascript java selenium


【解决方案1】:

这样做的快速方法是使用 js,这是使用 python 和 selenium 的答案:

    # Test that captcha response field is in the page
    captcha_response = driver.find_element_by_id(LocatorsFirstPage.FORM_CAPTCHA_RESPONSE_ID)


    # Make captcha response visible to input response
    driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')

    # Set captcha response
    driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = arguments[0]', result)
    driver.find_element_by_id("g-recaptcha-response").innerHTML=result

    print(captcha_response.get_attribute("value"))

然后您可以通过将样式设置为“display: none;”来使其不可见用js也是一样。

【讨论】:

    【解决方案2】:

    你的不可见元素是在某个时候可见还是永久不可见?如果元素在一段时间后可见,您可以等待它的可见性:

    wait.until(ExpectedConditions.visibilityofelement(by.id(...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多