Selenium action 'move_to_element' doesn't work in Safari because of usupported 'pause' command - selenium

The next command is failed on Safari browser during automation testing:
ActionChains(driver).move_to_element(searchInput).perform()
Exception:
InvalidArgumentException: Message: Encountered key input source with
invalid 'value' in payload: {actions = ({duration = 0;type =
pause;});
id = key;
type = key;}
The whole refined test example:
def test_safari2(self):
driver = webdriver.Safari()
driver.get('https://www.wikipedia.org')
locator = (By.ID, 'searchInput')
# 1. the line below is passed
searchInput = WebDriverWait(driver, timeout=30).until(expected_conditions.visibility_of_element_located(locator))
# 2. the line below is failed in Safari, but passed in Chrome, FF
ActionChains(driver).move_to_element(searchInput).perform()
However! If self.w3c_actions.key_action.pause() is commented inside action move_to_element(), then the whole Action chains works!
def move_to_element(self, to_element):
"""
Moving the mouse to the middle of an element.
:Args:
- to_element: The WebElement to move to.
"""
if self._driver.w3c:
self.w3c_actions.pointer_action.move_to(to_element)
# self.w3c_actions.key_action.pause()
else:
self._actions.append(lambda: self._driver.execute(
Command.MOVE_TO, {'element': to_element.id}))
return self
The similar situation with other actions.
My question is:
Is it is known limitation of Safari? And therefore ActionChais command could not be use for Selenium + Safari? Or there is some configuration pecularity?
My test runner configuration:
OS: Mac HighSierra 10.13.6
Safari 12.0 (13606.2.11)
Selenium: 3.14.1
Python: 2.7.14
Safari is started with w3c capabilities and protocol (i.e. driver.w3c=True)
Issue background:
I have an enough developed framework with a lot of actions and tests that work Ok for Chrome and Firefox. Now I'm trying to extend coverage for Safari browser also. So, that is why I'm searching for solution for not working ActionChains

Workaround by wrapping ActionChains class so that key_action.pause is not used (which does not seem to serve any important purpose):
import selenium.webdriver
class ActionChains(selenium.webdriver.ActionChains):
def __init__(self, driver):
super(ActionChains, self).__init__(driver)
if driver.name in ('Safari', 'Safari Technology Preview'):
self.w3c_actions.key_action.pause = lambda *a, **k: None

Related

Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12

I'm attempting to replicate this code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://inventwithpython.com')
try:
elem = browser.find_element_by_class_name(' cover-thumb')
print('Found <%s> element with that class name!' % (elem.tag_name))
except:
print('Was not able to find an element with that name.')
But it keeps returning the exception. I'm running this on mac w. vscode and there are few things off.
find_element_by_class_name method doesn't seem to register as a method.
Everytime I run this Intellicode prompts get disabled
I can't run this at all on Chrome as it crashes the chrome browsers
I've also searched online for driver issues and have done webdriver with the chrome driver path. Didn't work either
This is the error I'm getting if run it without try and except.
elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
Selenium removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* ...
You now need to use:
driver.find_element("class name", "VALUE_OF_CLASS_NAME")
Eg:
driver.find_element("class name", "cover-thumb")
But if a class name has multiple segments, use the dot notation from a CSS Selector:
driver.find_element("css selector", "img.cover-thumb")

Send a numpad key (with Num Lock off)

I want to test how my app reacts to numpad keys. I found in https://www.w3.org/TR/webdriver/ specs that for example for Numpad Home (with location = DOM_KEY_LOCATION_NUMPAD = 3) a symbol \uE057 should be used. However, it doesn't work for me: I get Home with default location (0), moreover, event.code is empty. It gives me a different result when I physically press NUMPAD7 button with Num Lock off: it that case, I get correct location 3 and event.code is Numpad7.
var options = FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
var driver = FirefoxDriver(options);
driver.navigate().to("https://keycode.info/");
driver.findElementByTagName("body").sendKeys("\uE057");
So how can I send such a key? I'm now thinking of manual recording of generated events when I physically press a key, and then sending these events via Selenium's execution of JS script. However, I haven't tried it yet; maybe there is a better way to do it in Selenium; maybe there is another framework that allows it better.
By the way, I've filed a similar ticket in geckodriver because it looks like a bug of webdriver to me...
\ue01d is the unicode for NUmberpad3
python code:
from selenium import webdriver
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.get("https://keycode.info/")
driver.find_element_by_tag_name("body").send_keys("\ue01d")
input()
you can use https://www.javadoc.io/doc/org.seleniumhq.selenium/selenium-api/latest/org/openqa/selenium/Keys.html to send keys instead of sending unicode directly
In firefox this is will work if you use action chain:
ActionChains.send_keys("\ue01d").perform()

Selenium Webdriver: NameError name 'firefox' is not defined

