Selenium [非常急] js 语句无法对下拉选择框产生影响的问题

醋精测试媛 · 2020年11月09日 · 最后由 醋精测试媛 回复于 2020年11月10日 · 1697 次阅读

如图所示,一个普通的 readonly 的 input 下拉框,当我用 js 语句强行使得 input 框的值为某个值时,输入该 input 元素的值,是没有错的,但是我点击按钮搜索,却不是以该条件搜索的,请问有没有什么办法可以解决这个问题呢?

日历的下拉框控件也是这样的,操作后控件被正常赋值。但此时点击提交或在其他输入框输入内容,日历控件日期这一搜索条件不起效果

普通的选择下拉框尚可点击下拉用鼠标选择,但是日历的下拉框点击起来非常麻烦,一般是用 send_keys 吧。
如果有没有说清楚的地方可以问问题,我会详细说的,我真滴很着急啊项目要交了。

最佳回复

只能去点击,日历控件先判断现在是第几个月,再往前或者后翻,再选几号,其实也还好写

🆗

data_editor_el = self.find_element(*self.data_editor)
data_editor_el.click()
# 显式等待直至日历控件可见
WebDriverWait(self.driver, 3).until(EC.visibility_of_element_located(self.calendar))
current_ym_el = self.find_element(*self.current_ym)
current_ym = current_ym_el.text
current_year, current_month = re.findall(C.number_pattern(), current_ym)
current_year, current_month = int(current_year), int(current_month)
start_year, start_month, start_day = re.findall(C.number_pattern(), start)
start_year, start_month, start_day = int(start_year), int(start_month), int(start_day)
end_year, end_month, end_day = re.findall(C.number_pattern(), end)
end_year, end_month, end_day = int(end_year), int(end_month), int(end_day)
while start_year < current_year:
    previous_year_el = self.find_element(*self.previous_year)
    previous_year_el.click()
    current_year -= 1
while start_year > current_year:
    next_year_el = self.find_element(*self.next_year)
    next_year_el.click()
    current_year += 1
while start_month < current_month:
    previous_month_el = self.find_element(*self.previous_month)
    previous_month_el.click()
    current_month -= 1
while start_month > current_month:
    next_month_el = self.find_element(*self.next_month)
    next_month_el.click()
    current_month += 1
day_el = self.find_elements(*self.available_date)[start_day - 1]
day_el.click()
while end_year < current_year:
    previous_year_el = self.find_element(*self.previous_year)
    previous_year_el.click()
    current_year -= 1
while end_year > current_year:
    next_year_el = self.find_element(*self.next_year)
    next_year_el.click()
    current_year += 1
while end_month < current_month:
    previous_month_el = self.find_element(*self.previous_month)
    previous_month_el.click()
    current_month -= 1
while end_month > current_month:
    next_month_el = self.find_element(*self.next_month)
    next_month_el.click()
    current_month += 1
day_el = self.find_elements(*self.available_date)[end_day - 1]
day_el.click()
共收到 7 条回复 时间 点赞

在执行 js 之后执行 send_keys 之前断个点,然后你手动去输入,点击搜索看下搜索结果是不是以你输入的条件搜索的

去踢球吧 回复

谢谢,我执行 js 后在输入搜索内容之前断了点,输入搜索内容,并没有以输入条件去搜索,应该是搜索条件(日历控件和下拉选择控件这两个控件的 input 的问题,但是我有输出这两个控件的 value,是正确的),但是一旦点击搜索,就不起作用了😔

如果是现在的 react,vue 都是 vdom,自己还有一层逻辑。所以直接设置值是不会生效的。
比如 react,class 组件内部有自己的 state,hooks 有 useState,还有 redux,mobox 之类的。
所以现在输入框 setvalue 并不能改变代码逻辑中的状态值

constructor(props) {
        super(props);
        this.state = {
            inputvalue: "",
        }
    }
cp 回复

这样子,有什么办法吗,特别是日历控件,不太好去点击。

只能去点击,日历控件先判断现在是第几个月,再往前或者后翻,再选几号,其实也还好写

cp 回复

受教了,我先去试试是否可行

🆗

data_editor_el = self.find_element(*self.data_editor)
data_editor_el.click()
# 显式等待直至日历控件可见
WebDriverWait(self.driver, 3).until(EC.visibility_of_element_located(self.calendar))
current_ym_el = self.find_element(*self.current_ym)
current_ym = current_ym_el.text
current_year, current_month = re.findall(C.number_pattern(), current_ym)
current_year, current_month = int(current_year), int(current_month)
start_year, start_month, start_day = re.findall(C.number_pattern(), start)
start_year, start_month, start_day = int(start_year), int(start_month), int(start_day)
end_year, end_month, end_day = re.findall(C.number_pattern(), end)
end_year, end_month, end_day = int(end_year), int(end_month), int(end_day)
while start_year < current_year:
    previous_year_el = self.find_element(*self.previous_year)
    previous_year_el.click()
    current_year -= 1
while start_year > current_year:
    next_year_el = self.find_element(*self.next_year)
    next_year_el.click()
    current_year += 1
while start_month < current_month:
    previous_month_el = self.find_element(*self.previous_month)
    previous_month_el.click()
    current_month -= 1
while start_month > current_month:
    next_month_el = self.find_element(*self.next_month)
    next_month_el.click()
    current_month += 1
day_el = self.find_elements(*self.available_date)[start_day - 1]
day_el.click()
while end_year < current_year:
    previous_year_el = self.find_element(*self.previous_year)
    previous_year_el.click()
    current_year -= 1
while end_year > current_year:
    next_year_el = self.find_element(*self.next_year)
    next_year_el.click()
    current_year += 1
while end_month < current_month:
    previous_month_el = self.find_element(*self.previous_month)
    previous_month_el.click()
    current_month -= 1
while end_month > current_month:
    next_month_el = self.find_element(*self.next_month)
    next_month_el.click()
    current_month += 1
day_el = self.find_elements(*self.available_date)[end_day - 1]
day_el.click()
醋精测试媛 关闭了讨论 11月10日 14:28
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册