简体   繁体   English

无法在 java 中使用 selenium 单击复选框

[英]Cannot click on checkbox using selenium in java

I am trying to filter out names of books in Amazon, so after I navigated successfully and after I serached for my desired book I tried to filter out all the books that are in english on the side filter panel我正在尝试过滤掉亚马逊中的书籍名称,因此在我成功导航并搜索了我想要的书籍后,我尝试过滤掉侧面过滤器面板上的所有英文书籍

When I am trying in my code to click on the 'English' languge checkbox, it does click on something but the checkbox not being checked as expected, this is the html:当我尝试在我的代码中单击“英语”语言复选框时,它确实单击了某些内容,但未按预期检查该复选框,这是 html: 在此处输入图像描述

and this is my code:这是我的代码:

        List<WebElement> list = driverWrapper.findElements(By.cssSelector("ul[aria-labelledby='p_n_feature_nine_browse-bin-title'] li"));
        for(WebElement elem: list){
            if(elem.getText().equals(LangugaeFilterOptions.ENGLISH.getValue())){
                elem.click();
            }
        }

EDIT: This is the complete element html:编辑:这是完整的元素 html:

<li id="p_n_feature_nine_browse-bin/3291437011" aria-label="English" class="a-spacing-micro" xpath="1"><span class="a-list-item">                      <a data-routing="" class="a-link-normal s-navigation-item" tabindex="-1" href="/s?k=lord+of+the+rings+the+two+towers&amp;i=stripbooks-intl-ship&amp;rh=p_n_feature_nine_browse-bin%3A3291437011&amp;dc&amp;crid=23TXZEAWG63AR&amp;qid=1595601320&amp;rnid=3291435011&amp;sprefix=lord+of+the+rings+%2Cstripbooks-intl-ship%2C314&amp;ref=sr_nr_p_n_feature_nine_browse-bin_1">            <div class="a-checkbox a-checkbox-fancy s-navigation-checkbox aok-float-left"><label><input type="checkbox" name="" value=""><i class="a-icon a-icon-checkbox"></i><span class="a-label a-checkbox-label"></span></label></div>      <span class="a-size-base a-color-base" dir="auto">English</span>              </a>         </span></li>

You need to send the click on the input tag.您需要发送对输入标签的点击。 the click is going somewhere else.点击正在其他地方。 Modify the CSS/Xpath to reach the input below.修改 CSS/Xpath 以到达下面的输入。 Hope that should help!希望这会有所帮助!

You can try to findElements using xpath instead of cssSelector Here is the link of how you can do it -> https://stackoverflow.com/a/54746459/11278696您可以尝试使用findElements而不是cssSelector来查找xpath这是如何做到这一点的链接-> https://stackoverflow.com/a/54746459/11278696

The problem was that this checkbox is actually a link checkbox so what is needed to do is to click on the element问题是这个复选框实际上是一个链接复选框,所以需要做的是点击元素

driver.findElement(By.linkText("English").click();  

and that's would do the trick!这样就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM