Import defined selenium function browser issue











up vote
0
down vote

favorite












Set-up



I use selenium for a variety of things and found myself defining the same functions over and over again.



I decided to define the functions in a separate file, and import these to my work files.






Simple Example

If I define functions and execute all in one file, things work fine. See the simple full_script.py below,



# import webdriver
from selenium import webdriver

# create browser
browser = webdriver.Firefox(
executable_path='/mypath/geckodriver')

# define short xpath function
def el_xp(x):
return browser.find_element_by_xpath(x)

# navigate to url
browser.get('https://nos.nl')

# obtain title first article
el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


This successfully returns the title of the first article on this news website.





Problem



Now, when I split the script in a xpath_function.py and a run_text.py, and save them in a test folder on my desktop, things don't work fine.



xpath_function.py



# import webdriver
from selenium import webdriver

# create browser
browser = webdriver.Firefox(
executable_path='/mypath/geckodriver')

# define short xpath function
def el_xp(x):
return browser.find_element_by_xpath(x)


run_test.py



import os
os.chdir('/my/Desktop/test')
import xpath_function as xf

# import webdriver
from selenium import webdriver

# create browser
browser = webdriver.Firefox(
executable_path='/Users/lucaspanjaard/Documents/RentIndicator/geckodriver')

browser.get('https://nos.nl')

xf.el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


Executing run_test.py results in 2 browsers opened, of which one navigates to the news website and the following error,



NoSuchElementException: Unable to locate element: 
/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3


I suppose the issue is that in both xpath_function.py and run_test.py I'm defining a browser.



However, if I don't define a browser in xpath_function.py, I get an error in that file that no browser is defined.



How do I solve this?










