How can I use touchAction with Selenium 4's w3c protocol? - python

When I use TouchActions in python, the Error is
Message: unknown command: Cannot call non W3C standard command while in W3C mode
With this, ActionChains class works well. This is my python code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions
mobile_emulation = { "deviceName": "Nexus 5" }
options = Options()
options.add_argument('--headless')
options.add_experimental_option('mobileEmulation', mobile_emulation)
driver = webdriver.Chrome(options=options)
driver.get("http://example.com/")
element = driver.find_element(By.TAG_NAME, 'body')
actions = TouchActions(driver)
actions.tap(element)
#actions.double_tap(element)
#actions.flick(10, 10)
actions.perform()
driver.quit()
selenium 4.1.0
ChromeDriver 97.0.4692.71
Google Chrome 97.0.4692.71
How can I fix it?

Related

Python Selenium - pressing javascript src button

I'm trying to click on a button that is generated using this javascript code
<script type='text/javascript' value name ="clickme"
src='//examplecode.com/example.js>
I'm using driver.findelement and it says "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable", heres my python code:
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time
options = webdriver.ChromeOptions() # Initializing Chrome Options from the Webdriver
options.add_experimental_option("useAutomationExtension", False) # Adding Argument to Not Use Automation Extension
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("excludeSwitches", ["enable-automation"]) # Excluding enable-automation Switch
#options.add_argument("disable-popup-blocking")
#options.add_argument("disable-notifications")
options.add_argument("--enable-infobars")
options.add_argument("--enable-extensions")
options.add_argument("disable-gpu")
options.add_argument("start-maximized")
prefs = {"profile.default_content_setting_values.notifications" : 1}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options,executable_path=ChromeDriverManager().install())
driver.get("http://examplesite.com/")
tabName = driver.find_element(By.NAME, "clickme")
tabName.click()
time.sleep(200)
driver.quit()
maybe have some element layer upper than click() element
try this code:
tabname=WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CLASS_NAME, 'balabala')))
driver.execute_script("arguments[0].click();", tabname)

Selenium goog:chromeOptions hash

I'm trying to run Selenium in AWS Lambda (Python) and recently learned that since Chromedriver version 2.31 they changed chromeOptions to goog:chromeOptions
https://chromedriver.storage.googleapis.com/2.31/notes.txt
https://www.selenium.dev/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html
I am running Chromium 86 with Selenium 3.14. I've been troubleshooting multiple error messages for the past hours and I suspect this is my underlying issue I just dont know how can I pass this argument to the driver or selenium. I've been trying the following with no luck
driver = webdriver.Chrome(options='goog:chromeOptions')
or
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('goog:chromeOptions')
EDIT
Here's a snippet of the code I'm using
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')
Any help is greatly appreciated
You saw it right.
You have to add the following import:
from selenium.webdriver.chrome.options import Options
Now you can add any of the arguments. As an example:
options = Options()
options.add_argument("start-maximized")
And finally:
driver = webdriver.Chrome(options=options)
driver.get("https://www.google.com/")
As an alternative,
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com/")
Update
ChromeOptions object: Most Chrome-specific capabilities are exposed through the ChromeOptions object. In some languages, this is implemented by the ChromeOptions class. In other languages, they are stored under the goog:chromeOptions dictionary in desired capabilities.
As an example, in Ruby:
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"goog:chromeOptions" => {"args" => [ "window-size=1000,800" ]})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps

Scrape google snippet text in a certain language (English) using python and selenium

