2

How do I get the text from inputs without value attribute with Selenium? The problem is that they are filled automatically (maybe with JavaScript) when the page loads and the text doesn't appear on the html so I can't find anything that represents it.

image

2
  • Do you need a way to extract text without using value or is this rather a timing issue? Commented Aug 19, 2014 at 7:30
  • A way to extract it without using value Commented Aug 19, 2014 at 14:06

3 Answers 3

2

Using a module called win32clipboard, which is part of pywin32 solved my problem.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import win32clipboard

element.send_keys(Keys.CONTROL, 'a') #highlight all in box
element.send_keys(Keys.CONTROL, 'c') #copy

win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData() #paste
win32clipboard.CloseClipboard()

print text
Sign up to request clarification or add additional context in comments.

Comments

1

Once the text is in the box, you can access it using WebElement.get_attribute('value') where WebElement is the text box that you want to extract the text from.

Comments

0

You could implement your own expected condition. The following works

html/js:

<html>
<head>
<script type="text/javascript" >
    window.onload = function(){
        document.getElementById("string").value = "hello";
    };
</script>
</head>
<body>
        <input id="string" type="text" value="">

</body>
</html>

python:

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

class regex_to_be_present_in_element_value(object):
    def __init__(self, locator, regex):
        self.locator = locator
        self.regex = regex

    def __call__(self, driver):
        try:
            element_text = EC._find_element(driver,
                                         self.locator).get_attribute("value")
            if element_text:
                return re.match(self.regex, element_text)
            else:
                return False
        except StaleElementReferenceException:
                return False

driver = webdriver.Firefox()
driver.get("file:///path/to/the/htmlfile.html")
try:
    element = WebDriverWait(driver, 10).until(
        regex_to_be_present_in_element_value((By.ID, "string"), "he*")
    )
finally:
    driver.quit()

It simply waits until there is text in the value attribute of the specified element matching the regex generated by the regex string passed to the constructor, in this case simply "hello". "he*" matches "hello".

I used this as a guide in making the class: https://selenium.googlecode.com/git/docs/api/py/_modules/selenium/webdriver/support/expected_conditions.html#text_to_be_present_in_element_value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.