简体   繁体   中英

How to click the login button using Selenium with Python

<div class="login" id="login">
<div class="top">
    <div class="logo"></div>
    <div class="language">
        <div class="language-show" ng-click="showLanguageList($event)"><span title="English" class="current-language" id="current_language">English</span></div>
        <div class="language-list" id="language_list" style="display: none;" ng-click="changeLanguage($event)"><div title="English" class="out" id="en" onmouseover='this.className="over"' onmouseout='this.className="out"'>English</div></div>
    </div>
</div>
<table class="middle" border="0" cellspacing="0" cellpadding="0">
    <tbody><tr>
        <td class="login-l">&nbsp;</td>
        <td class="login-m">
            <div class="login-part">
                <div class="line"></div>
                <div class="login-error">
                    <div class="inputValidTip ng-hide" ng-show="szErrorTip!=''"><i class="error"></i><label class="ng-binding"></label></div>
                </div>
                <div class="login-user">
                    <input class="login-input ng-valid ng-dirty" id="username" type="text" maxlength="32" placeholder="User Name" autocomplete="off" ng-model="username">
                    <i class="icon-user"></i>
                </div>
                <div class="login-item">
                    <input class="login-input ng-pristine ng-valid ng-scope" style="display: none;" type="text" maxlength="16" ng-model="password"><input class="login-input ng-valid ng-dirty" id="password" type="password" maxlength="16" placeholder="Password" ng-model="password" pigsney=""><span class="pigsney close" style="left: 210px; top: 7px; display: inline-block;" onselectstart="return false;"></span>
                    <i class="icon-pass"></i>
                </div>
                <div class="login-item bottom">
                    <span class="pwd-link ng-binding ng-hide" ng-click="forgetPwd()" ng-show="oCap.bSptGuidImport || oCap.bSptQAReset" ng-bind="oLan.forgetPwd">Forgot password?</span>
                    <button class="btn btn-primary login-btn" type="button" ng-click="login()"><label class="ng-binding" ng-bind="oLan.login">Login</label></button>
                </div>
                <!--<div class="login-item anonymous" ng-show="anonymous">
                    <span ng-bind="oLan.anonymous" ng-click="login('anonymous')"></span>
                </div>-->
            </div>
        </td>
        <td class="login-r">&nbsp;</td>
    </tr>
</tbody></table>
<div class="footer" id="footer"></div>

I want to lick login button.

button class="btn btn-primary login-btn" type="button" ng-click="login()

How can I find this element? I used Python. And, find_element_by_css_selector() .

The web source is from some embedded machine. So, I can't use the Chrome. I can't find the xpath on IE because I am a beginner.

Please, help me!

You can click on the element using its class name, you can do it like:

button = driver.find_element_by_xpath("//button[text()='Login']")
button.click()

The desired element is a Angular element so click() the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.login-btn[ng-click^='login']>label.ng-binding[ng-bind$='login']"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary login-btn' and starts-with(@ng-click, 'login')]/label[@class='ng-binding' and text()='Login']"))).click()
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

You can locate an element with "Login" text in its child. You can also add a scroll to the element before the click

element = driver.find_element_by_xpath('//button[label[.="Login"]]')
ActionChains(driver).move_to_element(element).click().perform()

First wait element to be clickable

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, //button[text()='Login']"")))

Then use some JSExecutor to click it.

element = driver.find_element_by_xpath("//button[text()='Login']")
driver.execute_script("arguments[0].click();", element) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM