Unable to invoke firefox headless - python

I would like to know why i'm getting the following error message and how can i fix it:
Traceback (most recent call last):
File "teste.py", line 30, in <\module>
main()
File "teste.py", line 24, in main
driver = connectFirefox(defineOptions())
File "teste.py", line 18, in connectFirefox
driver = webdriver.Firefox(firefox_options=options)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/firefox
/webdriver.py", line 125, in init
if options.binary is not None:
AttributeError: 'Options' object has no attribute 'binary'
My code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.chrome.options import Options
def defineOptions():
options = Options()
options.add_argument("--headless")
return options
def connectChrome(options):
chromeDriverPath = "/usr/bin/chromedriver"
driver = webdriver.Chrome(chromeDriverPath, chrome_options=options)
print("Chrome Headless Browser Invoked")
return driver
def connectFirefox(options):
driver = webdriver.Firefox(firefox_options=options)
print("Firefox Headless Browser Invoked")
return driver
def main():
#driver = connectChrome(defineOptions())
driver = connectFirefox(defineOptions())
driver.get("https://www.archlinux.org/")
print("Headless Browser closing")
driver.quit()
#------------------------------------------------------------------------#
main()
What I'm trying to do is write a code where I can easily choose Chrome or Firefox headless.

I solved my problem. Here the code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
def connectChrome():
options = ChromeOptions()
options.add_argument("--headless")
chromeDriverPath = "/usr/bin/chromedriver"
driver = webdriver.Chrome(chromeDriverPath, chrome_options=options)
print("Chrome Headless Browser Invoked")
return driver
def connectFirefox():
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)
print("Firefox Headless Browser Invoked")
return driver
def main():
#driver = connectChrome()
driver = connectFirefox()
driver.get("https://www.archlinux.org/")
print("Headless Browser closing")
driver.quit()
#------------------------------------------------------------------------#
main()

Related

Failed to read marionette port(Selenium-py+Ubuntu+Geckodriver)

Im running a tiny script(See below).
Every time I run it I get: selenium.common.exceptions.TimeoutException: Message: Failed to read marionette port
I'm on Ubuntu 22, running Selenium 4, using Selenium-py. Any idea why this could be happening?
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service
def _make_options() -> webdriver.FirefoxOptions:
options = webdriver.FirefoxOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--remote-debugging-port=0")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--disable-gpu-sandbox")
options.add_argument("--single-process")
return options
def _initialize_geckodriver() -> webdriver.Firefox:
options = _make_options()
driver = webdriver.Firefox(
service=Service(GeckoDriverManager().install())
,options=options,
)
return driver
_initialize_geckodriver()

How to use Selenium with Firefox Proxy in Selenium 4.x

