Home > Enterprise >  hey, i would like to make a Selenium Bot in Python that solves Driving School questions
hey, i would like to make a Selenium Bot in Python that solves Driving School questions

Time:05-30

How to fetch the questionNumber with Selenium?

I already have this like I already have the login process but I want to fetch the question numbers to google them and return the answers found on google.

driver = webdriver.Chrome()

driver.get("https://www.windrive-theorietrainer.de/tt/questionnaires/category/questionnaire/3")
time.sleep(5)
lol = driver.find_element_by_id("email")
lol.send_keys("[email protected]")
lol = driver.find_element_by_id("password")
lol.send_keys("1233123123")
driver.find_element_by_xpath('//button[@type="submit"]').click()

driver.execute_script("window.open('"   "https://www.windrive-theorietrainer.de/tt/questionnaires/exercise/questionnaire"  "');")
time.sleep(5)
elements = driver.find_element_by_name('')

but it just tells me that this is a illegal method any ideas to fix?

<tt-questionnaire _nghost-nbk-c271="" 
  ><kendo-dialog
    _ngcontent-nbk-c271=""
    id="questionnaireModal"
    
    tabindex="0"
    dir="ltr"
    ><div
      
    ></div>
    <div
      role="dialog"
      
    >
      <!----><!---->
      <div >
        <div
          _ngcontent-nbk-c271=""
          id="questionnaireContainer"
          
          style=""
        >
          <!---->
          <div
            _ngcontent-nbk-c271=""
            id="questionnaire"
            
            style=""
          >
            <div _ngcontent-nbk-c271="" id="questionnaireHeader">
              <div _ngcontent-nbk-c271="" >
                <span _ngcontent-nbk-c271="" 
                  >Grundstoff<!----><!----><!----></span
                ><span _ngcontent-nbk-c271="" 
                  >1.2.05-101</span
                ><span _ngcontent-nbk-c271="" 
                  >Aufgabe 1</span
                ><span _ngcontent-nbk-c271="" >Punkte: 4</span>
              </div>
              <ul _ngcontent-nbk-c271="" >
                <tt-language-dropdown _ngcontent-nbk-c271="" _nghost-nbk-c241=""
                  ><!----></tt-language-dropdown
                >
              </ul>
            </div>
            <div _ngcontent-nbk-c271="" id="questionnaireContent">
              <!----><tt-questionnaire-question
                _ngcontent-nbk-c271=""
                _nghost-nbk-c270=""
                
                ><h2 _ngcontent-nbk-c270="" >
                  <tt-tts _ngcontent-nbk-c270="" _nghost-nbk-c243=""
                    ><div _ngcontent-nbk-c243="">
                      <span _ngcontent-nbk-c270=""
                        >Wo ist das Überholen verboten?</span
                      >
                    </div>
                    <!----></tt-tts
                  >
                </h2>
                <div _ngcontent-nbk-c270="" >
                  <div _ngcontent-nbk-c270="" >
                    <!----><!----><!---->
                  </div>
                  <div _ngcontent-nbk-c270="" >
                    <tt-questionnaire-selection-question
                      _ngcontent-nbk-c270=""
                      _nghost-nbk-c268=""
                      
                      ><div
                        _ngcontent-nbk-c268=""
                        
                      >
                        <!---->
                        <ul _ngcontent-nbk-c268="" >
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox1"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox1"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >In allen Einbahnstraßen</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox2"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox2"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >Wo die Verkehrslage unklar ist</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox3"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox3"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >Wo der Gegenverkehr behindert werden
                                    könnte</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <!---->
                        </ul>
                      </div></tt-questionnaire-selection-question
                    ><!----><!---->
                  </div>
                </div>
                <!----><!----></tt-questionnaire-question
              ><!---->
            </div>
            <div _ngcontent-nbk-c271="" id="questionnaireButtons">
              <div _ngcontent-nbk-c271="" >
                <span _ngcontent-nbk-c271="" 
                  ><i
                    _ngcontent-nbk-c271=""
                    
                  ></i
                  ><span _ngcontent-nbk-c271="" 
                    >noch 30 Aufgaben</span
                  ><!----><!----></span
                ><!---->
              </div>
              <div _ngcontent-nbk-c271="" >
                <button
                  _ngcontent-nbk-c271=""
                  
                >
                  Abgabe</button
                ><!----><!----><button
                  _ngcontent-nbk-c271=""
                  
                >
                  Markieren</button
                ><!----><!----><button
                  _ngcontent-nbk-c271=""
                  
                >
                  Weiter</button
                ><!----><!----><!----><button
                  _ngcontent-nbk-c271=""
                  
                >
                  <span _ngcontent-nbk-c271="">Erklärung</span
                  ><span
                    _ngcontent-nbk-c271=""
                    
                  ></span></button
                ><!---->
              </div>
            </div>
            <!----><!----><!---->
            <div _ngcontent-nbk-c271="" id="questionnaireNav">
              <div
                _ngcontent-nbk-c271=""
                id="questionnaireNavClasses"
                
              >
                <button _ngcontent-nbk-c271="" >
                  <span _ngcontent-nbk-c271="" 
                    >Grundstoff</span
                  ><!----><!----><!----></button
                ><button _ngcontent-nbk-c271="" >
                  <!----><span _ngcontent-nbk-c271="" 
                    >B</span
                  ><!----><!----></button
                ><!---->
              </div>
              <!---->
              <div
                _ngcontent-nbk-c271=""
                id="questionnaireNavButtons"
                
              >
                <button _ngcontent-nbk-c271="" >
                  1
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  2
                  <i
                    _ngcontent-nbk-c271=""
                    
                  ></i
                  ><!----></button
                ><button _ngcontent-nbk-c271="" >
                  3
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  4
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  5
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  6
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  7
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  8
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  9
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  10
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  11
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  12
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  13
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  14
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  15
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  16
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  17
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  18
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  19
                  <!----></button
                ><button _ngcontent-nbk-c271="" >
                  20
                  <!----></button
                ><!---->
              </div>
              <!---->
            </div>
          </div>
          <!---->
        </div>
        <!----><!---->
      </div>
      <!----><!---->
    </div></kendo-dialog
  ><!----></tt-questionnaire
