使用python和selenium更改Input字段的值并按enter键



我试图通过改变输入字段的值从网页收集一些信息。但是每次我改变这个值,网页就会自动改变将输入框的值恢复为默认值。下面是实现:

driver = webdriver.Chrome()
page = driver.get("https://www.edmunds.com/cars-for-sale-by-owner/")
inputElement = driver.find_element_by_name("zip")
inputElement.clear()
inputElement = driver.find_element_by_name("zip")
inputElement.send_keys('23835')
inputElement.send_keys(Keys.ENTER)

如何更改值并按enter键以获得过滤后的网页?

更新! !解决方案:通过driver.execute_script("arguments[0].setAttribute('value','')",inputElement)将值修改为null,然后编写其他逻辑发送密钥。一开始它不工作,但现在它工作了。

def enter_value(zipcode):
inputElement = driver.find_element_by_name("zip")
driver.execute_script("arguments[0].setAttribute('value','')",inputElement)
print(inputElement.get_attribute('value'))
inputElement.clear()
inputElement.send_keys(zipcode)
inputElement.send_keys(Keys.ENTER)
driver = webdriver.Chrome()
drive = driver.get("https://www.edmunds.com/cars-for-sale-by-owner/")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.NAME, 'zip')))
enter_value(zipcode="23831")

请使用ctrl + a然后delete来清除输入字段,因为.clear()似乎不一致。

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.edmunds.com/cars-for-sale-by-owner/")
inputElement = wait.until(EC.visibility_of_element_located((By.NAME, "zip")))
inputElement.send_keys(Keys.CONTROL + "a")
inputElement.send_keys(Keys.DELETE)
time.sleep(2)
inputElement.send_keys('23835')

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

相关内容

  • 没有找到相关文章

最新更新