【问题标题】:Changing Input field value not showing up on webpage using Selenium with Python使用 Selenium 和 Python 更改输入字段值未显示在网页上
【发布时间】:2021-01-07 18:23:50
【问题描述】:

我在网页上有一个输入字段,我想访问并设置值。为此,我有以下几点:

element = browser.find_element_by_id("input")
driver.execute_script('arguments[0].scrollIntoView()', element)
browser.wait_until_element_is_visible(element)
element.clear()
driver.execute_script(f'arguments[0].value = "{input_value}"', element)

上述方法可行,但有时在设置输入字段的值时,它实际上并没有在网页上设置它,因此输入字段将为空。但是当我输出:print(element.get_attribute('value')) 时,它返回的正是我希望输入字段具有的内容(所以按照示例,input_value)。

我想通过设置输入的 value 属性来更改 UI 上的输入字段,并且不喜欢使用sendKeys(),因为这些输入字符串很长。那么有谁知道如何让输入字段值显示在网页上?

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    要访问和设置您需要为WebDriverWait 诱导element_to_be_clickable() 的值,您可以使用以下任一Locator Strategies

    driver.execute_script('arguments[0].scrollIntoView()', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "input"))))
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "input")))
    element.click()
    element.clear()
    element.send_keys(input_value)
    

    注意:您必须添加以下导入:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2020-10-14
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多