Home > OS >  Use Selenium to get input element value, if it isn't in HTML?
Use Selenium to get input element value, if it isn't in HTML?

Time:12-20

Is there any way to read the text in an input without calling to get it from HTML? In the image, I want to get whatever value I can from the input, store it, then add a number to it, but it's not in the HTML(the input just refers to "quantity"

Not represented in HTML

Is there some way I could select the input box, copy the value, then interact with the value from there?

CodePudding user response:

To print the text i.e. 1 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.validation-message-container > input[data-bind*='isForwardFreightSeller']"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='validation-message-container']/input[contains(@data-bind, 'isForwardFreightSeller')]"))).get_attribute("value"))
    
  • Note : You have to add the following imports :

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

CodePudding user response:

You should be able to use the following javascript snippet for this:

return arguments[0].value;

This is assuming you know how to execute javascript on an element.

  • Related