>

If you find a way let me know I would also rep it :)

CodePudding user response:

You have two mistakes in one line.

First: there is no name="..." in HTML, and if you want to use '' then you have to use find_element_by_class() instead of find_element_by_name()

Second: this function needs only "questionNumber" without class=

find_element_by_class("questionNumber")

CodePudding user response:

[19132:16448:0529/222120.901:ERROR:device_event_log_impl.cc(214)] [22:21:20.901] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.
C:\Users\Night\Desktop\idk.py:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  lol = driver.find_element_by_id("email")
C:\Users\Night\Desktop\idk.py:12: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  lol = driver.find_element_by_id("password")
C:\Users\Night\Desktop\idk.py:14: DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead
  driver.find_element_by_xpath('//button[@type="submit"]').click()
C:\Users\Night\Desktop\idk.py:18: DeprecationWarning: find_element_by_class_name is deprecated. Please use find_element(by=By.CLASS_NAME, value=name) instead
  elements = driver.find_element_by_class_name("questionNumber")
Traceback (most recent call last):
  File "C:\Users\Night\Desktop\idk.py", line 18, in <module>
    elements = driver.find_element_by_class_name("questionNumber")
  File "C:\Users\Night\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 766, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\Night\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1251, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Night\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Night\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".questionNumber"}
  (Session info: chrome=102.0.5005.63)
