Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Embedding Python in C++

Embedding Python in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++pythoncomdesignlinux
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13092790
    wrote on last edited by
    #1

    I have tried using Selenium with both the PythonQt and the Python.h libraries. I have a Ubuntu 16.04 platform using QT Creator 4.2.2 with Qt5.8.0. I can get everything setup with either interpreter and call a simple Python class for strings from Qt. However when I try and use Selenium I end up having problems (running py script for Selenium from the prompt works fine). For instance, using python.h When PyObject_GetAttrString(pModule, "browser") is called I get a segmentation fault. I am assuming that I need to load the selenium module or set the python path to it in Qt, but I am not clear on how to do this with multiple modules/paths. This is my python code browser.py:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    class browser():
    def __init__(self):
    self.driver = None
    self.webelement = None
    def getbrowser(self, name):
    if name == "chrome":
    self.driver = webdriver.Chrome()
    elif name == "firefox":
    self.driver = webdriver.Firefox()
    def getpage(self,page):
    self.driver.get(page)
    def getsearchelement(self,elementname):
    self.webelement = self.driver.find_element_by_name(elementname)
    def getsearch(self,search):
    self.webelement.send_keys(search + Keys.RETURN)
    def stopbrowser(self):
    self.driver.quit()

    """ Following runs fine from prompt, crashes in PythonQt/python.h
    browser = webdriver.Firefox()
    browser.get('https:\\search.yahoo.com')
    elem = browser.find_element_by_name('p') # Find the search box
    elem.send_keys('seleniumhq' + Keys.RETURN)
    browser.quit()
    """

    Here is the routine from my c++ code

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {

    ui->setupUi(this);
    
    // Doing it here for debugging
    PyObject \*pName, \*pModule, \*pFuncSet, \*pValue, \*pClass, \*pInst, \*pArgs;
    PyObject \*pGetBrowser, \*pGetPage, \*pGetSearchElement, \*pGetSearch, \*pStopBrowser;
    // Initialize the Python Interpreter
    Py\_Initialize();
    PySys\_SetPath("/home/user/CppCode/StocksWeb/src/");
    // Build the name object
    pName = PyString\_FromString("browser");
    // Load the module object
    pModule = PyImport\_ImportModule("browser");
    pClass = PyObject\_GetAttrString(pModule, "browser");
    if(pClass != NULL && pModule != NULL ) {
        pArgs  = Py\_BuildValue("(  )");
        pInst = PyEval\_CallObject(pClass, pArgs);
        if ( pInst != NULL ) {
            pGetBrowser = PyObject\_GetAttrString(pInst, "getbrowser"
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups