簡體   English   中英

無法使Selenium WebDriver單擊復選框

[英]Can't make selenium webdriver click the checkbox

我正在研究Eclipse Neon,Selenium WebDriver 2.53.0和Firefox 45.3.0 ESR。 我無法使硒標記成為網站上唯一的復選框。 這是我的測試腳本:

package testy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class rejestr {
    public WebDriver driver;
    @BeforeMethod
    public void beforeMethod() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("http://dev.wedkarz.pzw.pl/#/login");
    }
    @Test
    public void f () throws InterruptedException { 
        driver.findElement(By.className("nav-item")).click();
        driver.findElement(By.id("firstName")).sendKeys("Jan");
        driver.findElement(By.id("lastName")).sendKeys("Nowak");
        driver.findElement(By.id("email")).sendKeys("jnowak@o2.pl"); 
        driver.findElement(By.id("plainPassword")).sendKeys("password");
        new Select(driver.findElement(By.id("district"))).selectByVisibleText("Okręg     PZW w Bydgoszczy");
        new Select(driver.findElement(By.id("circle"))).selectByVisibleText("Koło PZW nr 31");
        driver.findElement(By.id("fishingLicense")).sendKeys("12345678990");
        driver.findElement(By.id("squaredOne")).click();
    }
    @AfterMethod
    public void afterMethod() {
        System.out.print("Test zakończony powodzeniem");
        driver.quit();
    }
}

這是復選框的html代碼:

<div class="row form-group m-b-lg">
              <div class="squaredOne">
                <input id="squaredOne" name="check" type="checkbox" value="None" class="ng-untouched ng-valid ng-dirty">
                <label for="squaredOne">
                  <p>Wyrażam zgodę na otrzymywanie od PZW informacji<br> o charakterze promocyjnym i reklamowym
                  przekazywanych drogą elektroniczną, w tym przesyłanych z wykorzystaniem telekomunikacyjnych
                  urządzeń końcowych (np. komputer, tablet).</p>
                </label>
              </div>
              <span class="help-block">

              </span>
            </div>

非常感謝您的幫助,因為我已經努力了2天。

如果您使用的是IE瀏覽器,則有些棘手-

if (driver.Capabilities.BrowserName.Equals(“internet explorer"))
    driver.findElement(By.id("squaredOne").SendKeys(Keys.Space);
else
    driver.findElement(By.id("squaredOne").Click();

或者嘗試使用Xpath到達元素

driver.findElement(By.xpath("//input[@type='checkbox']")).click();

如果這樣不起作用,請嘗試使用類名

WebElement mybox = driver.findElement(By.className("squaredOne"));     
mybox.click();

您的代碼driver.findElement(By.id("squaredOne")).click()應該可以工作。 但是,如果沒有,您可以嘗試:

  1. 將選擇器更改為xpath:

     driver.findElement(By.xpath("//input[@id='squaredOne']")).click(); 
  2. 點擊父div:

     driver.findElement(By.xpath("//div[./input[@id='squaredOne']]")).click()` 
  3. 使用動作類:

     Actions actions = new Actions(driver); actions.click(driver.findElement(By.xpath("//input[@id='squaredOne']"))); actions.build().perform(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM