Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 选择隐藏在按钮下的复选框_Jquery_Selenium_Selenium Webdriver - Fatal编程技术网

Jquery 选择隐藏在按钮下的复选框

Jquery 选择隐藏在按钮下的复选框,jquery,selenium,selenium-webdriver,Jquery,Selenium,Selenium Webdriver,我需要选中复选框,但单击功能失败 我的网页中有一个按钮: <button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style=""> spans... </button> 跨越。。。 我们有: <ul class="classa123 classb123" style=""> <li clas

我需要选中复选框,但单击功能失败

我的网页中有一个按钮:

<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="">
spans...
</button>

跨越。。。
我们有:

<ul class="classa123 classb123" style="">
<li class=" ">
<label class="classoflabel" title="" for="checkbox-123">
<input type="checkbox" title="Select This" value="1" name="multiselect-box-type" id="checkbox123">
<span>Select This</span>
</label></li>
    ...
  • 选择这个
  • ...
如何选择此复选框? 使用
find\u element\u by\u id(“checkbox123”)。是否选中了()
我发现该复选框未选中,但当我尝试时。单击()失败。我想我应该先点击按钮打开复选框,但是按钮没有唯一的id或类。 我将SeleniumWebDriver与python一起使用。如果您知道如何解决这个问题,您也可以用其他语言编写代码。
谢谢……

好的,我想我已经得到你需要的了。首先,如果使用jQuery,可以使用任何CSS选择器作为选择器来获取元素。例如,要获取带有
checkbox123
id
的输入,您只需:

$('#checkbox123')
但是,如果您想要在
ul
中收集输入
复选框
es,则可以选择如下:

$('ul input[type=checkbox]')
最后,确定是否检查它非常简单

var checked = $('#checkbox123').is(':checked');
//  see your result in developer console with 
console.log(checked); // will be `true` or `false`
如果您试图在单击按钮后获取跨度(使用文本
选择此
),您可以尝试以下操作:

$('button').click(function(e) {
    $('ul input[type=checkbox]:checked').each(function(i) {
        var span = $(this).next('span');
        alert(span.text())
    });
})
[input#checkbox123, context: document, selector: "#content", jquery: "1.9.......and so on for rest of object
[context: document, selector: "#checkbox123", jquery: "1.9.......and so on for rest of object

最后一次调用,如果需要更改复选框的状态,请使用带有布尔值的on
checked
。例如:

$('#checkbox123').prop('checked', true); // will make it checked
$('#checkbox123').prop('checked', false); // will uncheck it
我相信这可能是我上次回答问题后更新的,所以我将在这里更新答案,以显示明显缺少的内容

最后,使用jQuery触发事件,例如
单击
,非常简单。也有几种方法可以做到这一点,有些基本上是一样的

最简单的是使用。简单到:

$('#checkbox123').click();
下一个用途是。基本上与
相同。单击

请记住,如果你正在使用它,但它不起作用。您的选择器可能有问题。一种确定的方法是控制台记录它。如果您没有看到打印到控制台的jQuery对象元素包含您期望的元素,那么您就知道选择器不起作用。请记住,这将显示一个jQuery对象,但其中不会包含HTML元素

console.log($('#checkbox123').trigger('click'));
如果存在,将类似于:

$('button').click(function(e) {
    $('ul input[type=checkbox]:checked').each(function(i) {
        var span = $(this).next('span');
        alert(span.text())
    });
})
[input#checkbox123, context: document, selector: "#content", jquery: "1.9.......and so on for rest of object
[context: document, selector: "#checkbox123", jquery: "1.9.......and so on for rest of object
如果不存在,将类似于:

$('button').click(function(e) {
    $('ul input[type=checkbox]:checked').each(function(i) {
        var span = $(this).next('span');
        alert(span.text())
    });
})
[input#checkbox123, context: document, selector: "#content", jquery: "1.9.......and so on for rest of object
[context: document, selector: "#checkbox123", jquery: "1.9.......and so on for rest of object

尝试按如下方式触发click事件:

   var element = $('#checkbox123')[0];
   if (document.createEvent) { 
      var e = document.createEvent("MouseEvents");
      e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
      element.dispatchEvent(e);
  }

通过\u id查找\u元素
?您不是在使用jQuery吗?您的问题非常令人困惑。您可以对您的问题进行分析,或者重写问题,一步一步地引导我们了解您正在做的事情和您的期望吗?@SpYk3HH是的,但此代码用于查找此复选框并测试其是否选中。@SpYk3HH我正在尝试选择“选择此”输入。我尝试使用。在查找是否选中此输入后单击,但在查找框后,单击函数返回错误。您仍然没有意义。我尝试了一个答案,但是如果你重写这个问题,它会非常有帮助。我对复选框是否选中没有问题,唯一的问题是单击,我需要选中此复选框。如果选中,效果很好,我需要选中此复选框。我用了click,但它不起作用他说click()不起作用…这适用于选择框,所以它可能在这里起作用。。。