无法在Java中使用Selenium点击复选框。

huangapple go评论80阅读模式
英文:

Cannot click on checkbox using selenium in java

问题

我正在尝试在亚马逊上过滤出书名,所以在成功导航后,在我搜索了我想要的书之后,我尝试从侧边的筛选面板中过滤出所有英文书籍。

当我在我的代码中尝试点击“英语”语言复选框时,它确实点击了某个东西,但复选框的状态不像预期的那样被选中,这是HTML代码:
无法在Java中使用Selenium点击复选框。

这是我的代码:

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();
    }
}

编辑:
这是完整的元素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;amp;i=stripbooks-intl-ship&amp;amp;rh=p_n_feature_nine_browse-bin%3A3291437011&amp;amp;dc&amp;amp;crid=23TXZEAWG63AR&amp;amp;qid=1595601320&amp;amp;rnid=3291435011&amp;amp;sprefix=lord+of+the+rings+%2Cstripbooks-intl-ship%2C314&amp;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>
英文:

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:
无法在Java中使用Selenium点击复选框。

and this is my code:

        List&lt;WebElement&gt; list = driverWrapper.findElements(By.cssSelector(&quot;ul[aria-labelledby=&#39;p_n_feature_nine_browse-bin-title&#39;] li&quot;));
        for(WebElement elem: list){
            if(elem.getText().equals(LangugaeFilterOptions.ENGLISH.getValue())){
                elem.click();
            }
        }

EDIT:
This is the complete element html:

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

答案1

得分: 1

你需要触发对输入标签的点击。点击事件发生在其他地方。修改CSS/Xpath以定位到下面的输入框。
希望这能帮到你!

英文:

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.
Hope that should help!

答案2

得分: 0

你可以尝试使用 xpath 来进行 findElements,而不是使用 cssSelector
这里是一个关于如何使用它的链接 -> https://stackoverflow.com/a/54746459/11278696

英文:

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

答案3

得分: -1

问题是这个复选框实际上是一个链接复选框,所需操作是点击<a href>元素:

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

这样就可以解决问题了!

英文:

The problem was that this checkbox is actually a link checkbox so what is needed to do is to click on the <a href> element

driver.findElement(By.linkText(&quot;English&quot;).click();  

and that's would do the trick!

huangapple
  • 本文由 发表于 2020年7月24日 21:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63075113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定