Package: xpathwebdriver
A python wrapper for interacting with Selenium through XPath and CSS
selectors. The main difference is that you can use use XPaths like://div/text()Which will return you a string, something webdriver API makes more
complicated. Means you can write all your tests based on XPath without
having to do heavy python manipulation of the resulting objects.Also adds:Interactive shell for testing XPath manually and easily against a
live browserYou can share the interactive shell with a script, to keep track
of errors/debuggingMultiple browser managementBrowser life management (wether to keep the browser open or kill it
on exit)Management is done through python contexts (withstatement)Useful settings for local and remote (headless) testingAlso supports environment variables as settingsPlus allowing custom settings that you can also push through
environment variablesScreenshots comparison and diff managementVirtual display management (so you can run “headless” in a remote
instance)you can use VNC to access the remote BrowserAddingxpath,css,selectormethods to returnedWebElementobjects, to keep the Xpath funcionalityUbuntu quick installationYou can opt to use Chromium to simplify installation:sudo apt-get install -y python3-pip imagemagick findimagedupes tightvncserver xserver-xephyr xvfb unzip chromium-browser chromium-chromedriver
sudo pip3 install xpathwebdriver Pillow ipythonYou can quickly test it running:xpathshellThat will open an interactive shell with a browser object. Use TAB to
autocomplete available API. Usebrowser.driverto directly access
the webdriver object.General installationpip install xpathwebdriverInstall xpathwebdriver using pip.Install google chrome.Download chromedriver for your chrome version and install it in your
path.https://chromedriver.chromium.org/downloadsThat generally should work on a modern Linux System (not tested but
should also work on other oses). Try example in section belowFor image comparison installpip install Pillow,findimagedupesandimagemagickpackages for your OS. (they
are not automatically installed to keep basic requirments low) On
ubuntu:sudo apt install imagemagick findimagedupesFor interactive shell installpip install ipythonCheck “Installing Selenium” section for other browsers and details.Examplefromxpathwebdriver.browserimportBrowserbrowser=Browser()browser.get_url('https://duckduckgo.com/')browser.fill(".//*[@id='search_form_input_homepage']",'xpathwebdriver\n')# Using xpath that returns textforidx,tinenumerate(browser.select_xpath('//div/text()')):print(idx,t)# Using css selector which returns elementsforidx,eleminenumerate(browser.select_css('.result__title')):print(idx,elem.text)Documentation and tutorialsCheckexamplesdirectoryTheBrowserAPI.mdfile has a quick list of Browser’s APIUsexpathsell-eto print available environment variables for
settingsUsexpathsell--settings-helpto print settings detailed
documentationor optionally checkxpathwebdriver/default_settings.pyIPython interactive shellFor a faster development and debugging cycles you can run an interactive
shell which will let access the browser. First install ipythonpip install ipython(not automatically installed to keep basic
requirements low)Then you can run thexpathshellin your terminal. You should see
something like:$ xpathshell
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

XpathBrowser in 'b' or 'browser' variables
 Current url: data:,
In [1]: b.get('github.com/joaduo/xpathwebdriver/')
INFO 05:53:35:  Current url: https://github.com/joaduo/xpathwebdriver/Or pass the url in the command arguments. Eg:xpathshell github.com/joaduo/xpathwebdriver/Inside IPython you can enterbrowser.select_xpath?to get
documentation and can access API docs.MoreXpathBrowserdetails at:https://github.com/joaduo/xpathwebdriver/blob/master/BrowserAPI.mdhttps://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver/xpath_browser.pyhttps://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver_tests/test_XpathBrowser.pyUsing unittest libraryimportunittestfromxpathwebdriver.webdriver_managerimportget_browserclassSearchEnginesDemo(unittest.TestCase):deftest_duckduckgo(self):withget_browser()asbrowser:browser.get_url('https://duckduckgo.com/')browser.fill('.//*[@id="search_form_input_homepage"]','xpathwebdriver\n')Check a more options in theexamplesdirectory.Installing SeleniumTo make sure you installed selenium and webdriver correctly use the code
below:fromseleniumimportwebdriverimporttimedriver=webdriver.Chrome()#or use another backenddriver.maximize_window()driver.get('https://www.google.com')print('You have 10 secs to check the browser window...')time.sleep(10)Find the easiest way to install selenium in your environment.You can install driver for chrome and gecko from (OS like ubuntu do have
.deb packages already)https://www.seleniumhq.org/download/#thirdPartyDrivershttp://chromedriver.chromium.org/https://github.com/mozilla/geckodriver/releasesPhantomJs:http://phantomjs.org/download.html(has the driver
embedded)Decompressed executables should be in yor PATH. If you update python’swebdriverpackage make sure you update browsers and drivers.Useful links for working with XPathhttps://addons.mozilla.org/es/firefox/addon/firebug/https://addons.mozilla.org/es/firefox/addon/firepath/http://ricostacruz.com/cheatsheets/xpath.htmlhttp://xpath.alephzarro.com/content/cheatsheet.htmlKilling processes hanging aroundDepeding on your configuration from virtualdisplay and browser,
processes like:Xvnc
Xvfb
Xephyr
chromedriver
...may keep hanging arround. You may want to kill them# check the wanted process is alive
ps faux | grep Xvnc
# and you can kill it. If you are running as root, make sure you are not killing someone else's process too
pkill Xvnc