【问题标题】:How could I set value to an disabled input element如何将值设置为禁用的输入元素
【发布时间】:2015-12-02 14:10:51
【问题描述】:

我想通过 selenium 设置日期值。

这是一个具有禁用属性的数据选择器元素

这个脚本不适合我

@driver.execute_script("document.getElementById('departureDate').setAttribute('value', '2015-09-07')")

我什至无法通过这个@driver.execute_script("document.getElementById('departureDate').getAttribute('value')")获得价值

但是我可以通过

来检查值
[50] pry(#<Tiger>)> el = @driver.find_element(:id, "departureDate")
#<Selenium::WebDriver::Element:0x3f5415a2252a05c4 id="{f12a4095-6326-d241-b22d-aca9c03918e3}">
[51] pry(#<Tiger>)> el.attribute("value")
"2015-09-18"

但是如何使用2015-09-07 设置值

我正在处理这个页面https://m.tigerair.com/booking/search

【问题讨论】:

    标签: selenium


    【解决方案1】:

    使用querySelector() 通过提供css path来获取标签信息,然后将此js-code作为字符串添加到selenium。 p>

    var taginfo = document.querySelector('input#departureDate');
    var date = taginfo.value;
    console.log(date);
    

    改变试试这个。

    taginfo.value.replace('2015-09-08'); 
    

    【讨论】:

    • 如何将这个 js 库添加到 ruby​​ selenium,谢谢
    • 将 js 代码读取为字符串并提供给 executeScript() @driver.execute_script(jsAsString); see this
    • js_code = "return document.querySelector('input#departureDate')" tagInfo = browser.execute_script(js_code) date = tagInfo .attribute("value") here
    • 我不应该在网站中包含 js lib 还是使用 Ruby-binding selenium 做一些事情?但该网站不归我所有。
    • 不需要包含任何 js 库,这个 querySelector() 已经在浏览器中可用。
    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2021-11-15
    • 2018-09-25
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多