英文:
Clicking checkboxes with Selenium WebDriver java
问题
我有些困难,无法让一些复选框被点击。通常的XPath不起作用,也没有ID。我已经附上了HTML代码。
英文:
I am having trouble getting some checkboxes to be clicked. the usual XPath does not work, and there is no ID. I have attached the HTML code.
答案1
得分: 1
driver.findElement(By.className("custom-control-label")).click();
英文:
driver.findElement(By.className("custom-control-label")).click();
Try above line. Thanks.
答案2
得分: 0
-
cssSelector
:使用以下[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890)来`click()`元素:
driver.findElement(By.cssSelector("input#contactAuth")).click();
-
xpath
:使用以下[定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890)来`click()`元素:
driver.findElement(By.xpath("//input[@id='contactAuth']")).click();
理想情况下,为了click()
元素,您需要触发WebDriverWait以等待elementToBeClickable()
,您可以使用以下定位策略之一:
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.birtviewer_clickable[name='exportReport']"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='birtviewer_clickable']"))).click();
英文:
To click()
on the element you can use either of the following Locator Strategies:
-
cssSelector
:driver.findElement(By.cssSelector("input#contactAuth")).click();
-
xpath
:driver.findElement(By.xpath("//input[@id='contactAuth']")).click();
Ideally, to click()
on the element, you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.birtviewer_clickable[name='exportReport']"))).click();
-
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='birtviewer_clickable']"))).click();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论