How to simulate pressing the enter key in Selenium?

Asked

Viewed 2,083 times

-2

I’m using Selenium and tried to locate the instagram post button, but I’m not getting my program to click it after typing something. I’ve tried something like:

driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click() 

But without answers, I wonder if simulating enter could solve this problem. If yes, how could I simulate this key?

  • If you are trying to comment on the photos try the following: driver.find_element_by_xpath("//button[@type='submit']").click() and see if it works

1 answer

0

Well , to give the enter first Voce must import from selenium.webdriver.common.keys import Keys

this package performs pressing of all special keys including Enter

after you have done this, and only send the enter key to an item on the page:

Example of a script that searches google by clicking enter instead of the search button:

    from selenium.webdriver.common.keys import Keys from selenium import webdriver
    
    
    driver = webdriver.Chrome() driver.get('https://www.google.com/') 
driver.find_element_by_xpath('/html/body/div/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input').send_keys("Enter pressionado")

    # aqui eu pressiono e botao Enter 

driver.find_element_by_xpath('/html/body/div/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input').send_keys(Keys.ENTER)

I hope I’ve solved your doubtful hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.