I'm trying to write this code using Selenium and Python:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://demo.guru99.com/test/login.html')
from selenium.webdriver.common.keys import Keys
email = firefox.find_element_by_xpath('//*[#id="email"]')
email.send_keys('user#gmail.com')
passwd = firefox.find_element_by_xpath('//*[#id="passwd"]')
passwd.send_keys('123456')
But I don't understand why this error occured, please help me
In your code, you are making a an instance of the webdriver.Firefox() object and calling it browser
browser = webdriver.Firefox()
Later on in your code, you try to find an element by xpath using a varible called firefox
email = firefox.find_element_by_xpath('//*[#id="email"]')
The problem is that that firefox was never actually created, I think what you meant to do is
email = browser.find_element_by_xpath('//*[#id="email"]')
You are defining the variable browser as a webdriver object. You then attempt to use firefox as a webdriver object, but you never defined the variable firefox. This is one way to solve your problem. You're better off calling the variable browser, rather than firefox, because then you can just change the browser type in your first line of code and the code will not be confusing.
browser = webdriver.Firefox()
browser.get('http://demo.guru99.com/test/login.html')
from selenium.webdriver.common.keys import Keys
email = browser.find_element_by_xpath('//*[#id="email"]')
email.send_keys('user#gmail.com')
passwd = browser.find_element_by_xpath('//*[#id="passwd"]')
passwd.send_keys('123456')

How to click on "Launch Application" dialog in Firefox with Selenium?

I have a Selenium test, in which I need to click on a "cliclient://" link, and that link needs to open an application. Now, I need to create a new profile for each test, and I don't know how to bypass the "Launch Application" dialog that appears when clicking on the link:
Here's a snippet of the test that I've created:
profile = Selenium::WebDriver::Firefox::Profile.new
profile.secure_ssl = false
profile.assume_untrusted_certificate_issuer=true
profile["plugin.default.state"] = 2
profile["plugin.state.java"] = 2
profile["browser.download.folderList"] = 2
profile["browser.download.manager.alertOnEXEOpen"] = false
profile["browser.download.manager.closeWhenDone"] = true
profile["browser.download.manager.focusWhenStarting"] = false
profile["browser.download.manager.showWhenStarting"] = false
profile["browser.helperApps.alwaysAsk.force"] = false
profile["browser.helperApps.neverAsk.saveToDisk"] = 'application/x-msdownload,application/octet-stream, application/x-msdownload, application/exe, application/x-exe, application/dos-exe, vms/exe, application/x-winexe, application/msdos-windows, application/x-msdos-program'
profile["gfx.direct2d.disabled"] = true
profile["layers.acceleration.disabled"] = true
What is it in the profile that I need to set, to bypass the dialog, or to somehow click on OK when this dialog appears?
You can also try using SikuliX http://sikulix.com/ which is an automation software which uses images to recognise the GUI elements on which certain actions need to be performed
Hovever to use it with ruby you will most probably need to compile and run a java class via a system command and also you will need JDK installed on the machine where the automation will be performed
Use C# to access the Win32 API and find the handle of the window with the title "Launch Application'. You'll need to use this, as the window is controlled by the OS and therefore Selenium can't interact with it. Then use the same API to click the cancel button (find its identifying properties using WinSpy)
Sorry if this isn't a full answer but I couldn't merely comment as I have insufficient rep at the moment.
The idea is creating a profile with default schemes you want and let selenium initialize by copying this profile. The key file that contains default schemes is handlers.json rather than prefs.js
Below is the proof of concept in python.
import json
import os
from pathlib import Path
from selenium import webdriver
# %APPDATA%\Mozilla\Firefox\Profiles\oa6m3bc6.default\Preferences\handlers.json
SCHEME_NAME = 'xxxxx'
PROFILE_DIR_NAME = 'xxxxxx'
handlers = {
'defaultHandlersVersion': {'en-US': 4},
'schemes': {SCHEME_NAME: {'action': 4}}
}
profile_dir = Path(os.environ['APPDATA']) / f'Mozilla/Firefox/Profiles/{PROFILE_DIR_NAME}'
profile_dir.mkdir(exist_ok=True)
with open(profile_dir / 'handlers.json', 'w+') as f:
json.dump(handlers, f)
profile = webdriver.FirefoxProfile(profile_dir)
driver = webdriver.Firefox(profile)
driver.get('http://example.com/')
Tested with Firefox 71.0, geckodriver 0.26.0 (e9783a644016 2019-10-10 13:38 +0000), Python 3.7, and Windows 10

Selenium's WebDriver.execute_script() returns 'None'

My program is having trouble getting an existing class from a webpage using Selenium. It seems that my WebDriver.execute_script function is not working.
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
My program returns 'None,' which tells me that the javascript function is not returning a value/being executed. Chrome's web dev tools tell me that 'content' is certainly a valid class name. The webpage isn't even dynamically generated (my eventual goal is to scrape dynamic content, which is why I make my WebDriver wait for 30 seconds before running the script.)
Return the value:
content = browser.execute_script("return document.getElementsByClassName('content');");