Selenium2Library code completion in PyCharm - python

I'm experimenting with creating a basic library extension for Robot Framework using Python, and I'm using PyCharm as the editor. For libraries imported directly code completion is working fine, but in this case I'm importing the Selenium2Library indirectly via a method:
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
Which I call from other methods with something like
driver = get_current_browser()
This successfully grabs the webdriver browser instance from Robot Framework and lets me do as I please, but I don't get code hints when I go to edit a 'driver' variable. Is there way I can get hints in this scenario?
Here's the code in full:
from robot.libraries.BuiltIn import BuiltIn
from Selenium2Library.keywords.keywordgroup import KeywordGroup
import logging
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
class MyLibrary(KeywordGroup):
def get_title_via_python(self):
driver = get_current_browser()
title = driver.title
logging.warn("checking title %s" % title)
return title

Try adding a docstring to your function to help PyCharm.
from selenium.webdriver import Remote # Remote imported only for code completion
def get_current_browser():
"""
:rtype: Remote
"""
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
More at http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html

Related

Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' (Python)

I am trying to run this script
import crawler
crawler.crawl(url="https://www.montratec.com",output_dir="crawling_test",method="rendered-all")
from this library:
https://github.com/SimFin/pdf-crawler
but I am getting this error:
Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
I already have Firefox installed and I am using Windows.
If you have Firefox installed in a non-default location which is not in your system’s search path, you can specify a binary field on the moz:firefoxOptions capabilities object (documented in README), or use the --binary PATH flag passed to geckodriver when it starts.
Since Selenium is tagged, You can do the following changes to get rid of the above error :-
This is purely selenium solution, if you have a running instance of driver, you re-configure it using the FirefoxOptions like below :
options = webdriver.FirefoxOptions()
options.binary_location = r"C:\Program Files\Mozilla Firefox\firefox.exe"
driver = webdriver.Firefox(executable_path=r'\geckodriver.exe full path here', firefox_options=options)
driver.get("https://www.montratec.com1")
for crawler (Web scraping framework based on py3 asyncio & aiohttp libraries.)
Installation :
pip install crawler
Sample code :
import re
from itertools import islice
from crawler import Crawler, Request
RE_TITLE = re.compile(r'<title>([^<]+)</title>', re.S | re.I)
class TestCrawler(Crawler):
def task_generator(self):
for host in islice(open('var/domains.txt'), 100):
host = host.strip()
if host:
yield Request('http://%s/' % host, tag='page')
def handler_page(self, req, res):
print('Result of request to {}'.format(req.url))
try:
title = RE_TITLE.search(res.body).group(1)
except AttributeError:
title = 'N/A'
print('Title: {}'.format(title))
bot = TestCrawler(concurrency=10)
bot.run()
Official reference here

Python Selenium Geckodrive

Hey there just trying a basic browser launch using Firefox.
I've tried using executable path, if statement, and without an if statement and the browser will still not open. I've checked the shell and I don't have an error.My best guess is I'm missing an action of some sort I just need someone to point my in the right direction using my current code, thank you.
from selenium import webdriver
class testbot():
def botfox(self):
driver = self.driver = webdriver.firfox(geckodriver)
driver.get("https://wwww.google.com")
if __name__ == "__botfox__":
botfox()
ok, try this :)
from selenium import webdriver
class testbot():
def botfox(self):
self.driver = webdriver.Firefox()
self.driver.get("https://wwww.google.com")
if __name__ == '__main__':
testBotInstace = testbot()
testBotInstace.botfox()
I'd be surprised if that worked. Have you tried calling it via testbot().botfox() ?
webdriver.firfox would not work, as the syntax is webdriver.Firefox
webdriver.firfox(geckodriver) would not work as geckodriver is not defined anywhere
botfox() would not work because there is no function defined as that. There is one inside of testbot but you would need to first instantiate the class and then call it via testbot().botfox()

How do I include the setup method in python-appium base page?

I am just a beginner when it comes to python or coding in general. I'm trying to setup an automation framework using python-appium and the page object model. My question is, How do I include the setup method into my base page? When I call the method from my test script it says 'driver' is unresolved and throws an exception. I know I am just missing something simple but my google-fu has failed me and now I have posted here.
Here is my setup method:
def setUp(self):
"Setup for the test"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = '05157df532e5e40e'
# Returns abs path relative to this file and not cwd
desired_caps['app'] = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'/Users/tyler/Desktop/Instasize_20172109_3.9.9_118_google.apk'))
desired_caps['appPackage'] = 'com.jsdev.instasize'
desired_caps['appActivity'] = '.activities.MainActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub',
desired_caps)
I want to call this method to all my tests or a variation of it depending on the device being used. Here is how I am trying to call it from my base page:
def driverSetup(self):
driverSetup = DriverBuilderAndroid(driver)
driverSetup.setUp()
All of my import statements are present. Let me know if there is any other info you need. If you could refer to a python appium POM tutorial that would be much appreciated as well. This is my first post on stackoverflow.
I created a separate python file called driverBuilder with my setup shown above. I then import it into each of my test files and call the method like this:
from DriverBuilder import DriverBuilder """<--class from DriverBuilder file"""
def test(self):
driver = DriverBuilder.driver

