How can you input text into an input element without using send keys in Python Selenium?
Question:
I’m working with a website where send_keys sometimes works and sometimes doesn’t (I have no idea why). Sometimes it sends keys then I try a few hours later and it doesn’t work anymore. I was wondering if anyone knew a way to send text/numbers into a input element WITHOUT using send_keys.
For example, would it be possible to input text into the following element without using send_keys:
<input _ngcontent-coh-c546="" type="text" name="tag" maxlength="25" placeholder="Tags" class="ng-dirty ng-valid ng-touched">
Any help would be greatly appreciated. Thank you.
Answers:
This should work effectively and faster compared to send_key
from selenium import webdriver
driver = webdriver.Chrome('chromedriver')
driver.get("http://www.google.com");
driver.execute_script("document.getElementsByName('q')[0].value='What ever you want to be typed here'")
I also have similar requirement but the text I want to pass is lengthy string, I implemented as below:
Solution 1:
def enter_text_js(self, data):
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='" + data + "'")
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='" + data + "';")
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='data'")
None of the above line worked and text box is empty. In the dev console though "document.getElementsByClassName(‘ck-tag’)[0]" returns proper element.
Solution 2:
xpath = '//div[@aria-label="Rich Text Editor, main"]//p'
element = self.driver.find_element(By.XPATH, xpath)
element.send_keys("Hello")
# Above line works
self.driver.execute_script('arguments[0].value=" ' + data + ' " ;', element)
# This line fails and does not enter any text.
Any idea how to fix this issue?
I’m working with a website where send_keys sometimes works and sometimes doesn’t (I have no idea why). Sometimes it sends keys then I try a few hours later and it doesn’t work anymore. I was wondering if anyone knew a way to send text/numbers into a input element WITHOUT using send_keys.
For example, would it be possible to input text into the following element without using send_keys:
<input _ngcontent-coh-c546="" type="text" name="tag" maxlength="25" placeholder="Tags" class="ng-dirty ng-valid ng-touched">
Any help would be greatly appreciated. Thank you.
This should work effectively and faster compared to send_key
from selenium import webdriver
driver = webdriver.Chrome('chromedriver')
driver.get("http://www.google.com");
driver.execute_script("document.getElementsByName('q')[0].value='What ever you want to be typed here'")
I also have similar requirement but the text I want to pass is lengthy string, I implemented as below:
Solution 1:
def enter_text_js(self, data):
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='" + data + "'")
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='" + data + "';")
self.driver.execute_script("document.getElementsByClassName('ck-tag')[0].value='data'")
None of the above line worked and text box is empty. In the dev console though "document.getElementsByClassName(‘ck-tag’)[0]" returns proper element.
Solution 2:
xpath = '//div[@aria-label="Rich Text Editor, main"]//p'
element = self.driver.find_element(By.XPATH, xpath)
element.send_keys("Hello")
# Above line works
self.driver.execute_script('arguments[0].value=" ' + data + ' " ;', element)
# This line fails and does not enter any text.
Any idea how to fix this issue?