使用Selenium WebDriver和Java点击复选框。

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

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.

使用Selenium WebDriver和Java点击复选框。

答案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();
    

huangapple
  • 本文由 发表于 2020年8月5日 03:03:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63253506.html
匿名

发表评论

匿名网友

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

确定