Yang Wu
Yang Wu

Reputation: 33

python selenium xpath could not locate elements

I'm new to Python and Selenium, trying to locate the register input field but it does not work. However, find_element_by_xpath('//input[@name=" password"]') works on the login input filed since the input file has an identical class and name, I'm not sure how to select the input field in the register div. Here is HTML and python code!

<div class="sign-in j-sign-in">
                        <h4>Sign In</h4>
                        <p class="login-error"></p>
                        <form>
                            <div class="c-input">
                                <div class="input-title">Email Address:
                                    <i class="input-error-txt" style="display: none;">*</i>
                                </div>
                                <input placeholder="" autocomplete="" type="email" name="email" class="">
                                <ul class="emaillist" style="display: none;">
                                 
                                </ul>
                                <!---->
                                <!---->
                            </div>
                            <div class="c-input">
                                <div class="input-title">Password:
                                    <i class="input-error-txt" style="display: none;">*</i></div> 
                                    <input placeholder="" autocomplete="" type="password" name="password" class="">
                                <!---->
                                <!---->
                                <!---->
                            </div>
                        </form>
                <div class="col-xs-1 sign-in-rightborder"></div>
                <div class="col-xs-4 col-xs-offset-1">
                    <div class="sign-in j-newto-shein">
                        <h4>New to SHEIN</h4>
                        <form>
                            <div class="c-input">
                                <div class="input-title">Email Address:<i class="input-error-txt" style="display: none;">*</i></div> <input placeholder="" autocomplete="" type="email" name="email" class="input-error">
                                <ul class="emaillist" style="display: none;"></ul>
                                <!---->
                                <div class="input-error-txt">Please enter an email address.</div>
                            </div>
                            <div class="c-input">
                                <div class="input-title">Password:<i class="input-error-txt" style="display: none;">*</i></div> <input placeholder="" autocomplete="" type="password" name="password" class="">
                                <!---->
                                <!---->
                                <!---->
                            </div>
                            <div class="normal-info she-hide">
                                <div class="">
                                    <p class="">· 8 characters minimum</p>
                                    <p class="">· At least one letter</p>
                                    <p class="">· At least one number</p>
                                </div>
                            </div>
                            <div class="c-input input-mar">
                                <div class="input-title">Confirm Password:<i class="input-error-txt" style="display: none;">*</i></div> <input placeholder="" autocomplete="" type="password" name="cfPassword" class="">
                                <!---->
                                <!---->
                                <!---->
                            </div>
    
    
email = browser.find_element_by_xpath(
            '//div[@class="sign-in j-sign-in"]/input[@name="email"]')
            email.clear()
            email.send_keys("[email protected]")
            password = browser.find_element_by_xpath(
            '//div[@class="sign-in j-newto-shein"]/input[@name="password"]')
            password.clear()
        password.send_keys("112233")
        
        sign = browser.find_element_by_xpath(
            '//div[@class="sign-in-btn-wrapper"]/button[@class="she-btn-black she-btn-l she-btn-block"]').click()

And error I'm getting:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

Upvotes: 0

Views: 62

Answers (2)

pavelsaman
pavelsaman

Reputation: 8362

email = browser.find_element_by_xpath('//div[@class="sign-in j-sign-in"]/input[@name="email"]');

This won't work because input is not directly under //div[@class="sign-in j-sign-in"]. You'd need to write it like this:

email = browser.find_element_by_xpath('//div[@class="sign-in j-sign-in"]//input[@name="email"]');

to get to the input.

Same goes for getting to the password input field.

An element with class sign-in-btn-wrapper can't be found in your HTML code, so I can't say what's wrong with the last xPath you have in your code, but I suspect it might be the same problem.

Upvotes: 1

Andreas B
Andreas B

Reputation: 42

Hello would be nice if you could provide the full HTML because I couldn't fully reproduce your website. So the button is left out you would have to add the button Code back in

So I just copied the XPath instead of the full XPath that did the trick for me.

Here is the Code:

from selenium import webdriver

options = webdriver.ChromeOptions()


browser = webdriver.Chrome(options=options)
browser.get("http://127.0.0.1")


email = browser.find_element_by_xpath(
'/html/body/div/form/div[1]/input')
email.clear()
email.send_keys("[email protected]")
password = browser.find_element_by_xpath(
'/html/body/div/form/div[2]/input')
password.clear()
password.send_keys("112233")

Upvotes: 0

Related Questions