I have this issue with script: TypeError: 'FirefoxWebElement' object is not iterable












0















I am having a problem executing this script:



pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]

Traceback (most recent call last):
File "IB.py", line 56, in <module>
kidCudix.like_photo('comfort')
File "IB.py", line 41, in like_photo
pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]
TypeError: 'FirefoxWebElement' object is not iterable









share|improve this question




















  • 2





    You haven't shown what hrefs is

    – roganjosh
    Nov 25 '18 at 14:08






  • 1





    get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

    – Guy
    Nov 25 '18 at 14:12











  • @Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

    – Corey Goldberg
    Nov 25 '18 at 17:22








  • 1





    @Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

    – Corey Goldberg
    Nov 25 '18 at 19:15
















0















I am having a problem executing this script:



pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]

Traceback (most recent call last):
File "IB.py", line 56, in <module>
kidCudix.like_photo('comfort')
File "IB.py", line 41, in like_photo
pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]
TypeError: 'FirefoxWebElement' object is not iterable









share|improve this question




















  • 2





    You haven't shown what hrefs is

    – roganjosh
    Nov 25 '18 at 14:08






  • 1





    get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

    – Guy
    Nov 25 '18 at 14:12











  • @Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

    – Corey Goldberg
    Nov 25 '18 at 17:22








  • 1





    @Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

    – Corey Goldberg
    Nov 25 '18 at 19:15














0












0








0








I am having a problem executing this script:



pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]

Traceback (most recent call last):
File "IB.py", line 56, in <module>
kidCudix.like_photo('comfort')
File "IB.py", line 41, in like_photo
pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]
TypeError: 'FirefoxWebElement' object is not iterable









share|improve this question
















I am having a problem executing this script:



pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]

Traceback (most recent call last):
File "IB.py", line 56, in <module>
kidCudix.like_photo('comfort')
File "IB.py", line 41, in like_photo
pic_hrefs = [elem.get_attribute('a href') for elem in hrefs]
TypeError: 'FirefoxWebElement' object is not iterable






python selenium object iterable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:10









Guy

19.3k72250




19.3k72250










asked Nov 25 '18 at 14:06









licensedelicensede

83




83








  • 2





    You haven't shown what hrefs is

    – roganjosh
    Nov 25 '18 at 14:08






  • 1





    get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

    – Guy
    Nov 25 '18 at 14:12











  • @Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

    – Corey Goldberg
    Nov 25 '18 at 17:22








  • 1





    @Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

    – Corey Goldberg
    Nov 25 '18 at 19:15














  • 2





    You haven't shown what hrefs is

    – roganjosh
    Nov 25 '18 at 14:08






  • 1





    get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

    – Guy
    Nov 25 '18 at 14:12











  • @Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

    – Corey Goldberg
    Nov 25 '18 at 17:22








  • 1





    @Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

    – Corey Goldberg
    Nov 25 '18 at 19:15








2




2





You haven't shown what hrefs is

– roganjosh
Nov 25 '18 at 14:08





You haven't shown what hrefs is

– roganjosh
Nov 25 '18 at 14:08




1




1





get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

– Guy
Nov 25 '18 at 14:12





get_attribute return single string, you can't iterate over it. Not to mention in your case it will return None, there is no attribute a href. Try href.

– Guy
Nov 25 '18 at 14:12













@Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

– Corey Goldberg
Nov 25 '18 at 17:22







@Guy, it's true that the attribute is incorrect and will cause an error, but that's not the error he is hitting (yet). The OP is not trying to iterate over the result of get_attribute.

– Corey Goldberg
Nov 25 '18 at 17:22






1




1





@Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

– Corey Goldberg
Nov 25 '18 at 19:15





@Guy, nope, it would iterate over hrefs and apply get_attrbute to each element

– Corey Goldberg
Nov 25 '18 at 19:15












1 Answer
1






active

oldest

votes


















0














Firstly, elem.get_attribute('a href') is problematic since 'a href' is not a valid attribute. Instead, you want to use elem.get_attribute('href').



However, that's not the error you are hitting.



The error message (TypeError: 'FirefoxWebElement' object is not iterable) implies that your input iterable (hrefs) is of the type FirefoxWebElement, which can't be used with a list comprehension since a single FirefoxWebElement is not iterable. Instead of a single element, you need to use a list of elements in your list comprehension. (A list comprehension creates a list from another input iterable). Your comprehension should be something like:



hrefs = [elem.get_attribute('href') for elem in elements]


Example:



The question is vague, but you are likely trying to create a list of links on a web page. To get a list of all links, the general pattern is:




  • create a webdriver

  • navigate to a page

  • locate all elements that represent anchor tags

  • extract the href attribute from each element and store them in a list


You could do that with code like:



from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://some_url')
elements = driver.find_elements_by_tag_name('a')
hrefs = [elem.get_attribute('href') for elem in elements]