I need to pass through options as profiles are deprecated.
I am trying to use FF to proxy to a secure browser. However, I ran into a problem, which is described below:
My code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.proxy import Proxy, ProxyType
socks = '127.0.0.1:9050'
profile_path = '~/Library/Application\ Support/TorBrowser-Data/Browser/tjv18qgq.default'
capabilities = webdriver.DesiredCapabilities.FIREFOX
proxy = Proxy({
"proxyType": 'manual',
"httpProxy": socks,
"sslProxy": socks,
"noProxy": ''
})
proxy.add_to_capabilities(capabilities)
options = FirefoxOptions()
options.headless = False
options.set_preference('profile', profile_path)
options.set_capability("proxy", proxy)
service = Service(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service, options=options)
driver.get('https://check.torproject.org/')
element = driver.find_element(By.TAG_NAME, 'h1')
if element.text == 'Sorry. You are not using Tor.':
print('Not connected')
driver.close()
My output:
====== WebDriver manager ======
Current firefox version is 95.0
Get LATEST geckodriver version for 95.0 firefox
Driver [/Users//.wdm/drivers/geckodriver/macos/v0.30.0/geckodriver] found in cache
Traceback (most recent call last):
File "/Users//dev/awesomeCRM/Backend/octopus-py/main.py", line 23, in <module>
driver = webdriver.Firefox(service=service, options=options)
File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 179, in init
RemoteWebDriver.__init__(
File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 268, in init
self.start_session(capabilities, browser_profile)
File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 356, in start_session
w3c_caps = _make_w3c_caps(capabilities)
File "/Users//dev/awesomeCRM/Backend/octopus-py/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 102, in _make_w3c_caps
if caps.get('proxy') and caps['proxy'].get('proxyType'):
AttributeError: 'Proxy' object has no attribute 'get'
FirefoxProfile() have been Deprecated and with selenium4 to use a custom profile you have to use an instance of Options.
The configurations which was earlier set through profile.set_preference() now can be set through options.set_preference() as follows:
from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference('network.proxy.socks_remote_dns', False)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()
tl; dr
Setting a custom profile
Outro
using http proxy with selenium Geckodriver

Error with selenium for send_keys : str' object has no attribute 'send_keys'

I am going to scrape with selenium; I followed the instruction but I have an error regarding "send_key" when I am going to send my username and password :
runfile('C:/Users/thmag/untitled3.py', wdir='C:/Users/thmag')
C:\Users\thmag\untitled3.py:20: DeprecationWarning: use options instead of chrome_options
driver= webdriver.Chrome(driver_path, chrome_options = options)
Traceback (most recent call last):
File "C:\Users\thmag\untitled3.py", line 30, in <module>
user_ele.send_keys('MyEmail#gmail.com')
AttributeError: 'str' object has no attribute 'send_keys'
My code is as follow:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
driver_path = r"C:\\Program Files (x86)\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(driver_path, chrome_options = options)
url= "https://healthunlocked.com/"
driver.get(url)
loginpage= driver.find_element_by_id("sitebar-login-button")
loginpage.send_keys(Keys.ENTER)
user_ele = driver.find_element_by_xpath('//*[#id="email"]')
user_ele.send_keys('MyEmail#gmail.com')
user_ele.send_keys(Keys.ENTER)
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.send_keys('MyPass')
pass_ele.send_keys(Keys.ENTER)
time.sleep(10)
driver.quit()
Clear the placeholder, then send keys :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(options = options)
url= "https://healthunlocked.com/"
driver.get(url)
# closing cookies thing
driver.find_element_by_id('ccc-notify-accept').click()
loginpage= driver.find_element_by_id("sitebar-login-button").click()
time.sleep(3)
user_ele = driver.find_element_by_id('email')
user_ele.clear()
user_ele.send_keys('MyEmail#gmail.com')
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.clear()
pass_ele.send_keys('MyPassword')
# submitting
driver.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/div/section/div[1]/form/button').click()
time.sleep(10)
driver.quit()
if you just want to login, you can use requests to send a post request
Use options instead of chrome options and set user_ele.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver= webdriver.Chrome(driver_path, options = options)
url= "https://healthunlocked.com/"
driver.get(url)
loginpage= driver.find_element_by_id("sitebar-login-button")
loginpage.send_keys(Keys.ENTER)
user_ele = driver.find_element_by_xpath('//*[#id="email"]')
user_ele.send_keys('MyEmail#gmail.com')
user_ele.send_keys(Keys.ENTER)
pass_ele = driver.find_element_by_xpath('//*[#id="password"]')
pass_ele.send_keys('MyPass')
pass_ele.send_keys(Keys.ENTER)

AttributeError: 'Options' object has no attribute 'self' error using ChromeOptions for headless Google Chromethrough Selenium Python

So I've been trying to get headless chrome working for days. I have no idea whats wrong !! I've tried everything I can find in the forums relating to the issue.
Right now this is the code im running (it's a direct snippet from someone else's tutorial which works fine for them):
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
browser_name = "chrome"
if browser_name == 'chrome':
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
start_url = "https://google.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
driver.quit()
When I run that code I receive the following error
driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 64, in __init__
desired_capabilities = options.self.to_capabilities()
AttributeError: 'Options' object has no attribute 'self'
It might be worth knowing that the chromedriver is in the correct path, I know this because when I run:
browser_name = "chrome"
if browser_name == 'chrome':
driver = webdriver.Chrome(r"./chromedriver")
start_url = "https://google.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
driver.quit()
This works fine
There are two distinct approaches. If you are using:
options = webdriver.ChromeOptions()
Using only:
from selenium import webdriver
is sufficient.
But if you are using the following import:
from selenium.webdriver.chrome.options import Options
You have to use an instance of Options() to set the headless property as True as follows:
options = Options()
options.headless = True
driver = webdriver.Chrome(executable_path=r"./chromedriver", options=options)

How to hide and then open browser in selenium python?

I need to hide browser do some actions and then open browser in selenium python?
some code:
driver = webdriver.Chrome('./chromedriver') # connecting driver
options.add_argument('headless') # that's how I hide browser
driver = webdriver.Chrome(chrome_options=options)
driver.get("google.com")
and now I need to open browser for user
You wont able to do it with your current code as your have initiated chromedriver in headless mode and your browser simulation program that does not have a user interface.Also your url is not corrent in above example. Try below code
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=r" path of chromedriver.exe",chrome_options=options)
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
base = "https://www.google.com/"
driver.get(base)
Output:
Another example
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
headless_page = "https://www.google.com/"
driver.get(headless_page)
url = driver.current_url
print(url) # print headless url
time.sleep(2)
driver = webdriver.Chrome() # reset headless to false
driver.get(url)

Categories

Resources