【问题标题】:Increasing clicking speed through WebDriverwait using Selenium使用 Selenium 通过 WebDriverwait 提高点击速度
【发布时间】:2019-09-22 11:42:36
【问题描述】:

是否可以缩短 Selenium 中每次点击之间的时间间隔?据我所知,默认情况下,webdriver 会尝试每半秒点击一次。

【问题讨论】:

  • 默认元素 lookup(假设您定义了等待)是半秒。只有当您使用click() 方法时,驱动程序才会尝试单击,并且只有一次。

标签: selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

点击()

click() 方法单击定义为的元素:

def click(self):
    """Clicks the element."""
    self._execute(Command.CLICK_ELEMENT)

Command.CLICK_ELEMENT 仅在调用click() 时执行。


大概您指的是在通过WebDriverWaitexpected_conditions 返回元素后调用click() 方法,如下所示:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#btn"))).click()

WebDriverWait的构造函数是:

class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

where:
    driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
    timeout - Number of seconds before timing out
    poll_frequency - sleep interval between calls By default, it is 0.5 second.
    ignored_exceptions - iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only

所以您所指的 时间段 可能是 poll_frequency,默认设置为 0.5 秒500毫秒,这是两个背靠背find_element_by_* 调用之间的时间间隔。

如果您的用例是调整/增加/减少 poll_frequency,您可以将 constructor 更改为:

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):

    self._driver = driver
    self._timeout = timeout
    self._poll = poll_frequency
    # avoid the divide by zero
    if self._poll == 0:
        self._poll = POLL_FREQUENCY
    exceptions = list(IGNORED_EXCEPTIONS)
    if ignored_exceptions is not None:
        try:
        exceptions.extend(iter(ignored_exceptions))
        except TypeError:  # ignored_exceptions is not iterable
        exceptions.append(ignored_exceptions)
    self._ignored_exceptions = tuple(exceptions)

【讨论】:

    【解决方案2】:

    你可以使用JS函数来实现这个

    num = 0; //to count clicks
    var element = document.getElementsByClassName('yourEl');
    function clicker () {element.click(); num+=1;}
    setInterval(clicker,0.2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 2020-10-26
      • 2020-11-26
      相关资源
      最近更新 更多