简体   繁体   中英

Python Selenium can't find element or click element

So I am trying to make my first Instagram bot and I've got the driver to open Instagram. But I can't make my script enter anything or click the username or password boxes.

So now my question is: Why doesn't my script enter anything or (when using .click() ) clicks the password/username boxes.

class instaBot():
    def __init__(self, username, password):

         self.driver = webdriver.Chrome("/Users/blaze/PycharmProjects/InstagramBot/Drivers/chromedriver")
         self.driver.get("https://www.instagram.com")
         time.sleep(5)
         loginbox = self.driver.find_elements_by_xpath("//input[@name = 'username']")
         loginbox.send_keys(username)
         time.sleep(1)
         loginbox2 = self.driver.find_elements_by_xpath("//input[@name = 'password']")
         loginbox2.send_keys(password)

instaBot('Code', 'Passerwoee')

Elements:

<input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value=""/>

You are using find_elements while finding the element, find_elements return the list of the element, you need to use find_element to fetch the element and then you need to operate on it.
You can do it like:

 loginbox = self.driver.find_element_by_xpath("//input[@name = 'username']")
 loginbox.send_keys(username)
 time.sleep(1)
 loginbox2 = self.driver.find_element_by_xpath("//input[@name = 'password']")
 loginbox2.send_keys(password)

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