How can I make my code more readable with multiple css selectorsHow can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline in Python?How to return multiple values from a function?How can I make a time delay in Python?How can you profile a Python script?How to make a chain of function decorators?How to make a flat list out of list of listsHow can I count the occurrences of a list item?How can I reverse a list in Python?

Why do Russians call their women expensive ("дорогая")?

Can an old DSLR be upgraded to match modern smartphone image quality

When a current flow in an inductor is interrupted, what limits the voltage rise?

Is it possible to change original filename of an exe?

How does one "dump" or deplete propellant without changing spacecraft attitude or trajectory?

Yandex programming contest: Alarms

find the Integer value after a string from a file

How can I include a header file that contains `>` in its name?

What is the intuition behind uniform continuity?

Is this light switch installation safe and legal?

How can I prevent interns from being expendable?

How to properly maintain eye contact with people that have distinctive facial features?

Are UK pensions taxed twice?

Get LaTeX form from step by step solution

Did airlines fly their aircraft slower in response to oil prices in the 1970s?

What are the slash markings on Gatwick's 08R/26L?

Rotated Position of Integers

60s (or earlier) short story where each colony has one person who doesn't connect well with others who is there for being able to absorb knowledge

What caused the tendency for conservatives to not support climate change regulations?

The qvolume of an integer

Where did the “vikings wear helmets with horn” stereotype come from and why?

Why is there a need to modify system call tables in linux?

Mapping a function f[xi_,xj_] over a list x1, ...., xn with the i < j restriction

When was the expression "Indian file" first used in English?



How can I make my code more readable with multiple css selectors


How can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline in Python?How to return multiple values from a function?How can I make a time delay in Python?How can you profile a Python script?How to make a chain of function decorators?How to make a flat list out of list of listsHow can I count the occurrences of a list item?How can I reverse a list in Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am looking for a way to shorten my code.



I am gathering captcha values from https://www.ultimateqa.com/complicated-page/ and submitting them for 3 captcha boxes.



Can I shorten my code using a function?
Or should I use different css selectors?



for i in range(1):
cap0 = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input'
captext1= driver.find_element_by_css_selector(cap0).get_attribute("data-first_digit")
capnumb1 = int(captext1)
captext2= driver.find_element_by_css_selector(cap0).get_attribute("data-second_digit")
capnumb2 = int(captext2)
driver.find_element_by_css_selector(cap0).send_keys(capnumb1 + capnumb2)


for i in range(1):
cap1 = '#et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input'
captext1= driver.find_element_by_css_selector(cap1).get_attribute("data-first_digit")
capnumb1 = int(captext1)
captext2= driver.find_element_by_css_selector(cap1).get_attribute("data-second_digit")
capnumb2 = int(captext2)
driver.find_element_by_css_selector(cap1).send_keys(capnumb1 + capnumb2)


for i in range(1):
cap2 = '#et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
captext1= driver.find_element_by_css_selector(cap2).get_attribute("data-first_digit")
capnumb1 = int(captext1)
captext2= driver.find_element_by_css_selector(cap2).get_attribute("data-second_digit")
capnumb2 = int(captext2)
driver.find_element_by_css_selector(cap2).send_keys(capnumb1 + capnumb2)









