Help me, I don't know how can I implement options in my oop code, I don't want browser pop up every time I run the code, I have to add headless option but I don't know how
I do know how to make my code headless in functional or imperative paradigm, but I can't figure out how to implement this in oop paradigm, whatever I do, I approach to an error, I don't know what to do, I would be extremely thankful to who guild me to light
my code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
os.environ["PATH"] = "C:\\SeleniumWebDrivers"
op = webdriver.ChromeOptions()
op.add_argument("--disable-blink-features=AutomationControlled")
op.add_argument("headless")
class Avamovie(webdriver.Opera(options=op)):
def __init__(self, teardown=False):
self.teardown = teardown
super(Avamovie, self).__init__()
self.implicitly_wait(15)
self.maximize_window()
self.minimize_window()
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
if self.teardown:
self.quit()
def get_the_page(self):
self.get("https://avamovie17.xyz")
def search_box(self, title):
sbox = self.find_element_by_css_selector("input[placeholder='جستجو']")
sbox.send_keys(title)
sbox.click()
r = self.find_element_by_css_selector("ul[class='search_result_list']")
r = r.find_element_by_tag_name("li").find_element_by_css_selector(
"span[class='title']"
)
r.click()
def download_links(self):
links = self.find_elements_by_css_selector("a[href*='.mkv']")
links = [link.get_attribute("href") for link in links]
print(links)
with Avamovie() as scraper:
scraper.get_the_page()
scraper.search_box("A hero 2021")
scraper.download_links()
my latest error:
Traceback (most recent call last):
File "c:\Users\yasin\Desktop\pf\Projects\Selena\selena.py", line 1, in <module>
from sites.avamovie import Avamovie
File "c:\Users\yasin\Desktop\pf\Projects\Selena\sites\avamovie.py", line 10, in <module>
class Avamovie(webdriver.Opera(options=op)):
File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\opera\webdriver.py", line 79, in __init__
OperaDriver.__init__(self, executable_path=executable_path,
File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\opera\webdriver.py", line 55, in __init__
ChromiumDriver.__init__(self,
File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\common\service.py", line 71, in start
cmd.extend(self.command_line_args())
File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\service.py", line 45, in command_line_args
return ["--port=%d" % self.port] + self.service_args
TypeError: %d format: a real number is required, not dict
I would appreciate any help, thanks guys
You appear to be trying to inherit from an instance of Opera, not the class itself. The options are passed to the call to super().__init__.
class Avamovie(webdriver.Opera):
def __init__(self, options, teardown=False):
self.teardown = teardown
super(Avamovie, self).__init__(options)
self.implicitly_wait(15)
self.maximize_window()
self.minimize_window()
...
with Avamovie(op) as scraper:
scraper.get_the_page()
scraper.search_box("A hero 2021")
scraper.download_links()
Related
Hello I did not found answer for similar problem so I add new topic.
I have problem with bdd + appium using a page object model. When I run my script I have issue:
Traceback (most recent call last):
File "/home/mimy/.local/lib/python3.8/site-packages/behave/model.py", line 1329, in run
match.run(runner.context)
File "/home/mimy/.local/lib/python3.8/site-packages/behave/matchers.py", line 98, in run
self.func(context, *args, **kwargs)
File "features/steps/allow_to_app_steps.py", line 6, in tap_allow_when_using_app
context.app.launch_page.tap_allow_button()
File "/home/mimy/.local/lib/python3.8/site-packages/behave/runner.py", line 321, in __getattr__
raise AttributeError(msg)
AttributeError: 'Context' object has no attribute 'app'
My environment.py file looks like this:
from appium import webdriver
from app.application import Application
def before_scenario(context, scenario):
desired_capabilities = {
"platformName": "Android",
"platformVersion": "10",
"deviceName": "Pixel 2 XL",
"appPackage": "com.xxx.xxx",
"appActivity": ".ui.MainActivity",
"automationName": "UiAutomator2"
}
context.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities=desired_capabilities)
context.driver.implicitly_wait(5)
context.app = Application(context.driver)
def after_scenario(context, scenario):
context.driver.quit()
My steps file looks like this:
from behave import given, when, then, step
#given('I click on the "Allow only while using the app" button')
def tap_allow_when_using_app(context):
context.app.launch_page.tap_allow_button()
#when('I click on the "Allow" button')
def tap_allow(context):
context.app.launch_page.tap_allow()
My pages file for my page object model looks like:
###LunchPage###
from selenium.webdriver.common.by import By
from pages.base_page import Page
class LaunchPage(Page):
dialog_title = (By.XPATH, "//android.widget.TextView[contains(#text,'Allow QSpot to access this device')]")
allow_only_while_using_the_app = (By.XPATH, "//android.widget.Button[#text='Allow only while using the app']")
allow = (By.XPATH, "//android.widget.Button[#text='Allow']")
def tap_allow_button(self):
self.click(*self.allow_only_while_using_the_app)
def tap_allow(self):
self.click(*self.allow)
###BasePage###
class Page:
def __init__(self, driver):
self.driver = driver
def find_element(self, *locator):
return self.driver.find_element(*locator)
def click(self, *locator):
e = self.find_element(*locator)
e.click()
And class Application
from pages.launch_page import LaunchPage
class Application:
def __init__(self, driver):
self.launch_page = LaunchPage(driver)
As I now this issue may be related with "driver was not starting" but I am not able to fix it.
Many thanks for help!
In my case, I missed up names in architecture names.
So after changing from "enviroment" to "environment" (missing N).
it became alive.
This is my first attempt at using the Tkinter plugin, I know very little past what tutorials I could find. All the answers I've seen so far put a class inside the py file that your building, I however have a plethora of tests that are already compiled into a Test class that runs many separate tests. All the tests run and no errors are encountered before trying to add to the ui.
I would like to be able to run each suite by clicking a button. My problem seems that I'm missing a step some where but not getting any errors or action when I click the button, but an error after I click and close the ui window. I should point out that importing the settings file (which contains most of the webdriver imports) does not help either. I get the same error.
Traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python37\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Python37\lib\unittest\case.py", line 663, in __call__
return self.run(*args, **kwds)
File "C:\Python37\lib\unittest\case.py", line 590, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'Test' object has no attribute 'runTest'
My ui code:
import sys, os, tkinter, TESTadmin
top = tkinter.Tk()
a = TESTadmin.Test()
B = tkinter.Button(top, text= "Test Window", command=a )
B.pack()
top.mainloop()
for clarity my main test file:
from helpers.settings import *
from pieces import adminLogin, adminLogout, docs
class Test(unittest.TestCase):
def setUp(self):
# Maximize Window (remove quotes to use)
'''sel.maximize_window()'''
self.browser = webdriver.Firefox()
self.browser.get("https://mywebsite.net")
# We instantiate and start the browser
def testCases(self):# Add Tests Below
#log in to admin side
login = adminLogin.AdminLogin.do(self)
#docs page
docpage = docs.Docs.do(self)
#log out
logout = adminLogout.Logout.do(self)
if G.log:
for k in G.log.items():
print(k)
### Uncomment to close browser after test ###
def tearDown(self):
self.browser.close()
if __name__ == "__main__":
unittest.main()
As it would turn out, the answer like I thought is simple.
this line :
def testCases(self):
needs to read:
def runTest(self):
after that change every thing works percectly.
My confusion is because originally when building these tests I was following the directions here -> https://selenium-python.readthedocs.io/
They show you to use the testCases() method, and this works! Just not for calling the class. I didn't know where to put the function let alone know that Webdriver had a built in function other than what i was using.
I'm building a custom class to add feautures to selenium.webdriver.Chrome on Python 3.6.2.
from selenium import webdriver
class MyChrome:
def __init__(self):
self.mydriver = webdriver.Chrome()
So far, beside some custom methods I made myself, I used to overridden some selenium.webdriver.Chrome very standard methods like this:
def get(self, url):
self.mydriver.get(url)
Since I don't want to waste time rewriting like that methods like get, find_element_by_xpath, etc... that already works fine for me I tried the following, as suggested here and here
def __getattr__(self, name, *args, **kwargs):
return getattr(self.mydriver, name)(*args, **kwargs)
But when I run the following code
from selenium import webdriver
class MyChrome:
def __init__(self):
self.mydriver = webdriver.Chrome()
def __getattr__(self, name, *args, **kwargs):
return getattr(self.mydriver, name)(*args, **kwargs)
chrome = MyChrome()
chrome.get('https://stackoverflow.com/')
I encounter the error
Traceback (most recent call last):
File "MyChrome.py", line 11, in <module>
chrome.get('https://stackoverflow.com/')
File "MyChrome.py", line 8, in __getattr__
return getattr(self.mydriver, name)(*args, **kwargs)
TypeError: get() missing 1 required positional argument: 'url'
How do I redirect calls to unknown methods called on my object chrome to it's instance variable self.driver?
I created a custom Webdriver once to add features to selenium Chrome Webdriver and I did that by subclassing selenium Chrome Webdriver. This way you inherit all the webdriver methods without a getattr
from selenium.webdriver import Chrome
class MyChrome(Chrome):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# add all your custom features
It doesn't provide answer to your question but something you can leverage
I'm trying to subclass the Chrome WebDriver to include some initialization and cleanup code, but then Python complains that the created object is set to None:
import glob
import selenium
import subprocess
from selenium.webdriver.common.by import By
class WebDriver(selenium.webdriver.Chrome):
def __init__(self, url, *args, **kwargs):
super().__init__(*args, **kwargs)
self.url = url
def __enter__(self):
self.get(self.url)
self.implicitly_wait(15)
def __exit__(self, type, value, traceback):
self.quit()
for path in glob.glob('/tmp/.org.chromium.Chromium.*'):
subprocess.run(['rm', '-rf', path], check=True)
with WebDriver('https://google.com') as driver:
driver.find_element(By.ID, 'lst-ib').send_keys('Search')
Running the code with Python 3:
$ python3 test.py
Traceback (most recent call last):
File "test.py", line 43, in <module>
driver.find_element(By.ID, 'lst-ib').send_keys('Search')
AttributeError: 'NoneType' object has no attribute 'find_element'
Your __enter__() magic method should return self for the driver variable to be pointed to the instance of the WebDriver class:
def __enter__(self):
self.get(self.url)
self.implicitly_wait(15)
return self
To get more information about why and how this works, please see:
Understanding Python's "with" statement
Explaining Python's '__enter__' and '__exit__'
I can not get a method to manage clipboard with the context menu
I'm trying with version 2.8 of wxpython
import wx
import wx.lib.iewin
class main (wx.Frame):
def __init__(self, parent):
wx.Frame.__init__ (self, parent, id, title, pos, size, style)
self.html_field = HTMLWINDOW(self.p_html, -1)
class HTMLWINDOW(wx.html.HtmlWindow):
def __init__(self, parent, id):
self.ie = wx.lib.iewin.IEHtmlWindow(self, -1, style)
self.Bind(wx.EVT_CONTEXT_MENU, self.menu)
def menu(self, event):
self.copied = self.ie.GetStringSelection()
alternative method. but only I can get words, not a whole selection
def OnCellClicked(self, cell, x, y, evt):
if isinstance(cell, wx.html.HtmlWordCell):
sel = wx.html.HtmlSelection()
word = cell.ConvertToText(sel)
super(MyHtmlWindow, self).OnCellClicked(cell, x, y, evt)
importing wx.lib.iewin I get this error
Traceback (most recent call last):
ValueError: _type_ 'v' not supported
File "main.py", line 3959, in <module>
import wx.lib.iewin as iewin
File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/lib/iewin.py", line 15, in <module>
import wx.lib.activex
File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/lib/activex.py", line 35, in <module>
import ctypes.wintypes as wt
File "/usr/lib/python2.7/ctypes/wintypes.py", line 23, in <module>
class VARIANT_BOOL(_SimpleCData):
ValueError: _type_ 'v' not supported
As Werner already pointed out, you are attempting to import wx.lib.iewin in Linux. The iewin module is Windows only. I think it should give an import error that tells you it's Windows only, but the name itself implies that. You could put in a bug ticket so that it does raise an appropriate error though.
If you need a browser-like widget, then you should check out WebView. It was added in wxPython 2.9, I believe and works cross-platform. You can read more about that widget here:
https://wxpython.org/Phoenix/docs/html/wx.html2.WebView.html