qdyxmas's Room

Done is better than perfect...

selenium-how-to-automate-html5-input-range-element-on-webdriver

ITERATIONS TO THE SOLUTION

方法1
def set_range(el, val):
    driver.execute_javascript("arguments[0].value = arguments[1];", el, val);


方法2
def set_range(el, val):
    minval = float(el.get_attribute("min") or 0)
    maxval = float(el.get_attribute("max") or 100)
    v = max(0, min(1, (float(val) - minval) / (maxval - minval)))
    width = el.size["width"]
    target = float(width) * v
    ac = ActionChains(driver)
    ac.move_to_element_with_offset(el, target, 1)
    ac.click()
    ac.perform()

方法3
def set_range(el, val):
    # The adjustment helper to drag the slider thumb
    def adjust(deltax):
        if deltax < 0:
            deltax = int(math.floor(min(-1, deltax)))
        else:
            deltax = int(math.ceil(max(1, deltax)))
        ac = ActionChains(driver)
        ac.click_and_hold(None)
        ac.move_by_offset(deltax, 0)
        ac.release(None)
        ac.perform()

    minval = float(el.get_attribute("min") or 0)
    maxval = float(el.get_attribute("max") or 100)
    v = max(0, min(1, (float(val) - minval) / (maxval - minval)))
    width = el.size["width"]
    target = float(width) * v

    ac = ActionChains(driver)

    # drag from min to max value, to ensure oninput event
    ac.move_to_element_with_offset(el, 0, 1)
    ac.click_and_hold()
    ac.move_by_offset(width, 0)

    # drag to the calculated position
    ac.move_to_element_with_offset(el, target, 1)

    ac.release()
    ac.perform()

    # perform a binary search and adjust the slider thumb until the value matches
    while True:
        curval = el.get_attribute("value")
        if float(curval) == float(val):
            return True
        prev_guess = target
        if float(curval) < float(val):
            minguess = target
            target += (maxguess - target) / 2
       else:
           maxguess = target
           target = minguess + (target - minguess) / 2
       deltax = target-prev_guess
       if abs(deltax) < 0.5
           break # cannot find a way, fallback to javascript.

       time.sleep(0.1) # Don't consume CPU too much

       adjust(deltax)

    # Finally, if the binary search algoritm fails to achieve the final value
    # we'll revert to the javascript method so at least the value will be changed
    # even though the browser events wont' be triggered.

     # Fallback
    driver.execute_script("arguments[0].value=arguments[1];", el, val)
    curval = el.get_attribute("value")
    if float(curval) == float(val):
        return True
    else:
        raise Exception("Can't set value %f for the element." % val)

标签 

14 评论

  1. PxqOT
    /回复

    Drug information sheet. Cautions. <a href="https://lyricafor.top">lyrica sales</a> in US Actual about drug. Read now.

  2. JaniceWeS
    /回复

    can women take cialis <a href="https://mycialistabs.com/">what is tadalafil used for</a> cialis back pain

  3. TiffanyreD
    /回复

    best time to take cialis 20mg <a href="https://toptadalafiltabs.com/">do you need a prescription for cialis</a> cialis vs viagra reddit

  4. Stellavam
    /回复

    how to write a synthesis essay ap lang <a href="https://topessaywriterfas.com/">top essay writer f a s</a> how to write a conclusion for a history essay

  5. RebeccaMew
    /回复

    essay service in miami <a href="http://writemyessayslfd.com/">writing a good college essay</a> essay writing service australian

  6. FrancesHix
    /回复

    viagra over the counter <a href="https://mrviagrashop.com">mrviagrashop.com</a> sildenafil citrate 100mg

  7. Martinanulse
    /回复

    canadian pharmacy ezzz viagra <a href="https://mysildenafilkr.com/">https://mysildenafilkr.com/</a> generic viagra walmart

  8. VxsGC
    /回复

    cover letter font <a href="http://coverletterforresumetop.com/">cover letter for employment</a> medical assistant cover letter

  9. Kathryngogue
    /回复

    cialis 40 mg <a href="http://tadalafilled.com/">discount cialis</a> does daily cialis lower blood pressure

  10. CstTK
    /回复

    resume reddit <a href="https://writingacoverletteronline.com/">it professional resume</a> nurse cover letter

  11. DuuMG
    /回复

    generic viagra over the counter <a href="https://edviagramaster.net/">sildenafil pronunciation</a> women in viagra commercials

  12. KujAM
    /回复

    cialis price costco <a href="https://cialisortadalafil.com/">where to buy tadalafil</a> cialis discount coupons

  13. JnvEU
    /回复

    viagra para mujer <a href="https://sildenafilviagratop.com">viagra sex</a> female viagra over the counter

  14. RvdSS
    /回复

    cialis on line <a href="https://topcialistabs.com/">cialis 20mg</a> canadian online pharmacy cialis

评论