0

This is the source code of the webpage ( https://eu.luxpowertek.com/WManage/web/login ) I am trying to login in and click the submit button.

<div id="loginFormContainer" style="margin: 0 auto; width: 450px; position: relative;">
    <form id="loginHolder" action="/WManage/web/login" method="post">
        <h2 class="title" style="text-align: center; margin-top: 0;">
            Monitor Center
        </h2>
        <div class="form-horizontal" style="padding: 10px 10px 0 10px;">
            
            <div class="form-group">
                <div class="input-group col-sm-12">
                    <span class="input-group-addon" style="width: 45px;"><img src="https://resource.solarcloudsystem.com/WManage/web/img/format/icons/user.png?v=2.0.4.4" height="16" /></span>
                    <input type="text" class="form-control input-lg" id="account" name="account" value="" maxLength="30" autofocus autocomplete="off" placeholder="Please enter username">
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 10px;">
                <div class="input-group col-sm-12">
                    <span class="input-group-addon" style="width: 45px;"><img src="https://resource.solarcloudsystem.com/WManage/web/img/format/icons/key.png?v=2.0.4.4" height="16" /></span>
                    <input type="password" class="form-control input-lg" id="password" name="password" maxLength="50" placeholder="Please enter password">
                </div>
                
                
                
                
            </div>
            
            <div style="width: 100%; margin-bottom: 10px;">
                <a href="forgetPassword" style="float: right; color: inherit;" target="_blank">Forget password?</a>
                <div style="clear: both;"></div>
            </div>
            
            <div class="form-group">
                <button type="submit" class="btn btn-lg btn-success" style="width: calc(100% - 115px); margin-right: 5px;">Sign in</button>
                <a class="btn btn-lg btn-default" style="width: 106px;" href="/WManage/web/register">Register</a>
            </div>
            
            <div class="form-group" style="margin-bottom: 5px;">
                
                    <a href="/WManage/web/login/viewDemoPlant?customCompany=" type="button" class="btn btn-lg btn-success" style="width: calc(100% - 115px); margin-right: 5px;">Visit demo station</a>
                
                
                <a class="btn btn-lg btn-default" style="width: 106px;" href="/WManage/web/warranty/register">Warranty</a>
            </div>
        </div>
    </form>
</div>

Here is my python code with all the various different ways I have tried so far to click the button. The username and password are just dummy code in this sample.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
service = Service('/usr/bin/chromedriver')

driver = webdriver.Chrome(service=service)
driver.get('https://eu.luxpowertek.com/WManage/web/login')


username = "[email protected]"
password = "kinghferyrt"

# find username/email field and send the username itself to the input field
driver.find_element("id", "account").send_keys(username)
# find password input field and insert password as well
driver.find_element("id", "password").send_keys(password)
# click login button
driver.find_element(By.XPATH("(//button[contains(text(),'Sign In')])[2]"));

So the actual submit button has no ID and this is where I think I am getting confues will all the different ways of selecting it like by Selector, By XPATH by.xxxxx etc

Sign in

Any help would be most appreciated, also it appears that the code syntax of selenium has changed over the years as many code samples I have found do not work any more like driver.find_element_by_xpath which is now (I believe) driver.find_element(By.XPATH,xxxx)

3 Answers 3

0

I'm not a Selenium expert, but here's what I think is happening. Updating as I go along…

driver.find_element(By.XPath, 'Sign In'.Click()) fails for two reasons:

driver.find_elements(By.XPATH, "//*[@href='../cgi-bin/cartReset']")[1].click():

  • There's no href attribute with an exact value of ../cgi-bin/cartReset, so this can't match anything.
  • Tries to click the second element. Python lists are zero-indexed, and you probably wanted [0].

driver.find_elements(By.XPATH,"//form[@type='Submit']").click():

  • The XPath would match a <form> element with an attribute type="Submit". But the type="Submit" attribute is on the button, not the form.

driver.find_element(By.CLASS_NAME, "btn btn-lg btn-success").click():

  • I don't think the class name selector takes a string with multiple classes like this.

driver.find_element(By.xpath("//input[@type='submit']")).submit();:

  • You don't have any input elements with type="submit", only a button element.
0

You can uniquely identify the button using the element type alongside the class.

enter image description here

You can then use this:

driver.find_element_by_css_selector("button.btn-success").click()
0

Recommend not using xpath, here is a document explaining selector speeds and reliability: here

Using css selector will be more reliable and faster, would also suggest changing to the below, which will (should) fix your issue and add some extra useful bits.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

# Initialize the WebDriver instance using webdriver-manager
driver = webdriver.Chrome(ChromeDriverManager().install())

# Navigate to the login page
driver.get("https://eu.luxpowertek.com/WManage/web/login")

# Wait for the username field to be present
wait = WebDriverWait(driver, 10)  # 10 seconds timeout
username_field = wait.until(EC.presence_of_element_located((By.ID, "account")))
username_field.send_keys("[email protected]")

# Wait for the password field to be present and input the password
password_field = wait.until(EC.presence_of_element_located((By.ID, "password")))
password_field.send_keys("kinghferyrt")

# Wait for the sign-in button to be clickable and click it
sign_in_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-success")))
sign_in_button.click()
  
# Finally, close the browser. You might want to remove this during development.
driver.quit()

(I haven’t tested this, but will roughly be what you’re looking for - this also uses chrome driver manager which means you don’t need to point to a local driver, it’ll download and cache it on the fly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.