selenium 中clear 失效解决方法

2019-12-18  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读