Leu_Grady
Leu_Grady

Reputation: 518

How could I send keys to input tag by using selenium?

I would like to send some text to an input tag by using the following code, but failed. I think I followed the guide from the official doc1, so couldn't figure out why nothing happens. Could anyone please have a look at the code? Any advice is appreciated.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

def search(driver):
    driver.get('https://www.logseq.com')
    driver.find_element_by_link_text("Open a local folder").click()
    actions = ActionChains(driver)
    actions.key_down(Keys.CONTROL).send_keys("k").perform()
    search_bar = driver.find_element_by_tag_name("input.cp__palette-input")
    search_bar.send_keys("abc")
   
if __name__ == '__main__':    
    driver = webdriver.Chrome()
    search(driver)

Upvotes: 2

Views: 773

Answers (1)

frianH
frianH

Reputation: 7563

Use the .execute_script method, like below:

search_bar = driver.find_element_by_css_selector('input.cp__palette-input')
driver.execute_script("arguments[0].value = 'abc';", search_bar)

Upvotes: 1

Related Questions