Search code examples
seleniumqwebelement

Get current value of a webelement in selenium after clicking a button


I have a web page in which there is an input field containing text "0" (when the page loads). There is a button in the page. When I click it, there is a small function that runs and the text in the aforementioned field changes to "20". But, when I try to get the element value, I still get "0", i.e., the same value before clicking the button.

<input type="text" class="form-control" value="0" data-bind="value: ShippingTotal() == '0' ? '0' : ShippingTotal()" disabled="">

How can I extract the true value from the input field?


Solution

  • It seems that although '20' is entered into the field, the "value" attribute still remains '0'.

    Try this:

    elem = driver.find_element_by_xpath("[YOUR_PATH_HERE]")
    
    elem.get_attribute("innerHTML")
    
    print(elem)
    

    You could double click the field and copy the value.

    from selenium.webdriver.common.action_chains import ActionChains
    import pyperclip
    
    elem = driver.find_element_by_xpath("[YOUR_PATH_HERE]")
    
    actions = ActionChains(driver)
    
    actions.double_click(elem).perform()
    
    elem.send_keys(u'\ue009', 'c') # This is CONTROL + C
    
    text = pyperclip.paste()