简体   繁体   English

无法使用Selenium(Java)选中特定的复选框

[英]No able to select particular check box using Selenium (Java)

I want to click on Store Pickup Available checkbox on following page 我要单击下一页上的“可用商店提货”复选框

http://www.target.com/c/pants-shorts-baby-toddler-boys-clothing/-/N-59yk1#navigation=true&viewType=medium&sortBy=newest&isleaf=true&navigationPath=59yk1&parentCategoryId=9976007&facetedValue=/-/N-59yk1&RatingFacet=0&categoryId=139007

And the particular HTML part has 并且特定的HTML部分具有

<input type="checkbox" name="facetId" id="in store, onlineCheckbox3" 
value="10058540" omniture="Store Pickup Eligible">

I tried many thing By.id() , By.cssSelector() and xpath also. 我尝试了很多东西By.id(),By.cssSelector()和xpath。

Can someone try and tell me the working code ... in-between I will continue trying. 有人可以告诉我工作代码吗……在此期间,我将继续尝试。

It will select By.id which you need to write 它将选择您需要编写的By.id

<input type="checkbox" name="facetId" id="facetId" 
value="10058540" omniture="Store Pickup Eligible">

The problem is the checkbox you want to click is hidden initially. 问题是您要单击的复选框最初被隐藏。 You can click it with something like this: 您可以通过以下方式单击它:

driver.findElement(By.xpath("//a[contains(text(),'in store, online')]")).click();
driver.findElement(By.xpath("//span[contains(text(),'Store Pickup Eligible')]/../../input")).click();

This will expand the element "in store, online", then click the checkbox labelled "Store Pickup Eligible". 这将展开元素“在线存储”,然后单击标记为“符合条件的商店提货”的复选框。

You can find this checkbox by CSS using: [id*='onlineCheckbox3'] . 您可以使用[id*='onlineCheckbox3']通过CSS查找此复选框。 As Richard stated, you must click the category to show the element first. 如Richard所述,您必须单击类别以首先显示元素。 Find this by: [id=dimensions]>ul>li:nth-child(9) a . 通过以下方法查找: [id=dimensions]>ul>li:nth-child(9) a Sometimes clicking an a doesn't work (depending on the html structure), and you must click an element inside the anchor, swap that out for span and it should work. 有时单击a无效(取决于html结构),并且您必须单击锚点内的元素,将其换成span才可以。 I prefer all CSS selectors when locating elements, but you can use whichever method you prefer. 定位元素时,我更喜欢所有CSS选择器,但是您可以使用任何喜欢的方法。

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

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