Hello ^^ I am currently coding on Selenium (python webdriver) and I would like to know the command to simulate the "Enter" touch in the script in order to validate a step. How can I do ?
Thank you ^^
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
# Navigate to url
driver.get("http://www.google.com")
# Enter "webdriver" text and perform "ENTER" keyboard action
driver.find_element(By.NAME, "q").send_keys("webdriver" + Keys.ENTER)
This can be found on their docs page. Basically you want to import Keys from selenium.webdriver.common.keys and then use element.send_keys(Keys.Enter) as needed.
source
Related
I'm new to Selenium and I'm trying to create my first automatization test where OS is gonna open Chrome browser, opens YouTube and enteres a word in the search bar.
Well, browser opens, YouTube opens, but OS doesn't enter any word.
This is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//*[#id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')
Seems like there could be 2 element in the page with the same id as search, we need to get the input element having id search.
The below code, with the changes should work fine now.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')
I'd like to change the time.sleep(8) to wait until I either click enter or click on a button using selenium. Like an input, but I don't want to write it in the python console, I instead want to press enter on the specific website.
time.sleep(8)
skicka = driver.find_element_by_xpath("//body/div[4]/div[3]/div[1]/div[1]/div[1]/form[1]/div[4]/div[1]/div[1]")
skicka.click()
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from time import sleep
from selenium import webdriver
import json
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://example.com/')
body=driver.find_element_by_tag_name('body')
driver.execute_script('''var script = document.createElement("script");
var button = document.createElement("button")
button.setAttribute("type", "button");
button.setAttribute("id", "seleniumbutton");
button.setAttribute("onClick", "myFunction()");
button.textContent="Click me to continue selenium";
script.setAttribute("id", "seleniumscript");
script.textContent=`function myFunction() {
alert("You pressed a key inside the input field");
document.querySelector("#seleniumbutton").remove();
document.querySelector("#seleniumscript").remove();
}`;
arguments[0].appendChild(button);
arguments[0].appendChild(script);
''',body)
WebDriverWait(driver,1000000000000000).until(EC.alert_is_present())
driver.switch_to_alert().accept()
input("Completed")
you can add an alert and button to the website and then wait for it . See the example. Here unless you click the button it won't execute your script
You can use a python method to check for a key press input. this might help you?
I am trying to use CTRL + S in selenium to save contents of a page but can't get anything happening. If I try to do it using my keyboard the save window opens.
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
action_chains = ActionChains(driver)
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads, download.prompt_for_download=False")
driver = webdriver.Chrome(options=options)
driver.get("https://imagecyborg.com/")
action_chains.send_keys(Keys.CONTROL).send_keys("s").perform()
The only thing that worked for me was pyautogui:
import pyautogui
pyautogui.hotkey('ctrl','s')
pyautogui.press('enter')
Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)
I'm new to selenium and trying to automate the download of some government data. When using the code below. I manage to navigate to the right page and enter the right parmeter in the form, but then can't find a way to click the 'submit' button. I've tried find_element_by_partial_link_text("Subm").click() and I've tried find_element_by_class_name on a number of class names. Nothing works. Any ideas?
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
main_url="http://data.stats.gov.cn/english/easyquery.htm?cn=E0101"
driver = webdriver.Firefox()
driver.get(main_url)
time.sleep(8)
driver.find_element_by_partial_link_text("Industry").click()
time.sleep(8)
driver.find_element_by_partial_link_text("Main Economic Indicat").click()
time.sleep(8)
driver.find_element_by_id("mySelect_sj").click()
time.sleep(3)
driver.find_element_by_class_name("dtText").send_keys("last72")
time.sleep(4)
try:
driver.find_element_by_class_name("dtFoot").click()
except:
driver.find_element_by_class_name("dtFoot").submit()
Solved my own problem, the key was using
driver.find_element_by_class_name(`dtTextBtn`)
instead of
driver.find_element_by_class_name(`dtTextBtn f10`)
The latter was what I saw in the source code, but the f10 blocked selenium.