to_capabilities() missing 1 required positional argument: 'self' - python

import unittest
from selenium import webdriver
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
options = webdriver.FirefoxOptions
options.headless = True
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)
driver.implicitly_wait(10)
driver.get('http://amazon.de')
print(driver.title)
I get the error
to_capabilities() missing 1 required positional argument: 'self'

Use Options() from
from selenium.webdriver.firefox.options import Options
options = Options()
and not from
options = webdriver.FirefoxOptions
Or
It should be
options = webdriver.FirefoxOptions()
both works fine

I am using the below code with firefox options :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver_path = "D:\geckodriver.exe"
options = webdriver.FirefoxOptions()
options.add_argument("--disable-infobars")
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
options.add_argument("--headless")
driver = webdriver.Firefox(options = options, executable_path= driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get('http://amazon.de')
print(driver.title)
Output :
Amazon.de: Günstige Preise für Elektronik & Foto, Filme, Musik,
Bücher, Games, Spielzeug & mehr

Related

Selecting value from dropdown-box is not possible with selenium?

I try to select the value "Ukrainian Division" in the dropdown box of the following site:
https://www.cyberarena.live/schedule-efootball
with the following code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
import time
if __name__ == '__main__':
WAIT = 3
options = Options()
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
link = f"https://www.cyberarena.live/schedule-efootball"
driver.get (link)
time.sleep(WAIT)
select = Select(driver.find_elements(By.XPATH,"//select")[1])
select.select_by_visible_text('Ukrainian Division')
# select.select_by_value("1")
input("Press!")
driver.quit()
But unfortunately, nothing happens - the options are not selected with this code.
I also tried it with select_by_value with this line
select.select_by_value("1")
instead of
select.select_by_visible_text('Ukrainian Division')
but this doesn´t work either.
How can I select this option from the dropdown box?
I tried ypur code and I also could not use Selenium Select object there. I don't know why. But we still can do that directly, with regular Selenium commands.
The following code is working:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.cyberarena.live/schedule-efootball'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//select[contains(.,'Division')]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Ukrainian')]"))).click()
The result is:

selenium difference between --headerless and -silent mode

When I try selenium option argument in my PC, with either of modes (--headerless) or (--silent) it works fine. But on clients device it interrupts with strange code. I even added windows size after arguments, but error is same.Error with Feature-Policy header
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--headerless')
webdriver_service = Service("chromedriver.exe")
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
The website with selenium headless browser mode detected as bot.
To avoid detection largely depends on maximize_window_size()
In your case,It's also need to add --disable-blink-features=AutomationControlled
The following example is working fine in Headless Chrome Selenium:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.headless = True
options.add_argument("start-maximized")
#options.add_experimental_option("detach", True)
options.add_argument("--no-sandbox")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service,options=options)
url = 'https://soundcloud.com/daydoseofhouse/snt-whats-wrong/s-jmbaiBDyQ0d?si=233b2f843a2c4a7c8afd6b9161369717&utm%5C_source=clipboard&utm%5C_medium=text&utm%5C_campaign=social%5C_sharing'
driver.get(url)
cookie = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="onetrust-accept-btn-handler"]'))).click()
video_duration = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//div[#class="playbackTimeline__duration sc-text-primary sc-text-h5"]/span[2]'))).text
print(video_duration)
Output:
2:51

Selenium - Getting error page when trying to load site?

I try to load this site
https://www.pferdewetten.de/
with the following code:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from sys import platform
import os, sys
import xlwings as xw
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent
if __name__ == '__main__':
SAVE_INTERVAL = 5
WAIT = 3
print(f"Checking chromedriver...")
os.environ['WDM_LOG_LEVEL'] = '0'
ua = UserAgent()
userAgent = ua.random
options = Options()
# options.add_argument('--headless')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_argument("--disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument(f'user-agent={userAgent}')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWebDriver = WebDriverWait (driver, 10)
link = f"https://www.pferdewetten.de/"
driver.get (link)
But i allways only get this information:
Is there any way to load this site using selenium?
Possibly elenium driven ChromeDriver initiated google-chrome Browsing Context is geting detected as a bot.
To evade the detection you can make a few tweaks as follows:
Remove the --no-sandbox argument and execute as non-root user.
Remove the --disable-infobars argument as it is no more effective.
Remove the --disable-extensions argument as it is no more effective.
Add an experimental option "excludeSwitches", ["enable-automation"] to evade detection.
Add an experimental option 'useAutomationExtension', False to evade detection.
Add the argument '--disable-blink-features=AutomationControlled' to evade detection.
Effectively your code block will be:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.pferdewetten.de/")
driver.quit()

Selenium Python Paste Not Working in Headless Mode

Python selenium paste isnt working in headless mode, i tried CONTROL + V, SHIFT + INSERT, with pyperclip3, pyperclip, klembord, but nothing seems to working, here is the code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pyperclip3
import time
Desc = """<p><iframe src="https://www.youtube.com/embed/-grLLLTza6k" frameborder="0" allowfullscreen=""></iframe></p><p><img src="https://images-na.ssl-images-amazon.com/images/I/71Zxjh0AdpL.png" style="width:100%;max-width:450px;clear:both;"></p><p></p><h2>10 Undeniable Reasons People Hate cheats</h2>"""
options = Options()
options.binary_location = r'CHROME_BINARY_LOCATION'
options.add_argument("--headless")
webdriver.Chrome(executable_path=r'CHROME_DRIVER_LOCATION', options=options)
driver.implicitly_wait(10)
driver.get("http://google.com")
time.sleep(2)
pyperclip3.copy(Desc)
element = driver.find_element(By.XPATH, '//input[#name="q"]')
# element.send_keys(Keys.CONTROL, 'v')
a = pyperclip3.paste()
element.send_keys(Keys.SHIFT, Keys.INSERT)
element.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
driver.save_screenshot("image.png")
driver.close()
driver.quit()
I tried the below code with explicit waits, and it seems to do the job with pyperclip :
options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 2})
options.add_argument('--window-size=1920,1080')
options.add_argument("--headless")
options.add_experimental_option("prefs", {"profile.default_content_settings.cookies": 2})
driver = webdriver.Chrome(executable_path=driver_path, options = options)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://google.com")
wait = WebDriverWait(driver, 20)
desc = '''<p><iframe src="https://www.youtube.com/embed/-grLLLTza6k" frameborder="0" allowfullscreen=""></iframe></p><p><img src="https://images-na.ssl-images-amazon.com/images/I/71Zxjh0AdpL.png" style="width:100%;max-width:450px;clear:both;"></p><p></p><h2>10 Undeniable Reasons People Hate cheats</h2>'''
pyperclip.copy(desc)
time.sleep(1)
wait.until(EC.visibility_of_element_located((By.NAME, 'q'))).send_keys(pyperclip.paste())
print("Succesful")
driver.save_screenshot("image.png")
Imports :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pyperclip as pyperclip

Call options for webdriver in selenium

I want to open a website and accept the cookie button via a bot. The problem is that the website does not open maximized automatically, so I entered the options to maximize it. However, i don't know how to call the options in my code without getting an error message. In my code below, how and where would I add the options calling?
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('headless')
driver = webdriver.Chrome(executable_path = 'C:\Program Files\chromedriver.exe')
url = "https://finance.yahoo.com
driver.get(url)
button = driver.find_element_by_name('agree')
button.click()```
Thanks for your help!
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
# options.add_argument('--headless')
driver = webdriver.Chrome(executable_path = 'C:\Program Files\chromedriver.exe', options=options)
url = "https://finance.yahoo.com"
driver.get(url)

Categories

Resources