Notice that find_elements_by_tag_name('a') returns a list of WebElement, which I use in the subsequent list comprehension. Also notice that the method used is find_elements_by_tag_name (plural). This differs from find_element_by_tag_name (singular) which would only locate a single element.






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',
    autoActivateHeartbeat: false,
    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%2f53468305%2fi-have-this-issue-with-script-typeerror-firefoxwebelement-object-is-not-iter%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









    0














    Firstly, elem.get_attribute('a href') is problematic since 'a href' is not a valid attribute. Instead, you want to use elem.get_attribute('href').



    However, that's not the error you are hitting.



    The error message (TypeError: 'FirefoxWebElement' object is not iterable) implies that your input iterable (hrefs) is of the type FirefoxWebElement, which can't be used with a list comprehension since a single FirefoxWebElement is not iterable. Instead of a single element, you need to use a list of elements in your list comprehension. (A list comprehension creates a list from another input iterable). Your comprehension should be something like:



    hrefs = [elem.get_attribute('href') for elem in elements]


    Example:



    The question is vague, but you are likely trying to create a list of links on a web page. To get a list of all links, the general pattern is:




    • create a webdriver

    • navigate to a page

    • locate all elements that represent anchor tags

    • extract the href attribute from each element and store them in a list


    You could do that with code like:



    from selenium import webdriver

    driver = webdriver.Firefox()
    driver.get('https://some_url')
    elements = driver.find_elements_by_tag_name('a')
    hrefs = [elem.get_attribute('href') for elem in elements]


    Notice that find_elements_by_tag_name('a') returns a list of WebElement, which I use in the subsequent list comprehension. Also notice that the method used is find_elements_by_tag_name (plural). This differs from find_element_by_tag_name (singular) which would only locate a single element.






    share|improve this answer






























      0














      Firstly, elem.get_attribute('a href') is problematic since 'a href' is not a valid attribute. Instead, you want to use elem.get_attribute('href').



      However, that's not the error you are hitting.



      The error message (TypeError: 'FirefoxWebElement' object is not iterable) implies that your input iterable (hrefs) is of the type FirefoxWebElement, which can't be used with a list comprehension since a single FirefoxWebElement is not iterable. Instead of a single element, you need to use a list of elements in your list comprehension. (A list comprehension creates a list from another input iterable). Your comprehension should be something like:



      hrefs = [elem.get_attribute('href') for elem in elements]


      Example:



      The question is vague, but you are likely trying to create a list of links on a web page. To get a list of all links, the general pattern is:




      • create a webdriver

      • navigate to a page

      • locate all elements that represent anchor tags

      • extract the href attribute from each element and store them in a list


      You could do that with code like:



      from selenium import webdriver

      driver = webdriver.Firefox()
      driver.get('https://some_url')
      elements = driver.find_elements_by_tag_name('a')
      hrefs = [elem.get_attribute('href') for elem in elements]


      Notice that find_elements_by_tag_name('a') returns a list of WebElement, which I use in the subsequent list comprehension. Also notice that the method used is find_elements_by_tag_name (plural). This differs from find_element_by_tag_name (singular) which would only locate a single element.






      share|improve this answer




























        0












        0








        0







        Firstly, elem.get_attribute('a href') is problematic since 'a href' is not a valid attribute. Instead, you want to use elem.get_attribute('href').



        However, that's not the error you are hitting.



        The error message (TypeError: 'FirefoxWebElement' object is not iterable) implies that your input iterable (hrefs) is of the type FirefoxWebElement, which can't be used with a list comprehension since a single FirefoxWebElement is not iterable. Instead of a single element, you need to use a list of elements in your list comprehension. (A list comprehension creates a list from another input iterable). Your comprehension should be something like:



        hrefs = [elem.get_attribute('href') for elem in elements]


        Example:



        The question is vague, but you are likely trying to create a list of links on a web page. To get a list of all links, the general pattern is:




        • create a webdriver

        • navigate to a page

        • locate all elements that represent anchor tags

        • extract the href attribute from each element and store them in a list


        You could do that with code like:



        from selenium import webdriver

        driver = webdriver.Firefox()
        driver.get('https://some_url')
        elements = driver.find_elements_by_tag_name('a')
        hrefs = [elem.get_attribute('href') for elem in elements]


        Notice that find_elements_by_tag_name('a') returns a list of WebElement, which I use in the subsequent list comprehension. Also notice that the method used is find_elements_by_tag_name (plural). This differs from find_element_by_tag_name (singular) which would only locate a single element.






        share|improve this answer















        Firstly, elem.get_attribute('a href') is problematic since 'a href' is not a valid attribute. Instead, you want to use elem.get_attribute('href').



        However, that's not the error you are hitting.



        The error message (TypeError: 'FirefoxWebElement' object is not iterable) implies that your input iterable (hrefs) is of the type FirefoxWebElement, which can't be used with a list comprehension since a single FirefoxWebElement is not iterable. Instead of a single element, you need to use a list of elements in your list comprehension. (A list comprehension creates a list from another input iterable). Your comprehension should be something like:



        hrefs = [elem.get_attribute('href') for elem in elements]


        Example:



        The question is vague, but you are likely trying to create a list of links on a web page. To get a list of all links, the general pattern is:




        • create a webdriver

        • navigate to a page

        • locate all elements that represent anchor tags

        • extract the href attribute from each element and store them in a list


        You could do that with code like:



        from selenium import webdriver

        driver = webdriver.Firefox()
        driver.get('https://some_url')
        elements = driver.find_elements_by_tag_name('a')
        hrefs = [elem.get_attribute('href') for elem in elements]


        Notice that find_elements_by_tag_name('a') returns a list of WebElement, which I use in the subsequent list comprehension. Also notice that the method used is find_elements_by_tag_name (plural). This differs from find_element_by_tag_name (singular) which would only locate a single element.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 18:26

























        answered Nov 25 '18 at 17:21









        Corey GoldbergCorey Goldberg

        37.8k22110125




        37.8k22110125
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468305%2fi-have-this-issue-with-script-typeerror-firefoxwebelement-object-is-not-iter%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

            Wiesbaden

            Marschland

            Dieringhausen