2

Hi guys okay so I am a relative (read: complete) newbie to Selenium / Java / HTML so apologies if I am asking the obvious. What I need is to be able to

  1. Click on Specific Checkbox 1 and
  2. If Specific Checkbox 2 is checked, to uncheck it

Here is the Website HTML:

Specific Checkbox 1

<div class="checkbox"> <label id="agree_to_terms_label" for="agree_to_terms_join" class="visible"> <input id="agree_to_terms_join" name="agree_to_terms" type="checkbox" data-required="true" data-required-message="You need to agree to the *** Account Holder agreement" data-change="" class="parsley-validated"> <span class="left-block"></span> I have read, understand and agree to the <a href="/terms-and-conditions/" target="_blank">*** Account Holder Agreement</a> and acknowledge <a href="/privacy-policy" target="_blank">*** Privacy Policy</a> <input type="hidden" name="agree_to_terms" value="yes"> </label> </div> 

Specific Checkbox 2:

<div class="checkbox"> <label id="agree_to_offers_label" for="agree_to_offers" class="visible"> <span class="left-block"> <input id="agree_to_offers" name="agree_to_offers" type="checkbox" data-required-message="" data-change="" checked="checked" value="yes"> <span>By joining *** you'll be notified of exclusive offers and account updates via email</span> </span> </label> </div> 

My fruitless attempts:

Xpath:

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

Element not visible

driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label")).click(); 

Clicks on the href hyperlinks within the div instead

driver.findElement(By.xpath("//*[@id='agree_to_terms_label']/input")).click(); 

Element not visible

CSS:

driver.findElement(By.cssSelector("input[id = 'agree_to_terms_join'][type = 'checkbox']")).click(); 

Element not visible

by.className:

driver.findElement(By.className("checkbox")).click(); 

Opens hyperlinks

I had a look around the forums and saw mention of elements being hidden away - however I can't spot any iframes or anything else that appears to be hiding the bugger?

Any help would be greatly appreciated!!

5
  • Please check whether is frameCommentedSep 16, 2017 at 13:47
  • Hi I checked for a frame but it doesn't look like there is one. It seems like it isn't visible for another reason (which I don't know yet)
    – KZNKatana
    CommentedSep 16, 2017 at 14:01
  • Can u share URLCommentedSep 16, 2017 at 14:03
  • Try using id dearCommentedSep 16, 2017 at 14:06
  • Yes here: quidco.com/join-quidco
    – KZNKatana
    CommentedSep 16, 2017 at 14:06

3 Answers 3

2

Your exact element to click on is "::before" which is a pseudo element. I think you would need to use Javascript. Below code should work for you

WebElement element1 = driver.findElement(By.cssSelector(".left-block")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element1); 
2
  • You are an absolute star - this works! However there are two .left-block classes in the web html - would you know how I can ask Selenium to specify which one I want? So for instance I want Specific Checkbox 1 (in my original question) to be selected only, but not Checkbox 2 (if that makes sense?)
    – KZNKatana
    CommentedSep 16, 2017 at 15:04
  • Thanks man. You can always specify the index. Something like WebElement element1 = driver.findElement(By.cssSelector(".left-block")[Index]); where index will start from 1
    – Kapil
    CommentedSep 16, 2017 at 15:26
1

try following:

WebElement yourChkBox = driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label")); WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOf(yourChkBox)); Actions act = new Actions(driver); act.moveToElement(yourChkBox).click().build().perform(); 

UPDATE:

OR try using javascriptexecutor as below:

WebElement yourChkBox = driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label")); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('value', 'Yes');", yourChkBox ); 
3
  • Thanks just tried this: @And ("^Checks relevant boxes$") public void checksBoxes() { //driver.findElement(By.xpath(".//label[@id='agree_to_terms_label']")).click(); WebElement yourChkBox = driver.findElement(By.xpath("//*[@id='agree_to_terms_join']/parent::label")); WebDriverWait wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.visibilityOf(yourChkBox)); Actions act = new Actions(driver); act.moveToElement(yourChkBox).click().build().perform(); }
    – KZNKatana
    CommentedSep 16, 2017 at 14:41
  • But I still can't get it to click on the checkbox - the hyperlink keeps opening instead. Not sure what I am doing wrong!
    – KZNKatana
    CommentedSep 16, 2017 at 14:42
  • I used this with your code instead: js.executeScript("arguments[0].click();", yourChkBox ); and it worked! Many thanks :)
    – KZNKatana
    CommentedSep 16, 2017 at 15:15
0

Try this :

WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.quidco.com/join-quidco/"); driver.findElement(By.xpath(".//label[@id='agree_to_terms_label']")).click(); driver.findElement(By.xpath(".//label[@id='agree_to_offers_label']")).click(); 

If necessary put implicit or explicit wait

8
  • wait i will tryCommentedSep 16, 2017 at 14:11
  • Tried it but it opens up the hyperlinks in that div class instead whereas I am trying to select the checkbox only...I also tried making the webdriver wait and it still does not locate element. Am I doing something wrong?
    – KZNKatana
    CommentedSep 16, 2017 at 14:22
  • You can click on label to select it. Please show ur code. Because it is workin in MY PC. Also it doesnt open hyper link. I copied the code use itCommentedSep 16, 2017 at 14:24
  • if possible copy your code in questions. This code is quite simpleCommentedSep 16, 2017 at 14:28
  • @And ("^Checks relevant boxes$") public void checksBoxes() { driver.findElement(By.xpath(".//label[@id='agree_to_terms_label']")).click(); }
    – KZNKatana
    CommentedSep 16, 2017 at 14:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.