Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to click on the checkbox using Selenium Javascript and Xpath?

I am trying to target the checkbox to agree to the terms and conditions but have no name or id to use to target. I have innerHTML and innerText that I am trying to target with selenium but am only encountering errors. I am using Javascript and testing on chrome.

Code trial:

await driver.findElement(By.xpath("//*innerText[text()=’I am at least 18 years of age and agree to the Official Rules.’]")).click();

Error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let err = new ctor(data.message)
              ^

InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression //innerText[text()=’I am at least 18 years of age and agree to the Official 
Rules.’] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//innerText[text()=’I am at least 18 years of age and agree to the Official Rules.’]' is not a valid 
XPath expression.

Image of the checkbox:

imageofcheckbox

Properties of the checkbox:

propertiesofcheckbox

>Solution :

The desired tag is a <label> tag.


Solution

To click on the associated with the text "I am at least 18 years of age and agree to the Official Rules" you can use the following locator strategy:

await driver.findElement(By.xpath("//label[@for='opt_rules']")).click();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading