2016-09-15 5 views
1

私はeclipse neon、selenium webdriver 2.53.0とfirefox 45.3.0 esrで作業しています。 サイト上で唯一のチェックボックスにセレンマークを付けることはできません。ここに私のテストスクリプトは次のとおりです。セレンのwebdriverがチェックボックスをクリックしないようにすることはできません

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("[email protected]"); 
     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コードです:私は2日間のためにそれを戦ってきたので

<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> 

は、あなたの助けを本当に感謝します。

+0

あなたはどのブラウザを使用していますか? – Techidiot

+0

私が言及したように - firefox 45.3.0 esr –

答えて

1

あなたはその少しトリッキーIEブラウザ上にある場合 -

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

それとも問題が解決しない場合は要素に

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

に到達するためにXPathを使用してみてください、クラス名

を使用してみてください
WebElement mybox = driver.findElement(By.className("squaredOne"));  
mybox.click(); 
+0

最後のスクリプトはうまく動作します。どうもありがとうございます。 –

+0

@JRodDynamite - 私が何かを見逃したと感じたら答えを編集してください。そしてOPは後でそれを言いました。 – Techidiot

+0

@Techidiot - 私の投票を逆転させました。急いで質問を読む。私の悪い。 – JRodDynamite

0

コード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(); 
    
関連する問題