简体   繁体   中英

Why send_keys function doesn't work in Python Selenium?

i habe a problem in my code.

 for photolink in all_links:

        self.browser.get(photolink)         #Link öffnen
        time.sleep(random.randint(5, 6))

        #liken
        self.browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[1]/article/div/div[2]/div/div[2]/section[1]/span[1]/button').click()
        time.sleep(random.randint(1, 3))

        #kommentieren
        commentbox = self.WaitforObject(By.CLASS_NAME,"Ypffh")
        commentbox.click()
        time.sleep(random.randint(1, 2))

        #commentbox.send_keys(self.config.Random_Comment())
        commentbox.send_keys("Test")
        commentbox.send_keys(Keys.ENTER)
        time.sleep(random.randint(3, 5))

        c = c + 1
        print("Liked ", c, " photos")

    time.sleep(random.randint(1, 3))

It works until "commentbox = self.WaitforObject(By.CLASS_NAME,"Ypffh")", so the curser is in the textfield. But when I want to write someting with "send_keys", it brokes.

Screenshot

  1. Make sure you have imported the keys library from selenium

    from selenium.webdriver.common.keys import Keys

  2. use find element by xpath (if you use xpath to send keys you dont have to click onto it first)

    commentbox = self.browser.find_element_by_xpath("xpath here")

    commentbox.send_keys("blah blah blah")

You may also have to click on the post button instead of using Keys.ENTER

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