英文:
Cannot click on checkbox using selenium in java
问题
我正在尝试在亚马逊上过滤出书名,所以在成功导航后,在我搜索了我想要的书之后,我尝试从侧边的筛选面板中过滤出所有英文书籍。
当我在我的代码中尝试点击“英语”语言复选框时,它确实点击了某个东西,但复选框的状态不像预期的那样被选中,这是HTML代码:
这是我的代码:
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;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>
英文:
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:
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:
<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>
答案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("English").click();
and that's would do the trick!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论