Stacktrace:
Backtrace:
        Ordinal0 [0x0079D953 2414931]
        Ordinal0 [0x0072F5E1 1963489]
        Ordinal0 [0x0061C6B8 837304]
        Ordinal0 [0x00649500 1021184]
        Ordinal0 [0x0064979B 1021851]
        Ordinal0 [0x00676502 1205506]
        Ordinal0 [0x006644E4 1131748]
        Ordinal0 [0x00674812 1198098]
        Ordinal0 [0x006642B6 1131190]
        Ordinal0 [0x0063E860 976992]
        Ordinal0 [0x0063F756 980822]
        GetHandleVerifier [0x00A0CC62 2510274]
        GetHandleVerifier [0x009FF760 2455744]
        GetHandleVerifier [0x0082EABA 551962]
        GetHandleVerifier [0x0082D916 547446]
        Ordinal0 [0x00735F3B 1990459]
        Ordinal0 [0x0073A898 2009240]
        Ordinal0 [0x0073A985 2009477]
        Ordinal0 [0x00743AD1 2046673]
        BaseThreadInitThunk [0x762FFA29 25]
        RtlGetAppContainerNamedObjectPath [0x772A7A7E 286]
        RtlGetAppContainerNamedObjectPath [0x772A7A4E 238]

Since everyone asking for the full Error report

here is the full log @furas

i tried your method about here but it doesnt seems to work

here is the code from the website

<div >
        <div
          _ngcontent-nbk-c271=""
          id="questionnaireContainer"
          
          style=""
        >
          <!---->
          <div
            _ngcontent-nbk-c271=""
            id="questionnaire"
            
            style=""
          >
            <div _ngcontent-nbk-c271="" id="questionnaireHeader">
              <div _ngcontent-nbk-c271="" >
                <span _ngcontent-nbk-c271="" 
                  >Grundstoff<!----><!----><!----></span
                ><span _ngcontent-nbk-c271="" 
                  >1.2.05-101</span
                ><span _ngcontent-nbk-c271="" 
                  >Aufgabe 1</span
                ><span _ngcontent-nbk-c271="" >Punkte: 4</span>
              </div>
              <ul _ngcontent-nbk-c271="" >
                <tt-language-dropdown _ngcontent-nbk-c271="" _nghost-nbk-c241=""
                  ><!----></tt-language-dropdown
                >
              </ul>
            </div>
            <div _ngcontent-nbk-c271="" id="questionnaireContent">
              <!----><tt-questionnaire-question
                _ngcontent-nbk-c271=""
                _nghost-nbk-c270=""
                
                ><h2 _ngcontent-nbk-c270="" >
                  <tt-tts _ngcontent-nbk-c270="" _nghost-nbk-c243=""
                    ><div _ngcontent-nbk-c243="">
                      <span _ngcontent-nbk-c270=""
                        >Wo ist das Überholen verboten?</span
                      >
                    </div>
                    <!----></tt-tts
                  >
                </h2>
                <div _ngcontent-nbk-c270="" >
                  <div _ngcontent-nbk-c270="" >
                    <!----><!----><!---->
                  </div>
                  <div _ngcontent-nbk-c270="" >
                    <tt-questionnaire-selection-question
                      _ngcontent-nbk-c270=""
                      _nghost-nbk-c268=""
                      
                      ><div
                        _ngcontent-nbk-c268=""
                        
                      >
                        <!---->
                        <ul _ngcontent-nbk-c268="" >
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox1"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox1"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >In allen Einbahnstraßen</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox2"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox2"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >Wo die Verkehrslage unklar ist</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <li _ngcontent-nbk-c268="" >
                            <span
                              _ngcontent-nbk-c268=""
                              
                              ><input
                                _ngcontent-nbk-c268=""
                                type="checkbox"
                                autocomplete="off"
                                id="answerCheckbox3"
                                 /><label
                                _ngcontent-nbk-c268=""
                                for="answerCheckbox3"
                              ></label></span
                            ><!----><span
                              _ngcontent-nbk-c268=""
                              
                              ><tt-tts
                                _ngcontent-nbk-c268=""
                                _nghost-nbk-c243=""
                                ><div _ngcontent-nbk-c243="">
                                  <span _ngcontent-nbk-c268=""
                                    >Wo der Gegenverkehr behindert werden
                                    könnte</span
                                  >
                                </div>
                                <!----></tt-tts
                              ></span
                            ><!----><!---->
                          </li>
                          <!---->
                        </ul>
                      </div></tt-questionnaire-selection-question
                    ><!----><!---->
                  </div>
                </div>
                <!----><!----></tt-questionnaire-question
              ><!---->
            </div>
  • Related