Python protection settings IE

I am facing a problem automating the protection settings in IE using Selenium with python.
I found a solution to automate the settings in java but it is not working when i changed it to python .
I tried the following::
from selenium import webdriver
caps=webdriver.DesiredCapabilites.INTERNETEXPLORER
caps['INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS']=True
driver=webdriver.Ie(caps)
This gave an error with respect to the argument given .
and when I used driver = webdriver.Ie()
It says protection mode settings must be same for all zones.
Can anyone help me automate this thing using selenium in python.
According to documentation, in python-selenum, you should use setting called
ignoreProtectedModeSettings:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.INTERNETEXPLORER
caps['ignoreProtectedModeSettings'] = True
driver = webdriver.Ie(capabilities=caps)
Desired capabilities doesn't work in some instances. Here is a method to change protection settings from the registry using winreg.
from winreg import *
def Enable_Protected_Mode():
# SECURITY ZONES ARE AS FOLLOWS:
# 0 is the Local Machine zone
# 1 is the Intranet zone
# 2 is the Trusted Sites zone
# 3 is the Internet zone
# 4 is the Restricted Sites zone
# CHANGING THE SUBKEY VALUE "2500" TO DWORD 0 ENABLES PROTECTED MODE FOR THAT ZONE.
# IN THE CODE BELOW THAT VALUE IS WITHIN THE "SetValueEx" FUNCTION AT THE END AFTER "REG_DWORD".
#os.system("taskkill /F /IM iexplore.exe")
try:
keyVal = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
key = OpenKey(HKEY_CURRENT_USER, keyVal, 0, KEY_ALL_ACCESS)
SetValueEx(key, "2500", 0, REG_DWORD, 0)
print("enabled protected mode")
except Exception:
print("failed to enable protected mode")
In case capabilities mode does not work, there is an alternative.
from selenium import webdriver
from selenium.webdriver.ie.options import Options
ie_options = Options()
ie_options.ignore_protected_mode_settings = True
driver = webdriver.Ie(options=ie_options)
driver.get('http://www.google.com')
Here is another variation on Dinesh's code that works to disable protected modes in the registry. It also closes the connection.
Simply put this code before your selenium automation code, and it will prepare the browser.
import winreg
def set_reg(REG_PATH, name, value):
try:
winreg.CreateKey(winreg.HKEY_CURRENT_USER, REG_PATH)
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, REG_PATH, 0, winreg.KEY_WRITE) as registry_key:
winreg.SetValueEx(registry_key, name, 0, winreg.REG_DWORD, value)
winreg.CloseKey(registry_key)
return True
except WindowsError:
return False
for i in range(1,5): set_reg(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\\" + str(i),'2500', 3)
Adapted from the top answer in this thread by Mash and icc97:
python script to read and write a path to registry

Bad status line exception while opening a page on django development server

I am executing selenium test cases via Proboscis, for good reporting of test results. I have the following test case written
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from proboscis import test
import unittest
driver = webdriver.Firefox()
#test(groups=["unit","login"])
class UI_test(unittest.TestCase):
def test_SuccessfulErrorMsgOnEmptyUserName(self):
driver.get("http://127.0.0.1:7999/login/")
username_input = driver.find_element_by_id("id_email")
username_input.send_keys('')
password_input = driver.find_element_by_id("id_password")
password_input.send_keys('bill3')
driver.find_element_by_xpath('//input[#value = "Log In"]').click()
driver.implicitly_wait(3)
driver.find_element_by_class_name("error-login")
driver.close()
def run_tests():
from proboscis import TestProgram
# from tests import unit
# Run Proboscis and exit.
TestProgram().run_and_exit()
if __name__ == '__main__':
run_tests()
What could be causing the BadStatusLine exception in this code?
Looks like a duplicate of Python/Django "BadStatusLine" error but i can't flag due to this question having a bounty. According to this answer, a BadStatusLine exception is likely caused by an empty response, as in there being no status line at all.
According to this answer, the server formally tells the client there is no more data, instead of simply causing a connection reset or rude timeout.

Categories

Resources