简体   繁体   English

为什么 send_keys function 在 Python Selenium 中不起作用?

[英]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.它一直有效到“commentbox = self.WaitforObject(By.CLASS_NAME,"Ypffh")”,因此光标位于文本字段中。 But when I want to write someting with "send_keys", it brokes.但是当我想用“send_keys”写一些东西时,它就坏了。

Screenshot截屏

  1. Make sure you have imported the keys library from selenium确保您已从 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)使用 xpath 的查找元素(如果您使用 xpath 发送密钥,则不必先单击它)

    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您可能还必须单击发布按钮而不是使用 Keys.ENTER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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