美文网首页
selenium 中clear 失效解决方法

selenium 中clear 失效解决方法

作者: 编程猫猫 | 来源:发表于2019-12-18 16:19 被阅读0次

1、可能没有定位到改元素上,可以先点击该元素

    driver.find_element_by_xpath(".//*[@id='spellcity']").click()
    driver.find_element_by_xpath(".//*[@id='spellcity']").clear()
    driver.find_element_by_xpath(".//*[@id='spellcity']").send_keys("嘉兴")

2、换种思路,双击该元素,选中原来的输入项

    #定位到输入框元素
    inputBox = wait.until(EC.presence_of_element_located((By.XPATH,".//*[@id='spellcity']")))
    #双击事件
    ActionChains(driver).click_and_hold(inputBox).perform()
    #输入内容
    inputBox.send_keys("嘉兴")

3、万能的js

    js = 'document.querySelector(".//*[@id='spellcity']").value="";'
    driver.execute_script(js)
image.png

相关文章

网友评论

      本文标题:selenium 中clear 失效解决方法

      本文链接:https://www.haomeiwen.com/subject/cbeunctx.html