send_keys doesn't send enter after input text - python

I'm trying to learn Selenium webdriver using Python. My code for test case looks:
import unittest
#import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.action_chains import ActionChains
class FindRouteToBerlin(unittest.TestCase):
# Start Firefox
def setUp(self):
self.driver = webdriver.Firefox()
def test_find_route_to_berlin(self):
driver = self.driver
# Open HERE Maps
driver.get('http://wego.here.com')
self.assertIn("HERE WeGo", driver.title)
# Go to Search field and fill with city
search = driver.find_element_by_xpath('//*[#id="searchbar"]/div/div/input')
search.send_keys('Berlin')
search.send_keys(Keys.RETURN)
# Press "Arrow"
arrow = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[6]/div/div/div[1]/div[2]/button')))
arrow.click()
# Enter address
address = driver.find_element_by_xpath('//*[#id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
address.send_keys(Keys.RETURN)
"""actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()"""
#def tearDown(self):
# self.driver.quit()
if __name__ == "__main__":
unittest.main()
First step with filling text "Berlin" and works without any problem. But when the second step comes it looks like Keys.RETURN is not executed after input "Szczecin".
I was trying to solve this problem with some ActionChains but after .perform() I have error and I've read that those problems are related to bugs in Selenium/Mozilla.
My code for ActionChains was:
actions = ActionChains(driver)
actions.move_to_element(address)
actions.send_keys(Keys.RETURN)
actions.perform()
Error: Message: POST
/session/1caed99c-1577-4f1d-804b-4ee397d8750b/moveto did not match a
known command
Any suggestions how can I solve this problem ?

You can try to select first element from drop-down suggestions instead of using Keys.RETURN:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
address = driver.find_element_by_xpath('//*[#id="itinerary_item_input_0"]')
address.send_keys('Szczecin')
first_suggestion = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[contains(#class, 'dropdown_list_item')]")))
first_suggestion.click()

Following is working for me:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
def setUp():
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
base_url = ('enter your url')
driver.get(base_url)
task = driver.find_element_by_id("some_element_id")
task.send_keys("Some_text",Keys.ENTER)
setUp()

Related

WebScraping with Selenium Python Question

I have this code :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import EdgeOptions
import os
os.environ['PATH'] += "C:\\Users\\czoca\\PycharmProjects\\pythonProject4\\chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://www.teintes.fr/")
driver.implicitly_wait(10)
myelement = driver.find_element(By.XPATH, "/html/body/div[6]/div[2]/div[1]/div[2]/div[2]/button[1]/p")
myelement = driver.find_element(By.NAME, "Carens III")
myelement1.click()
myelement.click()
Everything seems ok I am following some tutorials and documentation, for the XPATH and I tried other attributes..
But I have a pop up to consent to the web terms that i have to press Autorizate. But it wont click on it. Any ideia why?enter image description here
It is the button "Autoriser"
You can try this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
url = 'https://www.teintes.fr/'
driver.get(url)
accept_cookies_button = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Autoriser']")))
accept_cookies_button.click()
Advices:
Use recognizables selectors
It is a good tip to wait for the element you will use

unable to send information or click specific buttons on the website(selenium python)

I think this is more of my inability to read html of a specific website
I am trying to operate some things on this website:
https://fred.stlouisfed.org/series/DGS10
I am having 2 issues with it.
I tried to input the range of the date OR I also tried to put the range of the date at max by doing the either of the following(As long as I get 30 years of data or more I am ok). Below is my attempt to input a specific date and simulate pressing an enter-key on the keyboard.
range_search_bar = driver.find_element(By.XPATH, "//*[#id=\"input-cosd\"]")
range_search_bar.clear()
range_search_bar.send_keys("1980-10-10")
range_search_bar.send_keys(u'\ue007')
and below is an attempt to click the "max" date range button instead of inputting a date - I tried to do this first since this is simpler than the first code.
max_range_button = driver.find_element(By.XPATH, "//*[#id=\"zoom-all\"]")
max_range_button.click()
Unfortunately neither of them seem to change the range of the date...
I am suspecting that they are in a different iframe? But I could not find the iframe change...
I also tried to click the "download" button and then click the type "CSV(data)" button(which initiates the download) by doing the below code.
download_10_button = driver.find_element(By.XPATH, "//*[#id=\"download-button\"]/span")
download_10_button.click()
download_csv_button = driver.find_element(By.XPATH,("//*[#id=\"download-data-csv\"]"))
download_csv_button.click()
But I am getting this error : ElementNotInteractableException: element not interactable
Any ideas on what I might be doing wrong?
Thanks!
I can download file if I use time.sleep(...) because JavaScript needs time to load data and to open menu when it clicks Download
from selenium import webdriver
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
import time
url = 'https://fred.stlouisfed.org/series/DGS10'
#driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get(url)
time.sleep(5)
max_range_button = driver.find_element(By.XPATH, '//*[#id="zoom-all"]')
max_range_button.click()
time.sleep(5)
download_10_button = driver.find_element(By.XPATH, '//*[#id="download-button"]/span')
download_10_button.click()
time.sleep(2)
download_csv_button = driver.find_element(By.XPATH, '//*[#id="download-data-csv"]')
download_csv_button.click()
Or you may use waits for this.
But I still needed sleep to wait for loading all data.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
import time
url = 'https://fred.stlouisfed.org/series/DGS10'
#driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get(url)
time.sleep(5)
max_range_button = driver.find_element(By.XPATH, '//*[#id="zoom-all"]')
max_range_button.click()
time.sleep(5)
download_10_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="download-button"]/span')))
download_10_button.click()
download_csv_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="download-data-csv"]')))
download_csv_button.click()
And the same with putting date
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
#from selenium.common.exceptions import NoSuchElementException, TimeoutException
#from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
import time
url = 'https://fred.stlouisfed.org/series/DGS10'
#driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get(url)
time.sleep(5)
#max_range_button = driver.find_element(By.XPATH, '//*[#id="zoom-all"]')
#max_range_button.click()
range_search_bar = driver.find_element(By.XPATH, '//*[#id="input-cosd"]')
range_search_bar.clear()
range_search_bar.send_keys("1980-10-10")
range_search_bar.send_keys(Keys.ENTER) # u'\ue007')
time.sleep(5)
download_10_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="download-button"]/span')))
download_10_button.click()
download_csv_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="download-data-csv"]')))
download_csv_button.click()

