Selenium python нажать enter

selenium keyboard

Selenium webdriver can enter keypresses or type on any webpage. Selenium is the Python module to automate web browsers. The web driver is connected to both the web browser and the Python code.

The selenium webdriver starts the browser, the browser loads the webpage, selects the textbox and types.

Related course:

keyboard

selenium keyboard

To use keypress in selenium, first you need to import some stuff from the selenium module:

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

In the example below, a web browser is started. Then it searches for an HTML element by its id (elements often have a unique id). We grab the html element by its unique identifier like this:

input=browser.find_element_by_id(«searchInput»)

Then the method .send_keys() is used to type into the element. Don’t forget to also send the enter or return key if necessary.

input.send_keys(«Python»)
input.send_keys(Keys.ENTER)

The selenium keyboard code example below does all that. In this example it does an automated searh on wikipedia, but the principle works on any site.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time

Читайте также:  Python cyrillic to latin

browser=webdriver.Firefox()
try:
browser.get(«https://en.wikipedia.org»)
print(browser.title)
input=browser.find_element_by_id(«searchInput»)
input.send_keys(«Python»)
input.send_keys(Keys.ENTER)
wait=WebDriverWait(browser,10)
wait.until(EC.presence_of_element_located((By.ID,«content»)))
print(browser.title)
#print(browser.get_cookies())
#print(browser.page_source)
time.sleep(10)
finally:
browser.close()

Источник

Python code to Press enter key using selenium

We will discuss three ways or techniques using which we can press enter or return key in Selenium webdriver using Python. To make this topic easily understand, we will develop a python code to automate a google search using a Firefox browser, where the user searches a particular keyword and press enter or return key using Selenium Keys. All three ways use the same procedure except how we pass the information to send keys to press enter using Selenium Webdriver.

Steps to follow for automating Enter Key using Selenium and Python.

  • Import webdriver, options, WebDriverWait, expected_conditions, By Keys module from Selenium Package
  • Open a Firefox instance using Webdriver
  • Make a GET Request for https://google.com
  • Maximize Window
  • Find the XPATH for the Google search bar Input: //*[@id=”tsf”]/div[2]/div[1]/div[1]/div/div[2]/input
  • Send Keys: Pass Search query string using XPATH value.
  • Send Keys: Pass Enter or Return Key using XPATH value.

Three ways to Press enter key in selenium webdriver using python

So here are the three ways we will discuss using keys.RETURN, keys.ENTER and submit()

Press Enter Key in Selenium Method 1: send_keys(Keys.RETURN)

from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep options = Options() driver = webdriver.Firefox(options=options, executable_path=r'C:\driver\geckodriver.exe') wait = WebDriverWait(driver, 10) driver.get('https://google.com') driver.maximize_window() search_query = "Python code to automate Instagram Login" xpath_value = '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input' wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).send_keys(search_query) wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).send_keys(Keys.RETURN)

Press Enter Key in Selenium Method 2: send_keys(Keys.ENTER)

from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep options = Options() driver = webdriver.Firefox(options=options, executable_path=r'C:\driver\geckodriver.exe') wait = WebDriverWait(driver, 10) driver.get('https://google.com') driver.maximize_window() search_query = "Python code yahoo login" xpath_value = '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input' wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).send_keys(search_query) wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).send_keys(Keys.ENTER)

Press Enter Key in Selenium Method 3: submit()

from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep options = Options() driver = webdriver.Firefox(options=options, executable_path=r'C:\driver\geckodriver.exe') wait = WebDriverWait(driver, 10) driver.get('https://google.com') driver.maximize_window() search_query = "Press enter key in selenium webdriver using python" xpath_value = '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input' wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).send_keys(search_query) wait.until(EC.presence_of_element_located((By.XPATH, xpath_value))).submit()

Our team loves to write in Python, Linux, Bash, HTML, CSS Grid, CSS Flex, and Javascript. We love to write technical articles.

Читайте также:  Посмотреть javascript код страницы

Currently exploring Data Science, Machine learning and Artificial intelligence.

Источник

Как нажать Enter в Selenium?

Пишу приложение для автоматизации работы с сайтом. На сайте есть чат, отправка сообщений в который происходит при нажатии кнопки Enter. Без проблем получается найти поле и передать в него значение с помощью .sendKeys(«Text»). Как имитировать нажатие Enter? Пробовал .sendKeys(Key.ENTER). Не работает. Пишу на js.

you_are_enot

Решение оказалось проще, чем предполагалось. Достаточно в конце отправляемого сообщения добавить символ переноса строки «\n»

Нужно понять, почему не работает .sendKeys(Key.ENTER), вполне возможно, что теряется фокус с инпута = неявно теряется курсор. Попробуй указать курсору на место явно. типа такого:
«`
driver.get(‘www.example.com’);
var element = driver.findElement(webdriver.By.xpath(‘//div[yourInputXpath]’));
element.sendKeys(‘your text is here’);
element.click;
element.sendKeys(Keys.ENTER);
«`
За синтаксис извини, не пишу на js.
Решение костыльное конечно, нужно смотреть почему пропадает фокусировка и переопределить событие sendKeys, например. Но если нужно, так сказать «По быстрому» то должно сработать.

Chefranov

you_are_enot

Войдите, чтобы написать ответ

Как подключить yookassa на сайте?

Источник

Оцените статью