Selenium goog:chromeOptions hash - python

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

Related

Chromedriver error on Django DigitalOcean App Platform

I'm currently developping a web app using django and the App platform on digitalocean.
My web app uses selenium and chromedriver, I have found a way to install chromedriver using python libs such as chromedriver_binary on pip but the app can't open it and throws me an error :
Message: Service chromedriver unexpectedly exited. Status code was: 127
This error most likely means that some dependencies and libs are not available and leads to the script crashing.
Here's my current code inside views.py :
from selenium import webdriver
import chromedriver_binary
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
import undetected_chromedriver as uc
from selenium.webdriver.chromium.options import ChromiumOptions
from selenium.webdriver.common.by import By
import urllib.request
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.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from googleapiclient.discovery import build
from urllib.parse import urlsplit
def get_webdriver(url):
options = webdriver.ChromeOptions()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome()
driver.get(url)
return driver
Is there a way (maybe using another lib, or a whole other solution) to deploy chromedriver on this django app?
EDIT 1 :
Seems like the app does find the chromedriver, I can print it through the code below :
def get_webdriver(url):
from selenium.webdriver.chrome.service import Service
driver_path = chromedriver_binary.chromedriver_filename
print("Driver's path : ", driver_path)
ser = Service(driver_path)
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(service=ser)
driver.get(url)
return driver
The print returns me :
/workspace/.heroku/python/lib/python3.10/site-packages/chromedriver_binary/chromedriver
It really seems like some libraries or dependencies are missing but I don't know how to install them on this app platform.
Remove the unwanted arguments unless any specific requirement:
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-gpu')
and keep only the intended one:
options.add_argument('--headless')
and execute your test.

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)

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'})

Python: Disable images in Selenium Ms.Edge (chromium) Webdriver

I have spent a long time searching for this answer. I want to open a website without loading images using Selenium Python. I'm confused about looking for variables to adjust browser settings.
this is my code that I know to set the capabilitise
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.edge.options import Options
options = Options()
options.set_capability("dom.webnotifications.enabled", 1)
capabilities = options.to_capabilities()
driver = webdriver.Edge(capabilities=capabilities)
driver.get("https://www.mywebsite.com/")
daftar = driver.find_element_by_xpath('//*[#id="header-main-wrapper"]/div[2]/div[5]/button[1]')
actions = ActionChains(driver)
actions.click(daftar).perform()
and I know that this is the code to disable images using Chromedriver
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
so, how to set edge options with selenium python. I really don't know about this.
thank you
I found the answer myself after finding msedge.selenium_tools
this is my code that actually work perfectky to block the image
from selenium import webdriver
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.action_chains import ActionChains
options = EdgeOptions()
options.use_chromium = True
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
driver = Edge(options = options)
driver.get("https://www.mywebsite.com")
daftar = driver.find_element_by_xpath('//*[#id="header-main-wrapper"]/div[2]/div[5]/button[1]')
actions = ActionChains(driver)
actions.click(daftar).perform()

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