英文:
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:
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:
Properties of the checkbox:
英文:
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:
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:
Properties of the checkbox:
答案1
得分: 0
所需标签是 <label>
标签。
解决方案
要点击与文本 "我至少年满18岁并同意官方规则" 相关的 [tag:checkbox],您可以使用以下 定位策略:
await driver.findElement(By.xpath("//label[@for='opt_rules']")).click();
英文:
The desired tag is a <label>
tag.
Solution
To click on the [tag:checkbox] 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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论