share|improve this question




























    0















    I am looking for a way to shorten my code.



    I am gathering captcha values from https://www.ultimateqa.com/complicated-page/ and submitting them for 3 captcha boxes.



    Can I shorten my code using a function?
    Or should I use different css selectors?



    for i in range(1):
    cap0 = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap0).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap0).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap0).send_keys(capnumb1 + capnumb2)


    for i in range(1):
    cap1 = '#et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap1).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap1).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap1).send_keys(capnumb1 + capnumb2)


    for i in range(1):
    cap2 = '#et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
    captext1= driver.find_element_by_css_selector(cap2).get_attribute("data-first_digit")
    capnumb1 = int(captext1)
    captext2= driver.find_element_by_css_selector(cap2).get_attribute("data-second_digit")
    capnumb2 = int(captext2)
    driver.find_element_by_css_selector(cap2).send_keys(capnumb1 + capnumb2)









    share|improve this question
























      0












      0








      0








      I am looking for a way to shorten my code.



      I am gathering captcha values from https://www.ultimateqa.com/complicated-page/ and submitting them for 3 captcha boxes.



      Can I shorten my code using a function?
      Or should I use different css selectors?



      for i in range(1):
      cap0 = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap0).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap0).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap0).send_keys(capnumb1 + capnumb2)


      for i in range(1):
      cap1 = '#et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap1).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap1).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap1).send_keys(capnumb1 + capnumb2)


      for i in range(1):
      cap2 = '#et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap2).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap2).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap2).send_keys(capnumb1 + capnumb2)









      share|improve this question














      I am looking for a way to shorten my code.



      I am gathering captcha values from https://www.ultimateqa.com/complicated-page/ and submitting them for 3 captcha boxes.



      Can I shorten my code using a function?
      Or should I use different css selectors?



      for i in range(1):
      cap0 = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap0).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap0).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap0).send_keys(capnumb1 + capnumb2)


      for i in range(1):
      cap1 = '#et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap1).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap1).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap1).send_keys(capnumb1 + capnumb2)


      for i in range(1):
      cap2 = '#et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
      captext1= driver.find_element_by_css_selector(cap2).get_attribute("data-first_digit")
      capnumb1 = int(captext1)
      captext2= driver.find_element_by_css_selector(cap2).get_attribute("data-second_digit")
      capnumb2 = int(captext2)
      driver.find_element_by_css_selector(cap2).send_keys(capnumb1 + capnumb2)






      python selenium






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 10:04









      BollenBollen

      31




      31






















          3 Answers
          3






          active

          oldest

          votes


















          -1














          You can use the name attribute with the loop index as parameter



          for i in range(3):
          cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_i"]')
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          Or locate all the captcha by partial name and iterate over the list



          captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

          for cap in captchas:
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          No need to relocate the captcha field every time, locate it once and use it.






          share|improve this answer




















          • 1





            That is another great solution. I am learning alot, thanks :p

            – Bollen
            Mar 24 at 12:19


















          2














          You can use selector that matches all 3 cases (just separate CSS selectors with comma) and handle them in a loop:



          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1= cap.get_attribute("data-first_digit")
          capnumb1 = int(captext1)
          captext2= cap.get_attribute("data-second_digit")
          capnumb2 = int(captext2)
          cap.send_keys(capnumb1 + capnumb2)


          P.S. for i in range(1) loop should be removed as it doesn't make any sense






          share|improve this answer

























          • Good to know! that makes things alot easier, thanks :)

            – Bollen
            Mar 24 at 10:35


















          2














          Few pythonic/programmatic notes:



          • A for loop with range(1) is iterating once (on the 0th element); thus, not very useful as regular code iterates once anyways.

          • Anytime you see similar code being written down, you can generally abstract the logic into reusable code.

          I'm personally unfamiliar with selenium and not that great with CSS, but here's a go at the python:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
          cap_end = ' > div.et_pb_contact > form > div > div > p > input'
          for cap in caps:
          tmp_cap = cap + cap_end
          captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
          captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
          driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)


          Let me know in a comment if this works for you!



          Using my function with JaSON's combined CSS selectors logic:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1, capnumb1 = get_digit(cap, "data-first_digit")
          captext2, capnumb2 = get_digit(cap, "data-second_digit")
          cap.send_keys(capnumb1 + capnumb2)





          share|improve this answer

























          • Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

            – jsonV
            Mar 24 at 10:38











          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%2f55322634%2fhow-can-i-make-my-code-more-readable-with-multiple-css-selectors%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          -1














          You can use the name attribute with the loop index as parameter



          for i in range(3):
          cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_i"]')
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          Or locate all the captcha by partial name and iterate over the list



          captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

          for cap in captchas:
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          No need to relocate the captcha field every time, locate it once and use it.






          share|improve this answer




















          • 1





            That is another great solution. I am learning alot, thanks :p

            – Bollen
            Mar 24 at 12:19















          -1














          You can use the name attribute with the loop index as parameter



          for i in range(3):
          cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_i"]')
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          Or locate all the captcha by partial name and iterate over the list



          captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

          for cap in captchas:
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          No need to relocate the captcha field every time, locate it once and use it.






          share|improve this answer




















          • 1





            That is another great solution. I am learning alot, thanks :p

            – Bollen
            Mar 24 at 12:19













          -1












          -1








          -1







          You can use the name attribute with the loop index as parameter



          for i in range(3):
          cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_i"]')
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          Or locate all the captcha by partial name and iterate over the list



          captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

          for cap in captchas:
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          No need to relocate the captcha field every time, locate it once and use it.






          share|improve this answer















          You can use the name attribute with the loop index as parameter



          for i in range(3):
          cap = driver.find_element_by_css_selector(f'[name="et_pb_contact_captcha_i"]')
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          Or locate all the captcha by partial name and iterate over the list



          captchas = driver.find_elements_by_css_selector('[name^="et_pb_contact_captcha_"]')

          for cap in captchas:
          captext = cap.get_attribute("data-first_digit")
          capnumb1 = int(captext)
          captext = cap.get_attribute("data-second_digit")
          capnumb2 = int(captext)
          cap.send_keys(capnumb + capnumb2)


          No need to relocate the captcha field every time, locate it once and use it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 at 10:52

























          answered Mar 24 at 10:46









          GuyGuy

          20k72251




          20k72251







          • 1





            That is another great solution. I am learning alot, thanks :p

            – Bollen
            Mar 24 at 12:19












          • 1





            That is another great solution. I am learning alot, thanks :p

            – Bollen
            Mar 24 at 12:19







          1




          1





          That is another great solution. I am learning alot, thanks :p

          – Bollen
          Mar 24 at 12:19





          That is another great solution. I am learning alot, thanks :p

          – Bollen
          Mar 24 at 12:19













          2














          You can use selector that matches all 3 cases (just separate CSS selectors with comma) and handle them in a loop:



          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1= cap.get_attribute("data-first_digit")
          capnumb1 = int(captext1)
          captext2= cap.get_attribute("data-second_digit")
          capnumb2 = int(captext2)
          cap.send_keys(capnumb1 + capnumb2)


          P.S. for i in range(1) loop should be removed as it doesn't make any sense






          share|improve this answer

























          • Good to know! that makes things alot easier, thanks :)

            – Bollen
            Mar 24 at 10:35















          2














          You can use selector that matches all 3 cases (just separate CSS selectors with comma) and handle them in a loop:



          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1= cap.get_attribute("data-first_digit")
          capnumb1 = int(captext1)
          captext2= cap.get_attribute("data-second_digit")
          capnumb2 = int(captext2)
          cap.send_keys(capnumb1 + capnumb2)


          P.S. for i in range(1) loop should be removed as it doesn't make any sense






          share|improve this answer

























          • Good to know! that makes things alot easier, thanks :)

            – Bollen
            Mar 24 at 10:35













          2












          2








          2







          You can use selector that matches all 3 cases (just separate CSS selectors with comma) and handle them in a loop:



          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1= cap.get_attribute("data-first_digit")
          capnumb1 = int(captext1)
          captext2= cap.get_attribute("data-second_digit")
          capnumb2 = int(captext2)
          cap.send_keys(capnumb1 + capnumb2)


          P.S. for i in range(1) loop should be removed as it doesn't make any sense






          share|improve this answer















          You can use selector that matches all 3 cases (just separate CSS selectors with comma) and handle them in a loop:



          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1= cap.get_attribute("data-first_digit")
          capnumb1 = int(captext1)
          captext2= cap.get_attribute("data-second_digit")
          capnumb2 = int(captext2)
          cap.send_keys(capnumb1 + capnumb2)


          P.S. for i in range(1) loop should be removed as it doesn't make any sense







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 at 10:15

























          answered Mar 24 at 10:09









          JaSONJaSON

          68819




          68819












          • Good to know! that makes things alot easier, thanks :)

            – Bollen
            Mar 24 at 10:35

















          • Good to know! that makes things alot easier, thanks :)

            – Bollen
            Mar 24 at 10:35
















          Good to know! that makes things alot easier, thanks :)

          – Bollen
          Mar 24 at 10:35





          Good to know! that makes things alot easier, thanks :)

          – Bollen
          Mar 24 at 10:35











          2














          Few pythonic/programmatic notes:



          • A for loop with range(1) is iterating once (on the 0th element); thus, not very useful as regular code iterates once anyways.

          • Anytime you see similar code being written down, you can generally abstract the logic into reusable code.

          I'm personally unfamiliar with selenium and not that great with CSS, but here's a go at the python:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
          cap_end = ' > div.et_pb_contact > form > div > div > p > input'
          for cap in caps:
          tmp_cap = cap + cap_end
          captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
          captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
          driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)


          Let me know in a comment if this works for you!



          Using my function with JaSON's combined CSS selectors logic:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1, capnumb1 = get_digit(cap, "data-first_digit")
          captext2, capnumb2 = get_digit(cap, "data-second_digit")
          cap.send_keys(capnumb1 + capnumb2)





          share|improve this answer

























          • Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

            – jsonV
            Mar 24 at 10:38















          2














          Few pythonic/programmatic notes:



          • A for loop with range(1) is iterating once (on the 0th element); thus, not very useful as regular code iterates once anyways.

          • Anytime you see similar code being written down, you can generally abstract the logic into reusable code.

          I'm personally unfamiliar with selenium and not that great with CSS, but here's a go at the python:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
          cap_end = ' > div.et_pb_contact > form > div > div > p > input'
          for cap in caps:
          tmp_cap = cap + cap_end
          captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
          captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
          driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)


          Let me know in a comment if this works for you!



          Using my function with JaSON's combined CSS selectors logic:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1, capnumb1 = get_digit(cap, "data-first_digit")
          captext2, capnumb2 = get_digit(cap, "data-second_digit")
          cap.send_keys(capnumb1 + capnumb2)





          share|improve this answer

























          • Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

            – jsonV
            Mar 24 at 10:38













          2












          2








          2







          Few pythonic/programmatic notes:



          • A for loop with range(1) is iterating once (on the 0th element); thus, not very useful as regular code iterates once anyways.

          • Anytime you see similar code being written down, you can generally abstract the logic into reusable code.

          I'm personally unfamiliar with selenium and not that great with CSS, but here's a go at the python:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
          cap_end = ' > div.et_pb_contact > form > div > div > p > input'
          for cap in caps:
          tmp_cap = cap + cap_end
          captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
          captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
          driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)


          Let me know in a comment if this works for you!



          Using my function with JaSON's combined CSS selectors logic:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1, capnumb1 = get_digit(cap, "data-first_digit")
          captext2, capnumb2 = get_digit(cap, "data-second_digit")
          cap.send_keys(capnumb1 + capnumb2)





          share|improve this answer















          Few pythonic/programmatic notes:



          • A for loop with range(1) is iterating once (on the 0th element); thus, not very useful as regular code iterates once anyways.

          • Anytime you see similar code being written down, you can generally abstract the logic into reusable code.

          I'm personally unfamiliar with selenium and not that great with CSS, but here's a go at the python:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          caps = ['#et_pb_contact_form_0', '#et_pb_contact_form_1', '#et_pb_contact_form_2']
          cap_end = ' > div.et_pb_contact > form > div > div > p > input'
          for cap in caps:
          tmp_cap = cap + cap_end
          captext1, capnumb1 = get_digit(tmp_cap, "data-first_digit")
          captext2, capnumb2 = get_digit(tmp_cap, "data-second_digit")
          driver.find_element_by_css_selector(tmp_cap).send_keys(capnumb1 + capnumb2)


          Let me know in a comment if this works for you!



          Using my function with JaSON's combined CSS selectors logic:



          def get_digit(cap,attr):
          element = driver.find_element_by_css_selector(cap).get_attribute(attr)
          return (element, int(element))

          cap_css = '#et_pb_contact_form_0 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_1 > div.et_pb_contact > form > div > div > p > input, #et_pb_contact_form_2 > div.et_pb_contact > form > div > div > p > input'
          cap_list = driver.find_elements_by_css_selector(cap_css)
          for cap in cap_list:
          captext1, capnumb1 = get_digit(cap, "data-first_digit")
          captext2, capnumb2 = get_digit(cap, "data-second_digit")
          cap.send_keys(capnumb1 + capnumb2)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 at 10:34

























          answered Mar 24 at 10:28









          jsonVjsonV

          659




          659












          • Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

            – jsonV
            Mar 24 at 10:38

















          • Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

            – jsonV
            Mar 24 at 10:38
















          Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

          – jsonV
          Mar 24 at 10:38





          Glad to hear it! I have no knowledge of Selenium haha... Also, I updated my answer to include a merge of JaSON's answer and mine own. That should also work assuming the combined selector is valid.

          – jsonV
          Mar 24 at 10:38

















          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%2f55322634%2fhow-can-i-make-my-code-more-readable-with-multiple-css-selectors%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript