selenium with python action chains not working - python

I am trying to send a ALT+ESC command to my selenium chrome-driver to send it to the back of all other windows
This is the relevant code
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_ALT, Keys.ESCAPE)
actions.perform()
this is not working please help

To press key-combo:
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).key_down(Keys.LEFT_ALT).send_keys(Keys.ESCAPE).key_up(Keys.LEFT_ALT).perform()
But seems this is an OS-level keys combination for work with windows and this will not work in the selenium context.
Selenium actions applied to web page elements, fire some events inside browser.

ultimately I couldn't find a way to execute a OS command in selenium.
The following code has the same functionality but dose not necessarily run on the browser
from pyautogui import hotkey
hotkey('altleft', 'esc')

Related

Why are Selenium Chromedriver shortcuts not working?

I am using chromedriver.exe for Chrome version 80, and chrome version 80.0.3987.149. My selenium is up to date. I'm trying to send shortcuts to the browser in python and nothing is working. I've tried ActionChains, and elem.send_keys(). No shortcuts I've used have worked and I can't figure this out.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import pyautogui
driver = webdriver.Chrome(executable_path=r'path/to/exe')
driver.get('https://www.google.com')
time.sleep(8)
elem = driver.find_element_by_xpath("//body")
elem.send_keys(Keys.CONTROL,'t') #Opens a new tab
This doesn't work! I've tried an ActionChain approach and that doesn't work either. Any help would be awesome.

How to control WINDOW download dialga-box open/save/cancel with Selenium in Python

Seems download dialog-box is not a DOM , thus
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
dr = webdriver.Ie(executable_path="IE.exe")
# EXAMPLE -- download ZIP Jun 20,2018
dr.get('https://usda.library.cornell.edu/concern/publications/j098zb09z?locale=en')
download_CSV =driver.dr.dr.find_element_by_css_selector('#j6731631h > div').click()
# Find the download dialog-box and click save but no function in python selenium.
How to apply these function in python through what kind of library ?
using System.Windows.Forms;
SendKeys.SendWait(#"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
SendKeys.SendWait(#"{Enter}");
Explanation from CheryJose clearly tell dialog-box is not one of the DOM
Selenium Web Driver Cancelling Firefox Downloads
How to combine with those script in Python ?
to control the download dialog-box -- these dir & click OK
Please refer to this thread, you could use the send_keys method and Keys.ENTER:
from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("element_id").send_keys(Keys.ENTER)
Here are some related articles, you could refer to them:
Python selenium.webdriver.common.keys.Keys.ENTER() Examples
ENTER key press using Selenium WebDriver with python

Selenium Webdriver: how do respond on a prompt

I try to set up a test for a webpage using Selenium WebDriver and Python. Therefore I start the Docker image selenium/standalone-firefox.
Within this test normally a JavaScript written prompt pops up and want to receive an entry prior I can click OK.
But how can I interact with this prompt and the OK button?
On Selenium IDE the recorder uses answer on next prompt for that. How to do this with Python-Selenium? If Python does not support an corresponding command, how do I get the needed information to do the same with the available commands?
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.firefox.options import Options
# connect to docker Selenium Server
options = Options()
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=options.to_capabilities()
)
driver.get("https://www.ecalc.ch/motorcalc.php?hacker&lang=en&weight=4500&calc=auw&motornumber=1&warea=60&elevation=300&airtemp=25&motor=hacker&type=2|a60-7xs_v4_28-pole&gear=1&propeller=apc_electric&diameter=18&pitch=10.0&blades=2&batteries=topfuel_light_4500mah_-_30/45c&s=8&esc=master_spin_160_pro&cooling=good")
print(driver.title)
driver.find_element_by_id("modalConfirmOk").click()
driver.find_element_by_name("btnCalculate").click()
driver.find_element_by_id("AddCSV").click()
????
You must handle the prompt alert. Try it with:
driver.switchTo().alert().sendKeys("Your project name");
You can handle using alert class in selenium.
#Switch the control to the Alert window
obj = driver.switch_to.alert
time.sleep(2)
#Enter text into the Alert using send_keys()
obj.send_keys('test')
refer this link selenium

Send key input to HTML5 game using Selenium

I am trying to use Selenium to send key inputs to an HTML5 game I created using Phaser. However, I am quite puzzled why I can't get it to work. The same code works when I try on 2048 or other websites like google, python etc (but not on other HTML5 games built via phaser). Any tips or pointers would be super useful!
Below the python code:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("https://dry-anchorage-61733.herokuapp.com/") #this is game link
#driver.get("https://gabrielecirulli.github.io/2048/") #works for 2048
actions = ActionChains(driver)
for _ in range(6):
actions.send_keys(Keys.ARROW_UP).perform()
time.sleep(1)
actions.send_keys(Keys.ARROW_LEFT).perform()
time.sleep(1)
Looks like selenium doesn't focus on your app.
Try clicking on the element and chain with sending keys
element = driver.find_element_by_tag_name("canvas")
actions.click(element).key_down(Keys.ARROW_LEFT).perform()
This worked for me

Send Ctrl+f python selenium chrome

I am able to use some keys like Tab
browser.find_element_by_tag_name('body').send_keys(Keys.TAB)
But not Ctrl+f or Ctrl+p
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL +'f')
I also tried using xpath find_element_by_xpath & send_keys(Keys.CONTROL,'f') but still not working
You shall try by using action_chains in selenium webdriver
from selenium.webdriver.common.action_chains import ActionChains
#
# Your code
#
browser.find_element_by_tag_name('body')
ActionChains(browser).send_keys(Keys.CONTROL, "f").perform()
To be honest I have not known the answer before. I searched and run code using selenium.webdriver.common.action_chains but failed.
Then after more research, I was suggested to use pyautogui library. I don’t know how, but it worked for me… you can try the code.
# Import necessary libraries
from selenium import webdriver
import pyautogui
# to get the broswer
driver = webdriver.Chrome(executable_path="C:\Windows\chromedriver_win32 (1)\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.google.com/")
# code to perform ctrl+F
pyautogui.hotkey('ctrl', 'f')

Categories

Resources