【问题标题】:Selenium Python Slow character speedSelenium Python 字符速度慢
【发布时间】:2021-08-22 18:12:17
【问题描述】:

有没有办法让我的发送键在键入字符时变慢。 很难减慢它的打字速度。 我尝试使用 Sleep 方法,但不起作用。

A('Starting up browser...')
e=l()
e.add_argument('disable-infobars')
B=k.Chrome(options=e)
B.implicitly_wait(5)
n=[0.02,0.04,0.03]
o=[F,J]
def f(word):
        A=word;A=list(A);C=[]
        for E in i(10):C.append([G(U),d.BACKSPACE])
        F=G(o)
        if F:
                I=G([1,2,3])
                for E in i(I):A.insert(G(i(j(A))),G(C))
                A=[C for B in A for C in B]
        D=B.find_element_by_css_selector('.selfTurn input')
        for J in A:K=G(n);D.send_keys(J);H(K)
        H(0.2);D.send_keys(d.ENTER)
A('Navigating to the JKLM website...')
B.get(m)
g=J               

【问题讨论】:

  • 由于缺乏正确的格式和使用单字母变量名,您的代码很难理解。

标签: python selenium selenium-webdriver


【解决方案1】:

您可以使用 action_chains send_keys 方法将键发送到元素,而不是使用 selenium driver send_keys 方法。
我会在你的代码上显示,但它似乎太不正常,所以我会给你一个一般的例子。
您需要添加导入

from selenium.webdriver.common.action_chains import ActionChains

然后用

初始化actions对象
actions = ActionChains(driver)

然后将带有操作的文本发送到.selfTurn inputcss_selector 定位的方法

element = driver.find_element_by_css_selector('.selfTurn input')
action.click(element)
action.send_keys("your_text")
action.perform()

【讨论】:

    【解决方案2】:

    我知道您想像打字一样发送按键,因为如果打字速度比正常快,会检测到,对吗?

    在 selenium 中,您可以使用 send_keys,但没有与类型关联的时间间隔参数。见Selenium Document

    所以使用自定义send_keys 并进行如下编辑。

    def send_keys(self, *keys_to_send, time_interval):
        """
        Sends keys to current focused element.
    
        :Args:
         - keys_to_send: The keys to send.  Modifier keys constants can be found in the
           'Keys' class.
        """
        typing = keys_to_typing(keys_to_send)
    
        for key in typing:
            self.key_down(key)
            self.key_up(key)
            time.sleep(time_interval)  # what I insert to delay input time
    
        return self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-17
      • 2016-05-28
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2018-03-09
      相关资源
      最近更新 更多