te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Python | Selenium | Adjusting the value on a price slider with minmax values - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python | Selenium | Adjusting the value on a price slider with minmax values - Stack Overflow

programmeradmin2浏览0评论

I'm working on a practice Selenium script to adjust the value of a price slider that has min and max value sliders on the same bar. The code technically works, and I see the max slider value adjust, but not to the exact value I want. Is there a better way I can be doing this?

The code I'm working with at the moment is:

def test_priceScale():
    driver.get('')

    slider = driver.find_element(By.CSS_SELECTOR, "span[aria-label = 'ngx-slider-max']")

    current_value = float(slider.get_attribute('aria-valuenow'))
    max_value = float(slider.get_attribute('aria-valuemax'))

    target_value = 10

    slider_width = slider.size["width"]
    offset = (target_value - current_value) / (max_value * slider_width)

    actions = ActionChains(driver)
    actions.click_and_hold(slider).move_by_offset(offset, 0).release().perform()

My hope is to find a solution that if I change the value of target_value, I can see the slider adjust to that specific price.

I'm working on a practice Selenium script to adjust the value of a price slider that has min and max value sliders on the same bar. The code technically works, and I see the max slider value adjust, but not to the exact value I want. Is there a better way I can be doing this?

The code I'm working with at the moment is:

def test_priceScale():
    driver.get('https://www.practicesoftwaretesting')

    slider = driver.find_element(By.CSS_SELECTOR, "span[aria-label = 'ngx-slider-max']")

    current_value = float(slider.get_attribute('aria-valuenow'))
    max_value = float(slider.get_attribute('aria-valuemax'))

    target_value = 10

    slider_width = slider.size["width"]
    offset = (target_value - current_value) / (max_value * slider_width)

    actions = ActionChains(driver)
    actions.click_and_hold(slider).move_by_offset(offset, 0).release().perform()

My hope is to find a solution that if I change the value of target_value, I can see the slider adjust to that specific price.

Share Improve this question asked Feb 17 at 18:17 SamKablamSamKablam 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This was challenging...

  • I first tried to use the size of the slider in px vs the desired value to scale my clicks and that got me close but not exact... and I wanted exact, if I could get it.

  • Next I tried to see if there was a way to just set the desired value directly using attributes on the bubbles, etc. but I couldn't find a way that worked (and generally this is a bad idea because it's not how users interact with the site and can cause problems).

  • The last thing I tried was a drag and drop of 1px at a time to inch towards the desired value, checking the displayed text to see if I reached the desired value. This is what I ended up using, with some tweaks.

I kinda went overboard for a typical SO answer... I created a page object for the home page because I thought it would help with code reuse, etc. The test itself is very simple and short... the page object holds the guts of the logic, as it should.

The basic concept is I set a step value to 5 px. I step to the right until I've passed the desired value. Then I step left until I've passed the desired value. If I haven't found the desired value, I decrease the step value by 1 and repeat... stepping right then left. This continues until the desired value is reached or the step value = 0.

I've tried several different values for min and max and it's worked each time. You could increase the initial step value to get there faster, if you want.

Here's the working code:

slider_test.py

from selenium import webdriver

from home_page import HomePage

driver = webdriver.Chrome()

url = "https://www.practicesoftwaretesting/"
driver.get(url)

desired_min_value = 51
desired_max_value = 151

homePage = HomePage(driver)
homePage.set_limit(desired_min_value, homePage.Limit.MIN)
homePage.set_limit(desired_max_value, homePage.Limit.MAX)

driver.quit()

home_page.py

import time
from enum import Enum
from selenium.webdrivermon.action_chains import ActionChains
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver


class HomePage:
    def __init__(self, driver: webdriver):
        self.driver = driver
        self.label_max_locator = (By.CSS_SELECTOR, "span.ngx-slider-model-high")
        self.label_min_locator = (By.CSS_SELECTOR, "span.ngx-slider-model-value")
        self.slider_max_locator = (By.CSS_SELECTOR, "span.ngx-slider-pointer-max")
        self.slider_min_locator = (By.CSS_SELECTOR, "span.ngx-slider-pointer-min")
        self.wait = WebDriverWait(driver, 10)
        self.Limit = Enum("Limit", [("MIN", 1), ("MAX", 2)])  # an enum to set MIN or MAX

    def set_limit(self, desired_value: int, limit):
        match limit:
            case self.Limit.MIN:
                slider_locator = self.slider_min_locator
                label_locator = self.label_min_locator
            case self.Limit.MAX:
                slider_locator = self.slider_max_locator
                label_locator = self.label_max_locator

        bubble = self.wait.until(EC.visibility_of_element_located(slider_locator))
        current_value = self.wait.until(EC.visibility_of_element_located(label_locator))
        actions = ActionChains(self.driver)
        step = 5
        while step > 0:
            while int(current_value.text) < desired_value:
                actions.drag_and_drop_by_offset(bubble, step, 0).perform()
                time.sleep(0.1)
            while int(current_value.text) > desired_value:
                actions.drag_and_drop_by_offset(bubble, -step, 0).perform()
                time.sleep(0.1)
            step = step - 1
发布评论

评论列表(0)

  1. 暂无评论