share|improve this question


























    up vote
    0
    down vote

    favorite












    Set-up



    I use selenium for a variety of things and found myself defining the same functions over and over again.



    I decided to define the functions in a separate file, and import these to my work files.






    Simple Example

    If I define functions and execute all in one file, things work fine. See the simple full_script.py below,



    # import webdriver
    from selenium import webdriver

    # create browser
    browser = webdriver.Firefox(
    executable_path='/mypath/geckodriver')

    # define short xpath function
    def el_xp(x):
    return browser.find_element_by_xpath(x)

    # navigate to url
    browser.get('https://nos.nl')

    # obtain title first article
    el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


    This successfully returns the title of the first article on this news website.





    Problem



    Now, when I split the script in a xpath_function.py and a run_text.py, and save them in a test folder on my desktop, things don't work fine.



    xpath_function.py



    # import webdriver
    from selenium import webdriver

    # create browser
    browser = webdriver.Firefox(
    executable_path='/mypath/geckodriver')

    # define short xpath function
    def el_xp(x):
    return browser.find_element_by_xpath(x)


    run_test.py



    import os
    os.chdir('/my/Desktop/test')
    import xpath_function as xf

    # import webdriver
    from selenium import webdriver

    # create browser
    browser = webdriver.Firefox(
    executable_path='/Users/lucaspanjaard/Documents/RentIndicator/geckodriver')

    browser.get('https://nos.nl')

    xf.el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


    Executing run_test.py results in 2 browsers opened, of which one navigates to the news website and the following error,



    NoSuchElementException: Unable to locate element: 
    /html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3


    I suppose the issue is that in both xpath_function.py and run_test.py I'm defining a browser.



    However, if I don't define a browser in xpath_function.py, I get an error in that file that no browser is defined.



    How do I solve this?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Set-up



      I use selenium for a variety of things and found myself defining the same functions over and over again.



      I decided to define the functions in a separate file, and import these to my work files.






      Simple Example

      If I define functions and execute all in one file, things work fine. See the simple full_script.py below,



      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/mypath/geckodriver')

      # define short xpath function
      def el_xp(x):
      return browser.find_element_by_xpath(x)

      # navigate to url
      browser.get('https://nos.nl')

      # obtain title first article
      el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


      This successfully returns the title of the first article on this news website.





      Problem



      Now, when I split the script in a xpath_function.py and a run_text.py, and save them in a test folder on my desktop, things don't work fine.



      xpath_function.py



      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/mypath/geckodriver')

      # define short xpath function
      def el_xp(x):
      return browser.find_element_by_xpath(x)


      run_test.py



      import os
      os.chdir('/my/Desktop/test')
      import xpath_function as xf

      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/Users/lucaspanjaard/Documents/RentIndicator/geckodriver')

      browser.get('https://nos.nl')

      xf.el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


      Executing run_test.py results in 2 browsers opened, of which one navigates to the news website and the following error,



      NoSuchElementException: Unable to locate element: 
      /html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3


      I suppose the issue is that in both xpath_function.py and run_test.py I'm defining a browser.



      However, if I don't define a browser in xpath_function.py, I get an error in that file that no browser is defined.



      How do I solve this?










      share|improve this question













      Set-up



      I use selenium for a variety of things and found myself defining the same functions over and over again.



      I decided to define the functions in a separate file, and import these to my work files.






      Simple Example

      If I define functions and execute all in one file, things work fine. See the simple full_script.py below,



      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/mypath/geckodriver')

      # define short xpath function
      def el_xp(x):
      return browser.find_element_by_xpath(x)

      # navigate to url
      browser.get('https://nos.nl')

      # obtain title first article
      el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


      This successfully returns the title of the first article on this news website.





      Problem



      Now, when I split the script in a xpath_function.py and a run_text.py, and save them in a test folder on my desktop, things don't work fine.



      xpath_function.py



      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/mypath/geckodriver')

      # define short xpath function
      def el_xp(x):
      return browser.find_element_by_xpath(x)


      run_test.py



      import os
      os.chdir('/my/Desktop/test')
      import xpath_function as xf

      # import webdriver
      from selenium import webdriver

      # create browser
      browser = webdriver.Firefox(
      executable_path='/Users/lucaspanjaard/Documents/RentIndicator/geckodriver')

      browser.get('https://nos.nl')

      xf.el_xp('/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text


      Executing run_test.py results in 2 browsers opened, of which one navigates to the news website and the following error,



      NoSuchElementException: Unable to locate element: 
      /html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3


      I suppose the issue is that in both xpath_function.py and run_test.py I'm defining a browser.



      However, if I don't define a browser in xpath_function.py, I get an error in that file that no browser is defined.



      How do I solve this?







      python function selenium python-import






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:58









      LucSpan

      573318




      573318
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can easily fix it by changing the definition of el_exp to include the browser as an extra parameter:



          def el_xp(browser, x):
          return browser.find_element_by_xpath(x)


          now in run_test.py you call it like this:



          xf.el_xp(browser, '/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206339%2fimport-defined-selenium-function-browser-issue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You can easily fix it by changing the definition of el_exp to include the browser as an extra parameter:



            def el_xp(browser, x):
            return browser.find_element_by_xpath(x)


            now in run_test.py you call it like this:



            xf.el_xp(browser, '/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text





            share|improve this answer



























              up vote
              1
              down vote



              accepted










              You can easily fix it by changing the definition of el_exp to include the browser as an extra parameter:



              def el_xp(browser, x):
              return browser.find_element_by_xpath(x)


              now in run_test.py you call it like this:



              xf.el_xp(browser, '/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text





              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You can easily fix it by changing the definition of el_exp to include the browser as an extra parameter:



                def el_xp(browser, x):
                return browser.find_element_by_xpath(x)


                now in run_test.py you call it like this:



                xf.el_xp(browser, '/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text





                share|improve this answer














                You can easily fix it by changing the definition of el_exp to include the browser as an extra parameter:



                def el_xp(browser, x):
                return browser.find_element_by_xpath(x)


                now in run_test.py you call it like this:



                xf.el_xp(browser, '/html/body/main/section[1]/div/ul/li[1]/a/div[2]/h3').text






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 11:07









                Andersson

                34.3k103063




                34.3k103063










                answered Nov 8 at 11:04









                Uku Loskit

                29.3k76679




                29.3k76679






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206339%2fimport-defined-selenium-function-browser-issue%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Liste der Kulturdenkmale in Wilsdruff

                    Android Play Services Check