I was trying to scrape the snippet text from google search page and this solution worked well. The only issue I have now is that the text is in Bangla while I want it in English.
Here's what I've tried:
options = webdriver.ChromeOptions()
options.add_argument('lang=en')
driver = webdriver.Chrome(executable_path=r'the\path\for\chromedriver.exe', options=options)
I've tried adding 'lang=en' as an argument to ChromeOptions and pass it to webdriver.Chrome(). That's all I could figure out but it's not working.
Here's the full code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
options.add_argument('lang=en')
driver = webdriver.Chrome(executable_path=r'C:\Users\camoh\AppData\Local\Programs\Python\Python38\chromedriver.exe', options=options)
driver.get('https://google.com/')
assert "Google" in driver.title
#wait = WebDriverWait(driver, 20)
#wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("when barack obama born")
input_field.send_keys(Keys.RETURN)
#wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".Z0LcW.XcVN5d")))
result = driver.find_element_by_css_selector(".Z0LcW.XcVN5d").text
print(result)
driver.close()
driver.quit()
Here's the page when I run the code:
You can try with below code to add argument with preferred language:
from selenium.webdriver.chrome.options import Options as ChromeOptions #import library
options=webdriver.ChromeOptions() #create object of ChromeOptions
options.add_argument("--lang=en")
options.add_argument("--lang=en-US")#or you can use
Use -
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})

ChromeDriver desired_capabilities has been deprecated, please pass in an Options object with options kwarg

I am getting this deprecation warning when I start my Selenium webdriver.Remote in python, my selenium version is selenium==4.0.0b2.post1
desired_capabilities has been deprecated, please pass in an Options object with options kwarg
What is that Option object supposed to be? How do I declare it?
This is my code:
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
import time
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME
)
driver.get('http://www.google.com/')
You can use Options instead of DesiredCapabilities in the following way:
from selenium import webdriver
import time
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=webdriver.ChromeOptions()
)
driver.get('http://www.google.com/')
For selenium running on MacOS you can use options like this:
from selenium import webdriver
driver = webdriver.Remote(
command_executor='http://localhost:4444',
options=webdriver.FirefoxOptions()
)
driver.get('https://google.com')
driver.quit()
For selenium running on Windows you can use options like this:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r"C:\\Program Files\\Mozilla Firefox\\firefox.exe"
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444',
options=options
)
driver.get('http://google.com')
driver.quit()
If you are using Appium automation, this worked for me:
from appium import webdriver
APPIUM = 'http://localhost:4723'
CAPS = {
'platformName': 'iOS',
'platformVersion': '16.2',
'deviceName': 'iPhone 14',
'automationName': 'XCUITest',
'browserName': 'Safari'
}
driver = webdriver.Remote(
command_executor=APPIUM,
desired_capabilities=CAPS
)
try:
driver.get('https://google.com')
finally:
driver.quit()

How to click on download icon on chrome browser using python selenium

I want to download files by clicking on Download icon on Chrome browser.
I tried several ways like Xpath and CSS but it doesn't worked. Please let me know if there is any solution on this using Python 3.x and selenium.
Below is code that I have tried,
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
class TEAutomation:
def automateTask(self):
chromeOptions = Options()
chromeOptions.add_experimental_option("prefs",{"download.default_directory": "/home/vishal/Documents/PythonProgram/"})
baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
driver.maximize_window()
driver.get(baseUrl)
driver.implicitly_wait(10)
driver.find_element(By.XPATH,'//*[#id="download"]').click()
#driver.find_element(By.CSS_SELECTOR, '#download').click()
time.sleep(5)
driver.quit()
molexAuto = TEAutomation()
molexAuto.automateTask()
Thank you in advance.
Maybe the element is still not loaded when you try to click it, try waiting for it with WebDriverWait, I don't have chrome so you will have to test this yourself:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
class TEAutomation:
def automateTask(self):
chromeOptions = Options()
prefs = {
"download.default_directory": "/home/vishal/Documents/PythonProgram/",
"plugins.always_open_pdf_externally": True
}
chromeOptions.add_experimental_option("prefs", prefs)
baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
driver.implicitly_wait(10)
driver.maximize_window()
driver.get(baseUrl)
time.sleep(5)
driver.quit()
molexAuto = TEAutomation()
molexAuto.automateTask()

Categories