How do I make send keys in Selenium work?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://outlook.office.com/mail/')
driver.implicitly_wait(7)
login = driver.find_element_by_name("loginfmt")
login.send_keys("emailhere")
login.send_keys(Keys.RETURN)
driver.implicitly_wait(5)
password = driver.find_element_by_name("passwd")
password.send_keys("passwordhere")
password.send_keys(Keys.RETURN)
It perfectly inputs the email and takes me to the password input screen, but at that point, it does not send the keys for the password. Any ideas on what is wrong with my code?
explicit wait is needed here. refer wait, so code will be like
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
password = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.NAME, "passwd"))
)
password.send_keys("passwordhere")
additional reference at: documentation
I have tried with below code, which is maximizing the screen, and then using Name attribute with Explicit waits, it is working fine.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get('https://outlook.office.com/mail/')
wait = WebDriverWait(driver, 20)
login_btn = wait.until(EC.element_to_be_clickable((By.NAME, "loginfmt")))
login_btn.send_keys("some name")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Next']"))).click()
password_btn = wait.until(EC.element_to_be_clickable((By.NAME, "passwd")))
password_btn.send_keys("Your password")
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium on Twitter with Python. I can't get the button with any locating elements of selenium

I am trying to click spam button with Selenium on Python. But when I go to three dotes on someone profile i can click report #thisperson then I can't click "It’s suspicious or spam" button. I'm going crazy because I tried every way for pulling button with selenium. For example, find_element_by_id, path, css selector, class name and They don't work. Please help me,show me a way, enlighten me.
My Python code for clicking button:
spam_button = browser.find_element_by_xpath('//form[#id="report_webview_form"]/button[2]/span')
here is html tags for this button :
I tried a complicated path for clicking button :
spam_button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')
I don't know how I write "document" tag. So I get the error.
I tried find_element_by_id:
spam_button = browser.find_element_by_id('spam-btn')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()
And I got the error "Unable element"
I am lost. How can I click the button with selenium ?
My whole code:
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
browser = webdriver.Chrome(ChromeDriverManager().install())
url ="https://twitter.com/ANYACCOUNT"
browser.get(url)
time.sleep(1)
browser.find_element_by_xpath("//a[#href='/login']").click()
time.sleep(1)
username = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input')
time.sleep(1)
username.send_keys("MYNICKNAME")
time.sleep(1)
password = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input')
time.sleep(1)
password.send_keys("MYPASSWORD")
time.sleep(1)
browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span').click()
time.sleep(1)
button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/div/div/div/div/div/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(button).click(button).perform()
time.sleep(1)
sikayet = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div/div[2]/div/div/div/div[2]/div[3]/div/div/div/div[5]/div[2]/div/span')
browser.implicitly_wait(5)
ActionChains(browser).move_to_element(sikayet).click(sikayet).perform()
time.sleep(1)
#spam_button = browser.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[2]/div/iframe/#document/html/body/div/div/form/button[2]/span')
#browser.find_element_by_xpath('//button[#id="spam-btn"]')
#browser.implicitly_wait(5)
#ActionChains(browser).move_to_element(spam_button).click(spam_button).perform()
wait = WebDriverWait(browser,10 )
spam_button = wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
time.sleep(1)
spam_button.click()
I tried other ways as mentioned in below comment and I got this error everytime .
Try using explicit wait instead of implicit:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "spam-btn")))
spam_button = browser.find_element_by_id('spam-btn')
spam_button.click()
OR,
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = browser.find_element_by_css_selector('#spam-btn>div')
spam_button.click()
Update:
You need to switch to iframe.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://twitter.com/apiontkovsky/status/1385614545947820037')
wait = WebDriverWait(driver, 15)
# Authorise here
# ...
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg")))
driver.find_element_by_css_selector(".css-1dbjc4n.r-18u37iz.r-15zivkp .css-1dbjc4n.r-xoduu5 svg").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Report')]")))
driver.find_element_by_xpath("//span[contains(text(),'Report')]").click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".r-1yadl64.r-16y2uox")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#spam-btn>div")))
spam_button = driver.find_element_by_css_selector('#spam-btn>div')
spam_button.click()

How to upload file using Selenium with Python

I want to attach file through below link. How can I do that using selenium python?
My code:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome(executable_path="C:\Program Files (x86)\Drivers\chromedriver.exe")
driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")
driver.maximize_window()
time.sleep(10)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
driver.find_element_by_xpath("//*[#id='content']/div[2]/div[2]/div/div/div/div[1]/div/div/div/button").click()
driver.find_element_by_xpath("//*[#id='content']/div[2]/div[2]/div/div/div/section/div/form/div[3]/div[3]/div").click()
paul, please try to run below code - I have ran the same code in my system and it is working for me.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")
# Click on Apply Button
Apply_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Apply Now']/parent::button")))
action.move_to_element(Apply_btn).click().perform()
time.sleep(2)
Upload_File = driver.find_element_by_xpath('/html/body/label/input').send_keys("YourFilePath")
Do let me know if it shows error.

Categories

Resources