Search code examples
pythonseleniumxpath

How to click the login button with selenium?


How do I click the submit button in this form with selenium?

HTML

<form name="loginform" method="post" action="https://ap.hongik.ac.kr/login/LoginExec3.php"
  onsubmit="chkFrontLogin();return false;">
  <div class="t">
    <div class="r">
      <div class="c q1">
        <div class="id"><label>id</label><input type="text" name="USER_ID"></div>
        <div class="pw"><label>password</label><input type="password" name="PASSWD"></div>
      </div>
      <div class="c q2">
        <button type="submit" class="button submit"><span class="lbl">login</span></button>
      </div>
    </div>
  </div>
  <input type="hidden" name="Refer" value="http://gw.hongik.ac.kr/index.php">
</form>

My code

driver.find_element_by_xpath("//div[@id='main']/div[@class='align']/table/tbody/tr/td/form/button").click()

I used xpath and searched stackoverflow.

I cannot find a clue for the error(NosuchElementException).


Solution

  • Try this instead

    driver.find_element_by_xpath("/html/body/form/div/div/div[2]/button/span")
    

    An easy way to locate the correct xpath is to right-click and [Inspect] element in Chrome, then right-click the HTML code segment and [Copy] -> [Copy XPath].